1

In my docker compose file, I specify two services: phpant (a custom image derived from debian with PHP compiled and postfix running so PHP can actually send emails) and a mysqldb service that pulls the latest from the docker hub.

In the phpant service, I define a volume:

volumes:
   - "/var/www/html/config/"

After installation, we write config files to this folder. We are using a volume because we want the config data to persist even if we roll out a new / updated image.

However, when we push a new image, it's losing the config files, and this directory ends up empty.

What did I do wrong? How do I get the data volume to persist even after I update the docker image for that service?

Note: The Wordpress image appears to do this correctly, which is where I got the idea in the first place. I tried to emulate what they did, but clearly, I have skipped / missed a step.

DrDamnit
  • 348
  • 4
  • 16

1 Answers1

1

You specified only a path within the container for your volume. In this configuration, Docker creates a new volume each time the container is recreated.

If you want the volume data to be persistent, you need to specify a path accessible to the container host where the volume data can be stored. For example, relative to your docker-compose.yml:

volumes:
  - ./config:/var/www/html/config

See the documentation for more ways to configure volumes.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • This maps /var/www/html/config onto a local directory in the host machine, which is fine. But I was under the impression specifying a local directory was a *bind mount*, and this was discouraged now? (First couple sentences of: https://docs.docker.com/engine/admin/volumes/volumes/). Wordpress defines a volume in their [Dockerfile](https://github.com/docker-library/wordpress/blob/549e1bd44995ba414000530749e05d8078dc9467/php5.6/apache/Dockerfile), does this make a difference? – DrDamnit Jan 25 '18 at 19:43
  • 1
    The WordPress Dockerfile also specifies an ephemeral volume. You could also specify a named volume in your `docker-compose.yml` and make it persistent that way. – Michael Hampton Jan 25 '18 at 19:46