Running a Docker container using a Docker file
What's a Dockerfile? ๐ค
A Dockerfile is a text document containing all the commands a user might execute on the command line to assemble an image. Docker can automatically build images by reading instructions from a Docker file. To write a Dockerfile, we need to follow a specific pattern, i.e., INSTRUCTION argument
. Docker then uses these instructions to assemble the image layer by layer.
Here are some of the basic instructions that we will be using in our Dockerfile:
FROM
: a docker file must start with a FROM instruction because for an image, firstly we will need a base image. A base image is a starting point or an initial step for the image that we finally want to create.WORKDIR
: This instruction specifies the folder within the container where the application code will be stored.COPY
: Use this instruction to copy the application code into the container: Utilize theCOPY
instruction to transfer the application code into the container, ensuring it is stored in the specifiedWORKDIR
.RUN
: Any commands that you want to execute during the image creation process should be specified using this instruction. Typically, RUN is used to install packages required by our application.CMD
: To execute a command when the container starts, use the CMD instruction. This will enable us to run our application as soon as the container begins operation.
First, we run the project without Docker.
Running the project locally allows us to better understand the project's requirements, which will be beneficial when writing the Dockerfile for creating the Docker image.
If you wish to follow along, here are some prerequisites:
Spin up an ec2 instance.
Install Docker on the EC2 instance.
Allow users to run Docker without sudo by using this command:
sudo usermod -aG docker user_name
then reboot the instance by executingsudo reboot
.Clone this GitHub repository, which contains the source code application that we will be using in our project, and review the project documentation.
Install pip by executing
sudo apt install python3-pip
Install Django by executing
pip install Django
After running the project, I realized that some changes need to be made for you to access the running application on the EC2 IP.
Make the following changes in the django-todo/todoApp/settings.py
file set the ALLOWED_HOSTS
setting to * , like this > ALLOWED_HOSTS=['*']
Now, execute these commands to initialize the database, seed it with some data, and subsequently launch the server.
python3 manage.py makemigrations
python3 manage.py migrate
# this will run the application and serve it on port 8000
python3 manage.py runserver 0.0.0.0:8000
After the server runs, you can see the following output, we can see that it is accepting requests and serving appropriate responses:
Access the application by using the ec2 ip on your browser ec2_public_ip:8000
, you will see the following webpage.
Now let's containerize this application:
To run this application, we need to create a Dockerfile containing the necessary steps, which will allow Docker to generate an image from it.
Here is the Dockerfile for creating an image for this application:
FROM python:3.8-slim-buster
WORKDIR /app
COPY . .
# run commands while the image is being created
RUN pip install --upgrade pip
RUN pip install Django
RUN python3 manage.py makemigrations
RUN python3 manage.py migrate
# This command will run when the container starts
CMD ["python3","manage.py","runserver","0.0.0.0:8000"]
Build Docker image: To build the docker image run the following command
docker build -t todo-app /path/to/Dockerfile
.After running this command you will see the following output:Here we can see the steps being added as a layer in the image
Run the container:
To run the container just executedocker run -d -p 8000:8000 --name todo-app todo-app
. The "-d" option stands for "detached." This will run the container in the background and display the container ID, preventing logs from appearing on the screen.Let's peek inside the container: to see inside the container execute this command
docker logs -f todo-app
.
We can also access the application on the browser by using the ec2 public ip: