lumenwrites
lumenwrites

Reputation: 1507

What is the correct way to set up a Django/PostgreSQL project with Docker Compose?

Can you tell me what is the best way to way to set up and configure a Django/PostgreSQL project with Docker Compose?

(with the latest versions of everything, python 3.4, django 1.8.1, etc.)

Upvotes: 1

Views: 175

Answers (1)

linkyndy
linkyndy

Reputation: 17928

Have you looked at the examples on the Docker website? Here's a link describing exactly this.

Basically, you need two services, one for your Django app and one for your Postgres instance. You would probably want to build a Docker image for your Django app from you current folder, hence you'll need to define a Dockerfile:

# Dockerfile
FROM python:3.4-onbuild

That's the whole Dockerfile! Using the magic -onbuild image, files are automatically copied to the container and the requirements are installed with pip. For more info, read here.

Then, you simply need to define your docker-compose.yml file:

# docker-compose.yml
db:
  image: postgres
web:
  build: .
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

Here, you've defined the Postgres service, which is built from the latest postgres image. Then, you've defined your Django app's service, built it from the current directory, exposed port 8000 so that you can access it from outside your container, linked to the database container (so that they can magically communicate without anything specific from your side - more details here) and started the container with the classic command you use to normally start your Django app. Also, a volume is defined in order to sync the code you're writing with the one from inside your container (so that you don't need to rebuild your image every time you change the code).

Related to having the latest Django version, you just have to specify it in your requirements.txt file.

Upvotes: 3

Related Questions