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:

  • How to deploy application on JoNaS in a Docker container
  • echo environment variable with docker run
  • Tutum HAProxy Docker Virtual Host forward to entry point path
  • integrating django and sentry inside docker
  • How do I use map[string]struct{}
  • docker-machine create node without tls verification
  • version: '2'
    services:
      api:
        build: .
        ports:
          - '8245:8245'
        volumes:
          - .:/home/app/api
          - /home/app/api/node_modules
          - /home/app/api/public/src/bower_components
        links:
          - db
      db:
        build: ./database
        env_file: .env
        ports:
          - '8246:5432'
        volumes_from:
          - dbdata
    
      dbdata:
        image: "postgres:9.5.2"
        volumes:
          - /var/lib/postgresql/data
    

    Help?

  • Docker-Tuleap : tuleap Could not dump ssh keys for user admin
  • Can't get an interactive Docker shell inside screen
  • how to pass a --login into docker build
  • Starting mongo through docker on a Synology NAS
  • Can't connect to Express server inside Docker container created with docker-compose
  • Verify container running state [closed]
  • One Solution collect form web for “Persisting database using docker volumes”

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

    volumes:
      - /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>.

    volumes:
      - /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'
    services:
      dbdata:
        image: postgres:9.5.2
        volumes:
          - mypostgresdb:/var/lib/postgresql/data
    volumes:
      mypostgresdb:
        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.