Setup a Docker and Django Postgres Environment (Part 1)
Introduction
This is part one in a tutorial series explaining how to use Docker and Django Postgres in a web application. Django is an open-source, full-stack web framework written in Python programming language. It is designed to promote clean and practical design, fast development and provides support for several popular databases, including PostgreSQL. Docker uses OS-level virtualization to render software in packages referred to as containers. While the containers are separated from each other, they can communicate with each other and bundle their own libraries and configuration files. This tutorial will explain how to use Docker to setup a Django and PostgreSQL container for a web app.
Prerequisites
The Docker engine must be installed and functioning in order to use Docker and Django Postgres in a web application. Confirm the Docker engine version be executing the
docker –version
command in a terminal or command prompt window.A familiarity with Linux and UNIX command lines.
An understanding of the Docker-Compose YAML files syntax as well as Dockerfile commands.
As this tutorial series will use a
docker-compose.yml
file to build and run the Django and Postgres container, the separate package for Docker Compose must be installed. Execute thedocker-compose --version
command to confirm the package for Docker Compose is installed. Refer to the following screenshot:
A working installation of Python. Ideally, Python version 3.6 or newer should be used as Python 2.7 is now deprecated and losing long-term support,
The
virtualenv
library must be installed on the local machine building the Docker containers. Confirm installation with thewhich virtualenv
command. More information on how to use the respective commands can be obtained with thedocker --help
anddocker-compose –help
commands.
NOTE: Python 3 now has built-in support for virtual environments, and is recommend as Python 3.6 pyvenv
has been deprecated in favor of venv
.
Install Docker
Execute the following instructions if the Docker engine is not yet running.
Docker can be installed in Linux with just a few terminal commands. However, if using Windows or macOS, a login to Docker must be performed using a Docker ID, or by creating an account, before the installer can be download.
It should be noted that Docker Compose is included in the interactive installer packages for macOS and Windows. However, Docker Compose will require a separate installation on Linux.
Install Docker on macOS
On macOS, download a Docker.dmg
interactive installer and then double click it to begin the installation, as shown on the below screenshot. Once it is installed and running, the program should respond to the docker
commands in a terminal window.
NOTE: The
Terminal.app
for macOS should be located in the Utilities
sub-directory of Applications
in a finder window.
Install Docker on Windows
For Windows applications, Docker can be downloaded here with an interactive installer as an executable (.exe
) file. However, be aware Docker may not function on older unsupported versions of Windows, such as Windows 7 or XP.
Install Docker on Linux
As Docker was originally designed for Linux, the Docker Engine operates very well in Linux environments. However, the latest release of Docker Composer can be downloaded here. Once downloaded, just rename it and move the docker-compose-Linux-x86_64
archive to the /usr/local/bin/
directory with the following command:
1 2 | cd ~/Downloads mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose |
Now execute the following chmod +x
command to make the file executable:
1 | sudo chmod +x /usr/local/bin/docker-compose |
Install Docker on CentOS Linux
On a “Fedora” flavor of Linux that uses the YUM repository, such as CentOS or Red Hat, execute the following yum install
command to install the Docker CE Engine:
1 | sudo yum install docker-ce |
Lastly, execute the following command to install the Docker package:
1 | sudo yum install docker |
Install Docker on Ubuntu
In Linux, use the cURL library to download the GPG key. The following example uses curl
to download the Docker install GPG key for the Ubuntu distros of Linux:
1 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
Now the package must be added to the APT-GET repository list. The following command utilizes the lsb_release -cs
command as a wildcard to install the appropriate package for the Ubuntu’s release name, such as bionic
or xenial
:
1 | sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable edge |
NOTE: The above command may not work on a machine or Linux OS that is not AMD64 compatible.
Now execute the following command to update the local repository in order to install Docker:
1 | sudo apt-get update |
Install Docker on Ubuntu 19
As of Jan 2020 the default Docker library is no longer compatible with Ubuntu 19. Use curl
or wget
to download the appropriate DEB packages for Ubuntu 19 and then install them using the dpkg -i
command.
Download the Ubuntu 19 DEB packages for Docker
Execute the following wget
commands to download the .deb
archives for containerd.io
, docker-ce-cli
, and docker-ce
:
1 2 3 | wget "https://download.docker.com/linux/ubuntu/dists/disco/pool/stable/amd64/containerd.io_1.2.6-3_amd64.deb" wget "https://download.docker.com/linux/ubuntu/dists/disco/pool/stable/amd64/docker-ce-cli_19.03.3~3-0~ubuntu-disco_amd64.deb" wget "https://download.docker.com/linux/ubuntu/dists/disco/pool/stable/amd64/docker-ce_19.03.3~3-0~ubuntu-disco_amd64.deb" |
Install the Docker packages with ‘dpkg’
Execute the dpkg -i
command to install the .deb
packages after the downloads are complete. Execute the following command to install the containerd.io
container runtime library:
1 | sudo dpkg -i containerd.io_1.2.6-3_amd64.deb |
Now execute the following command to install the docker-ce-cli
CLI package before installing Docker CE in order to avoid any dependency errors:
1 2 | sudo dpkg -i docker-ce-cli_19.03.3~3-0~ubuntu-disco_amd64.deb sudo dpkg -i docker-ce_19.03.3~3-0~ubuntu-disco_amd64.deb |
NOTE: All of the packages must be installed in the specified order to avoid any dependency errors.
Create a Docker project
First, create a project folder for the Docker files and the Django project. Use the following mkdir
command to create a new project directory:
1 | mkdir django-postgres && cd django-postgres |
Now enter the project with the cd
command.
Next, create a Dockerfile
and docker-compose.yml
file for the Django container. A simple way to do this is with the following touch
command:
1 | touch Dockerfile && touch docker-compose.yml |
Make certain to save these files for building the Docker container later.
Create a Django project
A Django project sub-directory must be created while inside of a Python virtualenv (venv
). First, execute the following command to create a virtual environment directory to store the files for the Django project:
1 | python3 -m venv django-app |
The directory should be created with the bin/
, include/
, and lib/
Python directories for the virtual environment.
Now change into the project directory using the cd
command as follows:
1 | cd django-app |
Activate venv for the Django app
Execute the following source
command to activate the virtual environment:
1 | source bin/activate |
The (django-app)
should now be visible in the terminal prompt indicating that the entrance to the virtual environment has been established.
If using the pip3 list
command, notice that only a few bare bone Python applications are installed. Use the following pip3 install
command to install Django and the psycopg2 adapter for Postgres:
1 2 | pip3 install Django pip3 install psycopg2 |
The following django-admin
command, used for managing and deploying Django projects, should now be working:
1 | django-admin --version |
Use the django-admin startproject
command to start a new Django project.
1 | django-admin startproject postgres_docker. |
Supply the new project with a name and specify the path for the project using $PWD
or .
to create the path in the current directory. Note the Django project names don’t allow spaces or hyphens—but only allow underscores (_
)
and lowercase letters.
It make take a few seconds to update, but the name of the project as a new directory should be visible.
Now execute the following command to change into the new directory to modify the settings:
1 | cd postgres_docker |
This should bring up a screen that resemble the following image:
Conclusion
This was part one of a series explaining how to setup a Docker, Django and Postgres development environment with Docker and Django Postgres. Part one of this series specifically covered how to install Docker on Mac, Linux, Windows and Ubuntu, how to download the Ubuntu 19 DEB packages for Docker and how to install the Docker packages with ‘dpkg’. Part one also explained how to create both a Docker and a Django project. Remember that all of the packages must be installed in the specified order to avoid any dependency errors. Part two of this series will explain how to edit the settings.py
file so the Django application will use Postgres instead of a MySQL database. A Docker container will also be ran that includes the Django project. Be sure all of the prerequisites are met before proceeding with part two of this tutorial series.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started