Build a Dockerfile#
In this lab you will begin working on the Dockerfile
that will be the heart of your project. The Dockerfile packages your application to be run as a container. The instructions will start you off with a Dockerfile
that works for your project if you completed the A Django Application milestone.
Starter Dockerfile
#
In the root directory in your repo create a new Dockerfile
. The initial contents of your Dockerfile
should be:
FROM docker.io/python:3.10
RUN pip install django
COPY mysite /mysite
WORKDIR /mysite
CMD python ./manage.py runserver 0.0.0.0:8000
The initial Dockerfile
expects that the mysite
directory exists in the root of your repo. Note that it runs the pip install django
command that we ran before the Build a Django Application lab. If that command is not run Django won’t be installed in the container.
Keep Up With Changes
In future labs you will use the pip
and apt
commands to install new software. It’s your responsibility to make sure those commands are also run in your Dockerfile
so that your container is updated.
Build and run your container using the instructions in the Build a Docker Container lab. Make sure you start the container with a port definition like this:
$ docker run -it --rm -p 8000:8000 mysite
Debugging a Container That Won’t Start#
Sometimes a container builds but won’t start. Problems like this arise when there are problems with the WORKDIR
or CMD
lines in the Dockerfile
or when a COPY
moved files to the wrong place. When a container won’t start it can be hard to figure out what went wrong. When you run a container you can override the CMD
in the Dockerfile to get a shell. The shell lets you look around and try to see what’s wrong.
$ docker run -it --rm -p 8080:8000 mysite /bin/bash
With bash running you can look around the container with cd
and ls
and also try to start your application manually:
root@9e30cbcaa3af:/mysite# python3 ./manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 15, 2023 - 01:14:12
Django version 4.1.7, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.