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.)

  • Docker Swarm and private IP
  • Docker gd module for PHP 7
  • docker-compose push image to aws ecr
  • Change “hosts” / “-h” Docker for Windows in daemon.json
  • npm syscall symlink error -95 when installing node-sass on Docker for Windows
  • Install docker in vagrant
  • how to make fail2ban read json docker logs
  • Adding portMappings to Dockerrun.aws.json with Single container configuartion
  • Issue when I try to create a docker image [duplicate]
  • Docker-compose: deploying service in multiple hosts
  • “Running Kubernetes Locally via Docker” Guide is not working at all for MacOS, ssh command just hanging
  • Accessing Website running in Kubernetes Which is Running on Virtualbox from Host OS
  • One Solution collect form web for “What is the correct way to set up a Django/PostgreSQL project with Docker Compose?”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.