Persisting database using docker volumes

I’m trying to persist postgres data in a docker container so that as soon as you docker-compose down and docker-compose up -d you don’t lose data from your previous session. I haven’t been able to make it do much of anything – pulling the container down and back up again routinely deletes the data.

Here’s my current docker-compose.yml:

  • Running a docker image in Openshift Origin
  • Start Docker Containers on logon under Windows
  • Docker Centos 7 easy_install supervisor ssl issue behind proxy
  • Managing Docker containers from Ansible plays
  • alpine linux: pmap output not showing RSS
  • Debian Packagemanager won't install vagrant.deb package on alpine-linux, while building docker image
  • version: '2'
        build: .
          - '8245:8245'
          - .:/home/app/api
          - /home/app/api/node_modules
          - /home/app/api/public/src/bower_components
          - db
        build: ./database
        env_file: .env
          - '8246:5432'
          - dbdata
        image: "postgres:9.5.2"
          - /var/lib/postgresql/data


  • meet Call to undefined function Xinax\LaravelGettext\bindtextdomain() error in LaravelProject
  • Django, Postgres, Docker - Can not run migrations on database on ubuntu 14.04 python 2.7.11
  • Powershell remote access to nanoserver on docker
  • boot2docker access elasticsearch on localhost
  • nginx as a docker upstream proxy with maps
  • How to setup Node environment variable in Dockerfile for running node.js application?
  • One Solution collect form web for “Persisting database using docker volumes”

    According to the Dockment of Docker Compose, when you write something like:

      - /var/lib/postgresql/data

    It creates a new docker volume and map it to /var/lib/postgresql/data inside the container.
    Therefore, each time you run docker-compose up and docker-compose down, it creates new volume. You can confirm the behavior with docker volume ls.

    To avoid it, you have two options:

    (A) Map host directory into container

    You can map directory of host into container using <HOST_PATH>:<CONTAINER_PATH>.

      - /path/to/your/host/directory:/var/lib/postgresql/data

    The data of postgresql will be saved into /path/to/your/host/directory of the container host.

    (B) Use external container

    docker-compose has an option of external container.
    When it is set to true, it won’t always create volume.
    Here’s an example.

    version: '2'
        image: postgres:9.5.2
          - mypostgresdb:/var/lib/postgresql/data
        external: true

    With external: true, docker-compose won’t create the mypostgredb volume, so you have to create it by your own using following command:

    docker volume create --name=mypostgredb

    The data of postgresql will be saved into docker volume named mypostgredb. Read reference for more detail.

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