0

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.

CK LZEM7
  • 3
  • 3

1 Answers1

0

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.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • Thanks @Michael Hampton. Under this situation can't I start the container with the data in the anonymous bind? If I can't persist data with them could you please explain the usage of them because that would be highly helpful to my project. – CK LZEM7 Jan 29 '19 at 19:25
  • You must choose somewhere to save the data, if you want it saved. – Michael Hampton Jan 29 '19 at 19:26
  • But according to the docker documentation in this link https://docs.docker.com/storage/ it is said that "When you mount a volume, it may be named or anonymous. Anonymous volumes are not given an explicit name when they are first mounted into a container, so Docker gives them a random name that is guaranteed to be unique within a given Docker host. Besides the name, named and anonymous volumes behave in the same ways. ". So dont they have the same functionality?? – CK LZEM7 Jan 29 '19 at 19:45
  • You still haven't provided a path on the host to save the data. – Michael Hampton Jan 30 '19 at 00:03