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: Setting up a GUI in a Docker Container on a Raspberry Pi
  • Multiple threads inside docker container
  • Can we run multi-process program in docker?
  • Clashing between two postgres database though having two different docker-compose yaml files for django projects
  • Installing maven in a docker build overrides JAVA 8 with JAVA 7(!)
  • What is exactly happening when you're Docker Commiting a container?
  • Run webpack build during docker-compose build process
  • Use a configuration file from the host when building a Docker image
  • automation script for postgresql
  • Docker postgresql container with external data container
  • Setting Dynamic Env variable in Docker
  • Error accessing cgi script inside Docker container, Operation not permitted: Couldn't bind unix domain socket
  • 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
      image: postgres
      build: .
      command: python manage.py runserver
        - .:/code
        - "8000:8000"
        - 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.