Deploy Django with Docker and PostgreSQL


I have taken a project to Deploy Django and PostgreSQL on a couple of machines and I want to automate it with Docker,

I’ve found the following which lead me to

However I’m having trouble adding all up to work properly

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
March 31, 2023
Accepted Answer

Hey there,

there are a couple of ways to go about what you want. In the link you provided, it’s stated you can create a Docker file and then add a configuration file which configures the services for you.

What I would do is to create the following DockerFile :

# Use an official Python runtime as a parent image
FROM python:3.9-slim-buster

# Set the working directory to /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Set environment variables
ENV POSTGRES_USER=<your-postgres-username>
ENV POSTGRES_PASSWORD=<your-postgres-password>
ENV POSTGRES_DB=<your-postgres-db-name>
ENV DJANGO_SETTINGS_MODULE=<your-django-settings-module>

# Install PostgreSQL
RUN apt-get update && apt-get install -y postgresql postgresql-contrib

# Create the database
RUN service postgresql start && \
    su postgres -c "psql -c \"CREATE DATABASE ${POSTGRES_DB};\"" && \
    service postgresql stop

# Copy the Django project into the container
COPY . .

# Run Django migrations to create the necessary tables in the database
RUN python migrate

# Expose the default Django port

# Start the Django development server
CMD ["python", "", "runserver", ""]

In this Dockerfile, we start by using an official Python 3.9 slim image as the parent image. We then set the working directory to /app and copy the requirements.txt file into the container. The parent image is modified by adding a new code directory. The parent image is further modified by installing the Python requirements defined in the requirements.txt file.

Next, we set environment variables for the PostgreSQL username, password, database name, and Django settings module. We install PostgreSQL and create the database using the service command and the su command to run the PostgreSQL psql command as the postgres user.

We then copy the entire Django project into the container and run the migrate command to create the necessary tables in the database. We expose the default Django port 8000 and start the Django development server using the CMD instruction.

Note that you will need to modify this Dockerfile to match your specific Django and PostgreSQL configuration. For example, you will need to update the environment variables to match your PostgreSQL username, password, and database name, and update the CMD instruction to match the command used to start your Django server.

Having said that, if you plan to deploy these projects on DigitalOcean, I can suggest you to check the[Managed Databased DigitalOcean] offers.

Basically, it manages the Database for you so that you don’t have to worry about anything.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel