Skip to content

Instantly share code, notes, and snippets.

@Shemeikka
Created January 25, 2017 17:21
Show Gist options
  • Save Shemeikka/7d6f37bc704b04cbbc5c6e33e39c748b to your computer and use it in GitHub Desktop.
Save Shemeikka/7d6f37bc704b04cbbc5c6e33e39c748b to your computer and use it in GitHub Desktop.
Description of how to use data volumes and stuff..
# https://github.com/docker/docker/issues/30441#issuecomment-275124802
You can still use the same volume for two services. If I understand correctly, you have a separate image holding the data, purely for propagating the volume? Say that that image is named mydataimage, you can do something like this;
version: "3.0"
services:
init:
image: mydataimage
volumes:
- uploads-data:/usr/share/nginx/html/uploads/
web:
image: nginx
volumes:
- uploads-data:/usr/share/nginx/html/uploads/
volumes:
uploads-data:
Be aware though, that there will be a "race" condition; if the web service is started before the init service, then the volume is propagated by the content of the web service's image, not the init service's image.
So, you may want to do docker-compose up init manually to start the init service first (and propagate the volume), then start the other services. Alternatively (since propagating the volume is a one-time task), you can manually declare the volume "external" (i.e., the volume is not created by docker-compose, but created up-front), and run the image manually to propagate the volume;
version: "3.0"
services:
web:
image: nginx
volumes:
- uploads-data:/usr/share/nginx/html/uploads/
volumes:
uploads-data:
external:
name: my-volume
Then;
# create the volume
docker volume create my-volume
# propagate the volume with data from the `/data` directory in the mydata image
# afterwards, the container is no longer needed, so the `--rm` flag
# removes the container after it exits (and has propagated the volume)
docker run -it --rm -v my-volume:/data mydata
# once done, start the docker-compose stack
docker-compose up -d
Is there a reason you're using a separate image to propagate the volume, and not putting that in the services' image itself?
Generally; volumes are used for runtime data (e.g. database data, uploads, sessions, etc), but the source code itself of your service should not be in a volume. That way you can update the source code by deploying a new version of the image (myapp:v1.0 -> myapp:v1.1), which will update the version of your application, but preserve the "runtime" data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment