This is a sample TOM to show how it can be used with Docker.
The TOM was created by following the getting started guide with the credentials:
- User:
tom-user
- Password:
password
and the modifications laid out below.
Dockerizing existing projects will require some project specific modifications that differ slightly from this example.
All of the modifications listed below have already been made to this repository except Create database directory.
Since Docker containers are ephemeral and the TOM has a database that should be persisted, we need to put our database in a volume.
First, we'll create a directory in our project that we can use as a volume in our container:
mkdir storage
Now we need to tell the TOM where to find the database by updating an element in
the DATABASES
section of
settings.py:
'NAME': os.path.join(BASE_DIR, 'storage', 'db.sqlite3'),
Now that we've updated the location of where the TOM will look for the database,
let's migrate the databases in the storage
directory:
./manage migrate
In order to allow others to access our TOM, we need to serve it with a web server. Gunicorn is a popular Python web server that we'll use in this example.
pip install gunicorn gevent
Although we have setup a web server for our TOM, it's not able to server static files to users (e.g. images). We can serve static files using WhiteNoise.
Note that other applications are better suited to serve static files such as Nginx when a TOM is deployed using system packages (e.g. DNF, apt-get) or container orchestration (e.g. Kubernetes).
pip install whitenoise
To build a Docker image for our TOM, we need to give it a name and a unique tag.
Do not use the default latest
tag for production deployments. The latest
tag is error prone and makes debugging deployments very, very difficult.
"Latest is a proliferating ball of confusion that should be avoided like a ravine
full of venomous snakes". The command
below uses the git
tag of the repo as the tag of the Docker image:
TAG="$(git --describe --always)"
docker build --tag "dockertom:${TAG}"
Now run the created image:
docker run --interactive --tty --rm --name dockertom --publish 8080:8080 --volume "${PWD}/storage:/tom/storage "dockertom:${TAG}"
Access your TOM by going to http://localhost:8080!
Typing the commands above to build and run your Docker image can become very repetitive and is not reproducible. To make development easier and production more reliable, the commands should be automated. This has been done using a Makefile in this project which requires Make to be installed.
Everything needed to build and run a Docker image can be done by simply running:
make
in the root of this project.