Dockerfile
Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. A Dockerfile is a text file that contains a set of instructions that are used to create a Docker image. A typical Dockerfile consists of several steps, each of which corresponds to a specific action in the build process. These steps are executed in order, and the resulting image is built layer by layer.
In this blog, we will create a docker container to run our application in any environment and to do this we will follow below steps:
Task:
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Here we are creating a Dockerfile for react_django_demo_app. But before that follow the below steps:
git clone
https://github.com/prshende/react_django_demo_app.git
This will copy the project code from the GitHub repository to your local.
Now create a Dockerfile based on the application requirements,
This Dockerfile uses the official Python 3.9 image as the base image and sets the working directory to
/app
. It then copies the contents of the current directory to the/app
directory in the container. Next, it installs the required packages specified in therequirements.txt
file using pip. It exposes port 8001 for the Django application to listen on. Finally, it runs thepython
manage.py
runserver 0.0.0.0:8001
command to start the Django development server and make the application available on port 8001.Build the image using the Dockerfile and run the container
sudo docker build . -t react-django-app:latest
By using the above command it will create a Docker image for us by using Dockerfile.
You can check the Docker images by using the below command,
Now we have to convert the Docker image to the container by using the below command,
sudo docker run -d -p 8001:8001 react-django-app:latest
We can check the containers using
docker ps
command,Verify that the application is working as expected by accessing it in a web browser
To check the application working you have to do certain steps:
Add rule in your inbound rule to expose port 8001,
The application is now up and running,
Push the image to a public or private repository (e.g. Docker Hub )
To push the Docker image to a public or private repository, follow these steps:
Tag the image with the repository name using the following command:
docker tag <image_id> <repository_name>:<tag>
docker tag <7d536a556e64> <react-django-app>:<latest>
Log in to the Docker registry using the following command:
sudo docker login
Push the Docker image to the registry using the following command:
docker push <repository_name>:<tag>
sudo docker tag docker-app:latest pratik1052/django-app:latest
sudo docker push pratik1052/django-app:latest
Thank you for reading the blog.
Suggestions are always welcome. Thank you !!