Okay, we’ll be required to follow these simple steps:
- Create a Dockerfile
- Install python dependencies (we can use requirements.txt)
- Create a docker-compose.yml file
- Modify python settings.py file to add PostgreSQL DB Config.
Let’s start doing then …
Create a Dockerfile
In your Django project’s root directory, create a file with the name Dockerfile and add the following content into that
FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /code/
In this Dockerfile, we are starting with a Python 3 parent image(You can modify according to your project requirements). Then we’re adding a new code directory where your project code will be copied and used by the container. We are modifying the parent image further by installing the Python dependencies defined in the requirements.txt file.
Add required libraries in requirements.txt
Other than the dependencies you already have in your existing requirements.txt file, you must have these two libraries in it:
...
Django>=2.0,<3.0
psycopg2-binary>=2.8
...
Docker Compose Configurations
In your Django project’s root directory, create a file with the name docker-compose.yml and add the following configuration into that
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- postgres
postgres:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
This configuration defines two services: The web service of your app and the postgres service of your database. You can modify the configuration fields anytime, here’s a manual for working with docker-compose file.
Add DB Settings in Django settings.py File
Edit the database configurations in your project’s settings.py file by replacing the DATABASES = ... with the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'postgres',
'PORT': '5432',
}
}
We have made all the required changes. Now it’s time to test your application.
Run the project using docker-compose Command
Open the terminal, make sure you are in the root directory of your project.
Run the command …
sudo docker-compose up --build
You’ll be seeing lots of build logs and at the end something like …
...
...
web_1 | Django version 2.2.5, using settings 'mydemoapp.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
That means, your Django app is running at the port 8000 on your Docker host. Go to http://localhost:8000 on a web browser to see the Django app home page.
Hurray …! You just finished the setup of a Django app that uses Postgresql DB using a Docker Host.
Top comments (3)
Goto project location and You can use the following code to generate a requirements.txt file automatically:
$ pip freeze > requirements.txt
for python3
$ pip3 freeze > requirements.txt
Little bit confused on
requirements.txt
file. Are we supposed to create this file with following modules?Finally figure it out,
so I have to create requirements.txt file using following content
But as soon as I login I am seeing following error message.