Docker Project for DevOps Engineers (Day15)

Araiz bin Saqib
6 min readJul 9, 2024

--

Hey Everyone!

In our previous discussion, we covered Docker for DevOps Engineers. If you haven’t read it yet, you can check it out [here].

Today, we will delve into a Docker projects for DevOps Engineers. We will be deploying an actual project using Docker on AWS EC2.

Installation of Docker on EC2 (Guide)

To run an EC2 machine using an SSH client, Docker must be installed and running. We will start the project deployment using a Docker image and container. The project is a simple to-do app built with Python and Django, and it is owned by its respective owner.

[Link to the project’s GitHub repository]

First, we will start the instance and then connect to it.

Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

As I am on my local machine and connected to my AWS EC2 instance using an SSH connection, we will first create a new folder named ‘projects’ on the instance and then clone the project into it.

mkdir projects

git clone https://github.com/shreys7/django-todo.git

Before that, we need to make some changes to the settings of our application. Specifically, we need to allow the IP address of our host machine to access the application. To do this, we will modify the settings.py file in our todoApp folder.

ubuntu@ip-172-31-18-249:~/projects/django-todo$ vi todoApp/settings.py

We will find the Allowed hosts and will add its value as * which means for all and allows every IP address to access our application.

Now that we have modified the host’s settings, we will allow a port to open the application by modifying our AWS Security Group and adding the following rules.

We will add this rule. I will be accessing the application through port 8003, but you can use any other port as well. After this, we will move on to the creation of the Dockerfile. In the project repository, we run the following command:

vi Dockerfile

Build the image using the Dockerfile and run the container:

To create a docker image:

vi Dockerfile

This is a Dockerfile that sets up a containerized environment for a Django application. Here’s a breakdown of each line:

FROM python:3

  • It will bring an Ubuntu image(FROM) in which python 3 is already installed. This line sets the base image for your Docker container. It specifies that you want to use the official Python 3 image from Docker Hub as the base for building your own image.
  • It will basically make a virtual box in which the OS runing would be Ubuntu.

RUN pip install django==3.2

  • This command installs Django version 3.2 inside the Docker container. The RUN instruction executes commands during the build process of the Docker image. Here, it uses pip (Python's package installer) to install Django version 3.2.

COPY . .

  • This copies all files from your current directory on your local machine (where the Dockerfile is located) into the Docker image’s working directory (. means current directory). This is typically done to include your Django project files and any other necessary configurations.

RUN python manage.py migrate

  • After copying your project files, this command runs Django’s manage.py migrate command inside the Docker container. This command applies any pending database migrations that are defined in your Django project. It ensures that your database schema is up to date with your Django models.

CMD [“python”,”manage.py”,”runserver”,”0.0.0.0:8002"]

  • This sets the default command to run when a container is started from this Docker image. Here, it runs the Django development server (manage.py runserver) on host 0.0.0.0 (which means it will be accessible externally) and port 8002.

Summary:

  • The Dockerfile starts with a Python 3 base image.
  • It installs Django version 3.2.
  • Copies your local project files into the Docker image.
  • Applies database migrations.
  • Sets up the container to run the Django development server on port 8002.

Now, we’ll run the image aka Dockerfile:

docker build .

When you run docker build ., you're telling Docker to build a Docker image using the instructions provided in a file called Dockerfile. Here’s what happens in simple terms:

  1. Dockerfile: Imagine a recipe that tells Docker how to create a complete environment for your application.
  2. docker build .: This command tells Docker to look in the current directory (.) for a file named Dockerfile. Docker then reads this file to know what to include in the environment.
  3. Building: Docker follows the instructions step by step in the Dockerfile. It might install necessary software (like Python or Django), copy your project files into the environment, and set up everything your application needs to run.
  4. Result: After docker build . finishes, Docker creates a packaged-up environment (called an image) that includes your application and all its dependencies. This image is like a snapshot of your application's setup that can be run anywhere Docker is installed.

In essence, docker build . is like asking Docker to assemble all the parts your application needs into a neat, portable package that can be easily shared and run on any system that has Docker installed.

docker automation

To check if the image is running:

sudo docker ps

to run a container:

sudo docker run

The highlighted number in the below picture is the container id, that we need to run:

Let’s break down the command sudo docker run -p 8002:8002 fac9825078e6:

sudo docker run:

  • sudo is used to run the Docker command with administrative privileges, assuming the user has the necessary permissions.
  • docker run starts a new Docker container based on an existing Docker image.

-p 8002:8002:

  • This option (-p) is used to map ports between the Docker container and the host machine.
  • 8002:8002 specifies that port 8002 on the host should forward to port 8002 in the Docker container.
  • This means any traffic sent to port 8002 on the host machine will be forwarded to port 8002 within the Docker container.

fac9825078e6:

  • This is the unique identifier (ID) or name of the Docker image you want to run as a container.
  • Docker uses this ID to find the correct image to use when starting the container.

Summary:

  • sudo docker run -p 8002:8002 fac9825078e6 starts a new Docker container based on the specified image (fac9825078e6).
  • It maps port 8002 on your local machine to port 8002 inside the Docker container.
  • This command effectively starts your application or service inside the Docker container, making it accessible on port 8002 of your host machine.

For Reference Project visit here

Verify that the application is working as expected by accessing it in a web browser

#sudo docker run -p ports [containerid]
sudo docker rum -p 8002:8002 fac9825078e6

It runs the docker container and as a result, our application starts running.

Happy Learning:)

--

--