Docker has revolutionized the way we deploy and manage applications by introducing the concept of containers. These containers encapsulate everything an application needs to run, making it portable and easy to manage. At the heart of creating these containers lies the Dockerfile—a set of instructions that defines how a container image is built.
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. To create these containers, developers use something called a Dockerfile.
A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.
Task: Creating a Dockerfile for a Simple Web Application
- Let's walk through creating a Dockerfile for a simple web application. For this demonstration, we'll use a Django to-do app. Follow these steps:
git clone https://github.com/theshubhamgour/django-todo-cicd.git
- Navigate to the project folder:
cd django-todo-cicd.git
- Create a Dockerfile:
FROM python:3
WORKDIR /data
RUN pip install django==3.2
COPY . .
RUN python manage.py migrate
EXPOSE 8000
CMD ["python","manage.py","runserver","0.0.0.0:8000"]
Let's break down each instruction in the Dockerfile:
FROM python:3
- This instruction sets the base image for the Docker container. In this case, it uses the official Python 3 image as the starting point. This image includes a minimal Python environment.
WORKDIR /data
- The
WORKDIR
instruction sets the working directory inside the container where subsequent instructions will be executed. Here, it sets the working directory to/data
.
- The
RUN pip install django==3.2
- The
RUN
instruction executes a command during the build process. In this case, it installs Django version 3.2 using thepip
package manager. This ensures that the required dependencies for the application are present in the container.
- The
COPY . .
- The
COPY
instruction copies files from the host machine (the current directory, represented by.
) into the container. This step is crucial for including the application code in the container.
- The
RUN python
manage.py
migrate
- Another
RUN
instruction, this time running the Django management commandmigrate
. This command applies any database migrations defined in the Django application. It ensures that the database is set up correctly.
- Another
EXPOSE 8000
- The
EXPOSE
instruction informs Docker that the container will listen on the specified network ports at runtime. While this doesn't publish the specified ports to the host machine, it serves as documentation for developers about which ports to use.
- The
CMD ["python", "
manage.py
", "runserver", "0.0.0.0:8000"]
- The
CMD
instruction provides a default command to run when the container starts. In this case, it starts the Django development server and binds it to0.0.0.0:8000
, making it accessible from outside the container.
- The
- Build the Docker image:
docker build -t django-app .
Check whether the image is created
docker images
Lets run the image
docker run -it -p 8000:8000 0734337395f8
docker run
: This command is used to create and start a new Docker container.-it
: These are options that combine two flags.-i
stands for interactive, and-t
allocates a pseudo-TTY, which allows you to interact with the container's command-line interface. Together, they enable interactive mode.-p 8000:8000
: This option maps the host machine's port 8000 to the container's port 8000. It is used to expose and publish ports between the host and the container.0734337395f8
: This is the container ID or image name. The container ID is a unique identifier for a running containerVerify 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 )
docker push theshubhamgour/django-app
Lets change the tag of the docker image
docker tag django-app theshubhamgour/django-app
Now let's push the image
docker push theshubhamgour/django-app
Verify on DockerHub
Conclusion
Dockerfiles are powerful tools that simplify the process of containerizing applications. By following the steps outlined in this guide, you've created a Dockerfile for a simple web application, built a Docker image, and pushed it to a repository. Understanding Dockerfiles opens the door to efficient application deployment and management in containerized environments.