By Brendo Ross
Hello,
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 https://docs.docker.com/samples/django/ which lead me to
https://github.com/docker/awesome-compose/tree/master/official-documentation-samples/django/
However I’m having trouble adding all up to work properly
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!
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
WORKDIR /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 manage.py migrate
# Expose the default Django port
EXPOSE 8000
# Start the Django development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
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 https://www.digitalocean.com/products/managed-databases-postgresql)[Managed Databased DigitalOcean] offers.
https://docs.digitalocean.com/products/databases/postgresql/
Basically, it manages the Database for you so that you don’t have to worry about anything.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.