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:
Breaking down the Dockerfile line by line
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.shscript file, - the
helloworldproject folder, and - the
requirements.txtfile.
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 folderis placed in the directory/usr/local/educative/hellowworld.
Hence, all our source codes will be located in the relevant directories specified in our Dockerfile.
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.
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.
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.
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.
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.
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! :)