How to Set Up Django with Docker and PostgreSQL: An Advanced Guide
By khoanc, at: July 28, 2024, noon
How to Set Up Django with Docker and PostgreSQL: An Advanced Guide
In our previous blog post, Creating a Minimal Dockerfile for Your Django App, we explored how to containerize a Django application using Docker.
While that guide focused on a simple development setup, this article takes it a step further by introducing PostgreSQL into the mix, creating a robust and scalable environment for your Django app.
This guide is perfect for developers looking to:
- Integrate PostgreSQL as the database for their Django app.
- Manage services efficiently using Docker Compose.
- Transition from a basic Docker setup to a more complete development environment.
Step 1: Setting Up Your Project Directory
Organize your project files for clarity and scalability. The structure should look like this:
django-docker-postgres/
├── django_project/
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
Step 2: Writing the Dockerfile
The Dockerfile
defines the environment for your Django application. Here’s a minimal setup:
# Use a lightweight Python base image
FROM python:3.10-slim
# Set the working directory in the container
WORKDIR /app
# Copy and install dependencies
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code into the container
COPY . /app/
# Expose port 8000 for the Django development server
EXPOSE 8000
# Command to run the Django app
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Step 3: Configuring Docker Compose
Create a docker-compose.yml
file to define and link the services (Django and PostgreSQL):
version: '3.8'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_USER: glinteco
POSTGRES_PASSWORD: glinteco
POSTGRES_DB: glinteco
Step 4: Updating Django Settings
Configure your Django app to connect to PostgreSQL. Update the DATABASES
section in settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'glinteco',
'USER': 'glinteco',
'PASSWORD': 'glinteco',
'HOST': 'db',
'PORT': 5432,
}
}
The HOST
field should match the name of the db
service in docker-compose.yml
.
Step 5: Building and Running the Containers
Build the Containers
Run the following command to build the Docker images:
docker-compose build
Start the Services
Launch the containers:
docker-compose up
This starts both the Django and PostgreSQL containers. Access your app at http://localhost:8000
.
Step 6: Running Database Migrations
Initialize the database by running Django migrations:
docker-compose run web python manage.py migrate
This sets up your database tables in the PostgreSQL container.
Step 7: Testing the Setup
Navigate to http://localhost:8000
in your browser. If everything is configured correctly, your Django app should be running and connected to the PostgreSQL database.
Enhancements for Production
For a production-ready setup:
- Replace the development server with
gunicorn
.
- Use
nginx
as a reverse proxy.
- Manage environment variables securely using a
.env
file.
- Leverage Docker networking and volumes for improved scalability.
- Use some libraries that support more features - you can explore more here
Conclusion
By following this guide, you’ve created a fully containerized Django app with PostgreSQL. Docker and Docker Compose make it easy to manage both your application and its dependencies, providing a consistent and portable development environment. With a little more effort, you can scale this setup for production use.
Explore further: