In this blog, we are going to discuss Dockerfile and the project.
So Let's start our discussion with Dockerfile.
What is Dockerfile and why do we need it?
- It is a normal text file that contains a set of instructions in it to build a docker image. It is an automated way of creating docker images.
In this file, we mentioned the required OS image and required software in the form of instructions.
Dockerfile instructions are as follows:
FROM | It is used for the base image. e.g. ubuntu |
RUN | It is used to run commands before creating containers. |
MAINTAINER | Author / Owner / Description |
COPY | Copy files or directories from the host to the docker image |
ADD | Copy files or directories from the host to the docker image as well as downloads from the internet. |
EXPOSE | To specify the network port for the docker container |
CMD | It will run commands after creating containers. |
ENTRYPOINT | It will execute commands when the container starts. |
ENV | Environment Variables |
Now Let's understand the project files.
README.md: The file which is written by the developer to know how to run the code.
This project is in Python. we can see the .py extension.
manage.py: Need to run this file to run the application.
requirements.txt: This file contains the information regarding what is the requirement to run the application.
rquirements.txt =>For Django or Python projects.
POM.XML ==> For Java projects.
For Node.js ==> For Package.json
Task:
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Build the image using the Dockerfile and run the container
Verify that the application is working as expected by accessing it in a web browser
Push the image to a public or private repository (e.g. Docker Hub )
Steps:
- Launch AWS ubuntu ec2 instance.
2. Clone the repository from GitHub to your Ubuntu server using the command:
git clone <repository name>
git clone https://github.com/parimal007/react_django_demo_app.git
Create a Dockerfile:
FROM: It is used for the base image.
COPY: To Copy file from source to destination.
RUN: To execute commands before container creation
CMD: Execute commands during container creation.
Build an Image using Dockerfile:
To build an image using Dockerfile, Go to the directory that has your Dockerfile and run
docker build . -t <image name>
. means present working directory. Dockerfile is kept in the present working directory.
-t ==> For tagging an image.
docker build . -t react-django-app:latest
Now let's check whether the image is created or not.
type command: docker images
Run the image to create a container
We have created an image from the docker file now we have to create a container from that image.
docker run -d -p 8080:8080 react-django-app
-d ==> detached mode.
-p ==> Port mapping
How to Check Container running or not?
Use the command: docker ps
Verify that the application is working as expected by accessing it in a web browser
What if the app is not running on the 8080 port?
It means we need to allow it from a security group. (AWS security group)
Select the instance and go to the security tab and select security group.
Click on Edit inbound rules.
Click on Add rule and select custom TCP and add port 8080 Anywhere and save rules.
Now refresh the page.
Push the image to a public or private repository (e.g. Docker Hub)
What is Docker Hub?
Docker Hub is a registry service on the cloud that allows you to download docker images. We can also upload our docker images to the docker hub.
Now push the docker image into the docker hub. Before pushing enter your docker hub credentials.
docker login ==> for docker hub login
docker image tag react-django-app parimal1984/djangoapp
docker push parimal1984/djangoapp
I hope you would like this blog. In the next blog, we will discuss docker architecture and many more.
Thank you for reading this blog.Happy Learning!!!!!
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe