Day 13 of 100 Days : Creating and Running a Dockerized Web Applicationš
Welcome to Day 13! Today, weāll embark on an exciting journey where youāll take a web application and make it run inside a Docker container. By the end of this lesson, youāll have Dockerized a Django web app, built a Docker image, run it in a container, and even accessed it from your browser. Letās dive into the details!
š Step 1: Choose Your Web Application
To make this process easy to follow, weāll work with a sample Django application. Donāt worry if youāre new to Django; weāre mainly focusing on Docker concepts. Hereās how weāll start:
Clone the Repository: Letās use an existing project so you can focus on Docker without getting sidetracked by setting up the app. Open your terminal and run:
git clone https://github.com/iam-veeramalla/Docker-Zero-to-Hero.git
This command clones a repository containing sample Docker projects.
Navigate to the Web App Folder: Change directories to locate the web app files. Run:
cd Docker-Zero-to-Hero/examples/python-web-app
Now youāre in the folder where weāll add a Dockerfile to package and run this app in Docker.
š Step 2: Create a Dockerfile
A Dockerfile is a text document containing all the instructions needed to build your Docker image. This file defines the environment Docker will create to run your application, specifying base images, dependencies, files, and commands.
Create the Dockerfile: Open a new file named
Dockerfile
in thepython-web-app
directory. Hereās the content to include:# Use the official Ubuntu image as the base FROM ubuntu # Set the working directory inside the container WORKDIR /app # Copy the dependencies file and application code into the container COPY requirements.txt /app COPY devops /app # Update the package list and install Python and its package manager, pip RUN apt-get update && \ apt-get install -y python3 python3-pip && \ pip install -r requirements.txt && \ cd devops # Set the entry point to start the application ENTRYPOINT ["python3"] CMD ["manage.py", "runserver", "0.0.0.0:8000"]
Explanation of Each Line:
FROM ubuntu
: This line sets the base image. Here, weāre using Ubuntu as our starting point, which provides a clean and flexible environment.WORKDIR /app
: TheWORKDIR
command sets the default directory inside the container where subsequent commands will execute. Here, it creates and navigates to/app
.COPY requirements.txt /app
andCOPY devops /app
: These lines copy necessary files from our local machine to the container.requirements.txt
includes the Python libraries our app depends on, anddevops
contains the application code.RUN apt-get update ...
: This installs Python 3 and pip, then uses pip to install all required packages listed inrequirements.txt
. This step sets up the environment for the Django app to run.ENTRYPOINT
andCMD
: Together, these define the commands to start the Django development server.manage.py runserver 0.0.0.0:8000
will start the server on port8000
, making it accessible from outside the container.
This Dockerfile is now a blueprint that Docker will use to create our application image.
š Step 3: Build the Docker Image
Now, letās build the Docker image, which will package everything our application needs into a single, portable unit.
Build the Image: In your terminal, run:
docker build -t my-django-app .
The
-t
flag lets us add a tag to the image, in this case,my-django-app
. This tag gives the image a readable name, making it easy to refer to later.The
.
at the end specifies the current directory as the build context, meaning Docker will include all files in this directory when building the image.
Watch for Completion: As Docker processes each line in your Dockerfile, youāll see a series of steps building your image. Once it finishes, your Docker image is ready!
To verify, you can list all Docker images with:
docker images
ā¶ļø Step 4: Run the Docker Container
With our Docker image built, itās time to run it as a container. Running a container will allow us to interact with our application as if it were on a live server.
Start the Container: Execute the following command:
docker run -p 8000:8000 -it my-django-app
The
-p
option maps port8000
on your machine to port8000
inside the container, allowing you to access the application in your browser.The
-it
flag makes the container interactive and enables a terminal session.my-django-app
refers to the tag of the image we built.
Test in Your Browser: Open a browser and go to http://localhost:8000. If everything is set up correctly, you should see the running Django application! š
š Step 5: Publish Your Docker Image to Docker Hub
To make your image available for others or use it on other machines, you can push it to Docker Hub. Follow these steps:
Login to Docker Hub: If you havenāt logged in, use:
docker login
Enter your Docker Hub username and password when prompted.
Tag the Image: For Docker Hub, tag your image with your Docker Hub username:
docker tag my-django-app <your_dockerhub_username>/my-django-app
Replace
<your_dockerhub_username>
with your actual Docker Hub username.Push the Image: Finally, push your image to Docker Hub with:
docker push <your_dockerhub_username>/my-django-app
Youāll see a progress bar as Docker uploads your image. Once done, itās public on Docker Hub, and anyone can download and run it.
š Stopping the Container
When youāre done, you can stop your container by using CTRL + C
in the terminal running the container, or open a new terminal window and run:
docker stop <container-id>
To get the container ID, use docker ps
to list running containers.
š Recap
Today, you:
Created a Dockerfile for a Django web app.
Built and ran a Docker image of the app.
Verified the app in a browser and published your image to Docker Hub.
Great job! You now have a solid grasp of Dockerizing a web application and sharing it. Letās keep building on this foundation! š
Subscribe to my newsletter
Read articles from Munilakshmi G J directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Munilakshmi G J
Munilakshmi G J
"Aspiring DevOps Engineer on a 100-day journey to master the principles, tools, and practices of DevOps. Sharing daily insights, practical lessons, and hands-on projects to document my path from beginner to proficient. Passionate about continuous learning, automation, and bridging the gap between development and operations. Join me as I explore the world of DevOps, one day at a time!"