Hello Everyone! 🌟
Welcome to Day 17 of our #90DaysOfDevOps journey! Today is an exciting milestone as we dive into our first DevOps project using Docker. We’ve spent time learning about Docker, its core concepts, and commands, and now it’s time to put that knowledge into practice. Today, we’ll create a Dockerfile for a simple web application, build a Docker image, run it in a container, and finally, push that image to a Docker repository. Let’s get started!
What is a Dockerfile?
A Dockerfile is a text file that contains a series of instructions on how to build a Docker image. Think of it as a recipe that tells Docker what base image to use, what dependencies to install, how to configure the environment, and which commands to run to get your application up and running.
When Docker reads this file, it follows each step to assemble a Docker image, which can then be used to create containers that run your application consistently across any environment.
Why Dockerfile is Important
Dockerfiles are crucial in the DevOps world because they allow for reproducibility and version control of your application’s environment. By defining everything your app needs in a Dockerfile, you can ensure that it behaves the same way no matter where it’s deployed—whether it’s on your local machine, a CI/CD pipeline, or in production. This consistency reduces the classic "it works on my machine" problem and makes collaboration and deployment much more straightforward.
Hands-On Task:
Creating and Running a Dockerized Web Application
Today’s task involves creating a Dockerfile for a simple web application, building the Docker image, running it as a container, and verifying its functionality by accessing it in a web browser. Let’s walk through the process step-by-step.
Choose Your Web Application
First, decide which type of web application you’d like to Dockerize. For this tutorial, we’ll use a simple Django application, but you could just as easily use Node.js, Ruby, or any other web framework.
I will be using a simple django to-do application by Shubham Londhe, you can too get it by
git clone https://github.com/LondheShubham153/django-todo-cicd.git
Create a Dockerfile
Now, create a file named Dockerfile in the same directory as your app.py file. Here’s an example Dockerfile for our application:
dockerfile
#Use the official Python image from the Docker Hub as a base image FROM python:3.9-slim #Set the working directory inside the container WORKDIR /app #Copy the current directory contents into the container at /app COPY . /app #Run and install django RUN python -m pip install --no-cache-dir Django # Apply database migrations RUN python manage.py makemigrations RUN python manage.py migrate #Make port 8000 available to the world outside this container EXPOSE 8000 #Define the command to run the application CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Let’s break down what each line does:
Base Image:
FROM python:3.9-slim
Uses the official, lightweight Python 3.9 image from Docker Hub as the base for the container.
Working Directory:
WORKDIR /app
Sets the working directory inside the container to
/app
, ensuring all subsequent commands are executed relative to this directory.
Copy Project Files:
COPY . /app
Copies all files and directories from your current local directory into the
/app
directory within the container.
Install Django:
RUN python -m pip install Django
Installs Django using Python’s package manager,
pip
, ensuring that the Django framework is available in the container.
Create Migrations:
Expose Port:
EXPOSE 8000
Opens port 8000, making it available for external access to your Django application running inside the container.
Run Django Server:
CMD ["python", "
manage.py
", "runserver", "0.0.0.0:8000"]
Starts the Django development server, allowing the application to be accessed through the specified port (8000) of localhost(0.0.0.0).
Build the Docker Image
With your Dockerfile in place, it’s time to build the Docker image. Open your terminal, navigate to the directory containing your Dockerfile, and run the following command:
docker build -t my-django-app .
Let’s break this command down:
docker build: This command tells Docker to build an image.
-t my-django-app: This flags the image with the name my-flask-app.
. : The period at the end tells Docker to look for the Dockerfile in the current directory.
Once the build process is complete, you’ll have a new Docker image named my-flask-app.
Run the Docker Container
Now that we have an image, let’s run it in a container. You can start the container using the following command:
docker run -d -p 8000:8000 my-django-app
Let’s break down this command:
-d: This runs the container in detached mode, meaning it will run in the background.
-p 8000:8000: This maps port 8000 on your local machine to port 8000 inside the container. This is necessary because the Django app is running on port 8000 in the container.
my-django-app: This is the name of the Docker image we created.
Verify the Application
To verify that your application is running correctly, open your web browser and navigate to 0.0.0.0:8000. You should see the django application up and running. This confirms that your Dockerized application is up and running.
Push the Image to a Docker Repository
Finally, let’s push our Docker image to a repository like Docker Hub so that it’s accessible from anywhere. If you haven’t already, you’ll need to create an account on Docker Hub.
First, log in to Docker Hub via the command line:
docker login
After logging in, tag your image with your Docker Hub username:
docker tag my-django-app tusharpant1405/my-django-app
Now, push the image to Docker Hub:
docker push tusharpant1405/my-django-app
This command uploads your image to Docker Hub, making it available for anyone with access to your repository.
Conclusion
Congratulations! 🎉 You’ve just created, built, and deployed your first Dockerized web application. You’ve also learned how to push Docker images to a public or private repository, a critical step in sharing and deploying your applications in a real-world DevOps environment.
Docker is a powerful tool that simplifies the process of developing, shipping, and running applications. Mastering Docker is a vital skill for any DevOps engineer, and today’s task is an important step toward that mastery.
What's Next?
As you continue your #90DaysOfDevOps journey, keep experimenting with Docker. Try creating more complex Dockerfiles, explore Docker Compose for multi-container applications, and dive into orchestration with Docker Swarm or Kubernetes. Each of these topics will deepen your understanding of containerization and prepare you for the challenges of modern software deployment.
Share Your Progress
As always, don’t forget to share your progress and experiences on LinkedIn. Let your network know what you’ve learned, and encourage others to join you on this exciting journey.
Stay tuned for the next challenge, and keep up the amazing work! 🚀