The Dockerfile

Explore the mechanics of our Dockerfile. This is the most important file in the tarball and is needed to set up our Django environment on the Educative platform.





Work directory preliminary

In Getting Started, we discussed the contents of our Django tarball, which includes everything we need to create a Django container on Educative. This consists of the Dockerfile with instructions for building your Django Docker image. Once we upload the Dockerfile, which we will do in a later lesson, it will place all our files into a single work directory.

We will be working in the following directory on the Educative platform: /usr/local/educative.

The Django Dockerfile

Have a look at the snapshot below. This is our Dockerfile. Docker will use the Dockerfile to build our Django container. Let’s go step by step to learn how Docker will use each line:

# Dockerfile
# FROM directive instructing base image to build upon
FROM ubuntu:latest
# COPY startup script into known file location in container
COPY start.sh /usr/local/educative/start.sh
# COPY Django Project folder and requirements.txt into our working directory
COPY helloworld /usr/local/educative/helloworld
COPY requirements.txt /usr/local/educative/
# install python 3 and pip
RUN apt-get update
RUN apt-get install python3 -y
RUN apt install python3-pip -y && pip3 install --upgrade pip
# make python 3 default python environment
RUN alias python=python3
# install Django and Gunicorn dependencies from our requirements file
RUN cd /usr/local/educative && pip install -r requirements.txt
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifies the command to execute to start the server running.
CMD ["/start.sh"]
# done! :)

Breaking down the Dockerfile line by line

Press + to interact
# Dockerfile
# FROM directive instructing base image to build upon
FROM ubuntu:latest

First, we use the FROM command to pull the base image ubuntu:latest, as shown in Line 4 above.

Note: The base image can be used to configure other environments for your projects on Educative, as needed. In this example, we’re configuring a Django environment, but other courses may require different environments too!


Second, we use the COPY command to copy necessary files for our docker project:

  • the start.sh script file,
  • the helloworld project folder, and
  • the requirements.txt file.

These are copied into our docker container in the directory, /usr/local/educative/.

Note: In order to preserve the directory structure as in our tarball, the helloworld folder is placed in the directory /usr/local/educative/hellowworld.

Hence, all our source codes will be located in the relevant directories specified in our Dockerfile.

Press + to interact
# COPY startup script into known file location in container
COPY start.sh /usr/local/educative/start.sh
# COPY Django Project folder and requirements.txt into our working directory
COPY helloworld /usr/local/educative/helloworld
COPY requirements.txt /usr/local/educative/

Third, we use the RUN command to install python3, the latest version of Python, and the version supported by the Django framework. We also install the latest version of pip, a package management system for Python software.

Note: The latest official version of Django does not support Python 2.7. You can read more on the Django website here.

Press + to interact
# install python 3 and pip
RUN apt-get update
RUN apt-get install python3 -y
RUN apt install python3-pip -y && pip3 install --upgrade pip

Fourth, we use the RUN command to set Python 3 as our default Python environment. This is so that whenever you use the python program command during your course, the system will know to run Python 3 instead of the usual Python 2.

Press + to interact
# make python 3 default python environment
RUN alias python=python3

Fifth, we use the RUN command to install Django and dependencies of our Django project (i.e., the Gunicorn Server) listed in the requirements.txt file by using the following command:

RUN cd /usr/local/educative && pip install -r requirements.txt

Note: We will discuss the Gunicorn Server in a later lesson.

Press + to interact
# install Django and Gunicorn dependencies from our requirements file
RUN cd /usr/local/educative && pip install -r requirements.txt

At this stage, we have set up our Docker container with the source code and all the necessary installations.

Sixth, we use the EXPOSE command to expose the port 8000 from our Docker container to the outside environment to allow communication to and from the server.

Press + to interact
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000

Finally, we use the CMD command. This keyword is used to execute commands when we build a new container from the docker image, as we are doing with this Dockerfile. In our case, we will want CMD to run the start.sh script (to start the Gunicorn Server when our LiveVM is run during Django lessons in your course) whenever the Django container runs.

Note: For more on the topic of LiveVMs, read Running a Custom Docker and using a Live VM in our Author’s Guide.

Press + to interact
# CMD specifies the command to execute to start the server running.
CMD ["/start.sh"]

Tada! 🎉

In the above walkthrough, we’ve covered what our Dockerfile is doing. By now, you’ve learned the basic mechanics of how the most important file in the Django tarball works. That’s great progress!


In the next lesson, we’ll cover the workings of the requirements.txt file and the helloworld project folder.

Stay tuned! :)