How to run a python django app in docker

Django, described as the The web framework for perfectionists with deadlines is a high-level Python Web framework that encourages rapid development and clean design.
In this post I will show you how to get Django up and running in a docker container.
Steps
- The first thing you will need to do is install docker compose. Compose is a tool that allows you to run multi container applications.
I will be installing compose on an Ubuntu 18.04 system. To install compose on another OS check out the compose documentation here.To run Compose you need to have docker installed. If you don't already have docker installed run the following commandssudo apt-get remove docker docker-engine docker.io containerd run
sudo apt-get update
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
Now check to see if docker has installed successfully by running the following command
sudo docker run hello-world
You should see the following output
- Once docker is installed you can then install docker compose.
To install compose run the following commandsudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Once the download is complete apply executable permissions to the binary
sudo chmod +x /usr/local/bin/docker-compose
Now test the installation
docker-compose --version
The docker compose version should be returned. If not check your path. You can also create a symbolic link to
/usr/bin
or any other directory in your path. - Now that docker compose is installed you can start working on getting django up and running.
To do this we will create a Dockerfile, a Python dependencies file, and a docker-compose.yml file.Start by creating an empty directory for your project.
Then create a file called Dockerfile. Read more about Dockerfiles here. - Within your Dockerfile add the following.
FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt COPY . /code/
The Dockerfile starts by pulling a python3 image
RUN mkdir creates the /code directory
COPY will copy the requirements.txt file into the /code directory in the docker container
RUN will run the pip command against your requirements.txt file
COPY . will copy the contents of your local working directory to the code directory in the container.Save your Dockerfile.
Now create a requirements.txt file and add the followingDjango>=2.0,<3.0 psycopg2>=2.7,<3.0
This file will be used to install Django and psycopg2 in the docker container using the command RUN pip install -r requirements.txt.
Save this file. - Now create a file called docker-compose.yml
This file describes the services that make your app.
Add the following to the fileversion: '3' services: db: image: postgres web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db
This file defines two services, a db service and a web service.
The db service will pull in a postgres docker image and the web service will build using the Dockerfile already in your container, start the django server on port 8000 and expose port 8000.
Save and close this file. - We will now create the django project.
Run the following command from your project directorysudo docker-compose run web django-admin startproject djangoapp .
Compose will now build the services specified in the docker-compose file, db and web.
When the web service image is built, Compose runs it and executes thedjango-admin startproject
command in the container. This command instructs Django to create a set of files and directories representing a Django project.Once Compose is finished running list the content of your project folder
You will see a new directory. This is your django project directory, created by the
django-admin startproject
command.Within this folder you will see your django related files.
You can see these file are all owned by root. Change the permissions on this directory by running
sudo chown -R $USER:$USER .
- Now that your django app is created, we need to configure it.
We will start by connecting our postgres database.Within your djangoapp directory, open the settings.py file
Go to the DATABASE section
Replace what is there with
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'HOST': 'db', 'PORT': 5432, } }
These settings relate to the db service in you docker-compose file.
Save and close your settings.py file. - Now lets try to run our Django app.
Run the following command from the root of your project directorydocker-compose up
Once the command completes your django app should be up and connected to your postgres db.
You should see something similar to the following.Browse to http://localhost:8000 and you should see the django welcome screen
Thats it, you have successfully deployed django and a postgres db in docker containers using docker compose.
To see your containers rundocker ps
You should see something like the following
To bring down your application run
docker-compose down
This will remove your containers.That's it. If you found this post helpful, please leave a comment below.