Docker compose doesn't reuse the intended anonymous host volumes on container recreation

CK LZEM7 asked:

I have an Ansible container and when I have developed a docker-compose file create the container.

version: '2'
services:
  ansible_tower:
    image: ybalt/ansible-tower:latest
    ports:
      - "333:443"
    volumes:
      - ./certs:/certs
      - /var/lib/postgresql/9.4/main
      - ./backup:/home/backup
    privileged: true 
    container_name: ansible_tower  
    restart: always

but when I remove the container and when I recreate the container all the volume data are lost. (PostgreSQL data will be lost.) After a bit of debugging I have found that the host volumes are getting remapped with the newly created volumes when the container is getting recreated. ( The unnamed host volume name changes)But this is not what expected I thought the host volumes will get reused in the event of container recreation. What have I missed in here. Due to this all the data will be lost.

My answer:


Your docker-compose.yml only specified a path to have PostgreSQL data created in a new transient volume each time the container is started. This is what happens if you specify the container path without a host path.

If you want the data to be persistent, you must specify a source for storing the data on the host: either a path, or a named volume.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.