Production Database#

The container from the last milestone contains an integrated database, which is handy for testing but not sufficient for production. In production your container should use an external database. That gives the designer of the infrastructure the flexibility to choose the database solution that’s the right size and cost. In this milestone you must implement the ability for your container to connect to an external database.

Pluggable Database Parameters#

When you start the Postgres container you have to give it account information in environment variables. That’s a typical way to ship a production container where the container user specifies the secrets. In order for your Django application to be production ready you have to strip all of the secrets out of settings.py using environment variables. Using what you learned in the Put Secrets in Environment Variables lab update the Postgres connection information. You container should use the following environment variables:

  1. DB_NAME - The name of the database to use.

  2. DB_USER - The user account that accesses the database

  3. DB_PASSWORD - The user’s password

  4. DB_HOST - The hostname of the database server

The variables should correspond to the similarly named configuration variables in settings.py.

Updated Dockerfile#

Remember you had to install support for Postgres into your dev box before you were able to use Postgres. The same is true of your container. The Dockerfile for your application needs to be updated to run the necessary installation steps. When you’re working on your updated Dockerfile and source code the best practice is to build and test containers on your dev box. After you have it working there, commit the changes to GitHub and have GitHub build the production container for you. You should have memorized some of the commands in the Build a Docker Container and the debugging techniques in the Build a Dockerfile and Debugging a Running Container labs.

Release#

Submissions for this milestone should have miletone-5.x in the release name.

Test Procedure#

Starting with this milestone your application now supports a pluggable database. I will configure my own Postgres container and verify that your Django container uses it properly. I will start your container with a command similar to this:

$ docker pull ghcr.io/your-name/your-repo:milestone-5.x
$ docker run -it --rm -p 8000:8000 \
    --network mysite --name django \
    -e SECRET_KEY=mysecretkey -e PORT=8000 \
    -e DB_NAME=testdbname -e DB_USER=testdbuser \
    -e DB_PASSWORD=securepassword -e DB_HOST=postgres \
    ghcr.io/your-name/your-repo:milestone-5.x

Requirements#

  1. Remove the SQLite database from your container.

    1. It should be added to your .dockerignore

    2. It should be remove from GitHub and added to your .gitignore

  2. If the postgres DMBS is selected it should be configured with the following environment variables:

    1. DB_NAME - The name of the database to use.

    2. DB_USER - The user account that accesses the database

    3. DB_PASSWORD - The user’s password

    4. DB_HOST - The hostname of the database server

Turn In#

  1. Tag this milestone milestone-5.x.

  2. A docker pull command that gets your built container from GitHub’s repo. You can find this by clicking your built container under “Packages” in GitHub. The command should look like this:

$ docker pull ghcr.io/your-user-name/your-repo-name:milestone-5.x