New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compose v3: Moved shared volume to top-level volumes entry #709
Conversation
For compatibility for Docker Compose v3, replaced the unnamed volume in the nginx service with a named nginx_conf volume that is shared by both services.
I get this error :
|
@endersonmaia That's strange, running |
We have no real way of controlling which base image the files are copied from. So this may or may not work sometimes... UPDATE: Actually, we can use the |
volumes: | ||
- /var/run/docker.sock:/tmp/docker.sock:ro | ||
- ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl | ||
- nginx_conf:/etc/nginx/conf.d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- nginx-conf:/etc/nginx/conf.d:nocopy
This is to prevent Docker from copying the contents from the wrong container.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain what you mean by "the wrong container"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docker copies files from the image into the volume when starting the container.
If files are copied from the dockergen
container into the nginx-conf
volume, it'd become empty. This would happen when the dockergen
container is started before the nginx
container. Adding nocopy
here ensures that files are never copied from this container.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tested it, and the volume is not emptied if it has data. Considering that the /etc/nginx/conf.d
folder does not exist in jwilder/docker-gen
, and considering that docker-gen is maintaining the contents of this volume anyway, can you explain your concern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Build an
nginx
image with your own configuration files in/etc/nginx/conf.d
. - Delete the containers and the volume.
- Start the
dockergen
service. - Start the
nginx
service.
You'll see that the nginx-conf
volume was initialized to empty from the dockergen
image. Then it'll only have one default.conf
after generation from template. Your own configuration files would be lost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you make sure to start the dockergen
service before the nginx
service? Because startup order is not guaranteed, and that's why the problem could happen sometimes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was finally able to reproduce this issue, but adding nocopy
doesn't fix it. If the problem is startup order, maybe the right solution is to add depends_on
to the dockergen
service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure adding nocopy
doesn't fix it? Because it works for me. You'll need to make sure to remove the volumes before each run to be able to test it properly.
depends_on
has no effect when using docker stack deploy
, so it's not a solution. (Basically we should not care about the startup order.) See moby/moby#31333
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- If I just run
docker-compose up
, the config files are there regardless of whethernocopy
is added to thedockergen
service. This is probably just luck, given the race condition you describe, though I tried a few dozen times. - If I run
docker-compose up dockergen
thendocker-compose up nginx
, the config files are missing regardless of whethernocopy
is added to thedockergen
service.
Is there some other test I can run to verify the behavior you're describing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm starting to think this is not actually an issue. If I need to make a custom Nginx image, couldn't I just place the config file somewhere other than /etc/nginx/conf.d
? Problem solved.
Alternatively, we could use a /etc/nginx/dockergen
folder, but then you'd need a derived Nginx image.
|
||
whoami: | ||
image: jwilder/whoami | ||
environment: | ||
- VIRTUAL_HOST=whoami.local | ||
|
||
volumes: | ||
nginx_conf: {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nginx-conf
, because that's the convention used by Docker.
For example here: https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-shared-storage-volume-as-a-data-volume (my-named-volume
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that Docker Compose names everything with an underscore ($PROJECT_default
network, $PROJECT_$SERVICE_1
container, etc.), I'm not so sure about "convention." Doing that results in a volume called <project_name>_nginx-conf
, which is just... ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's intended. The underscore is used for prefix (e.g. project name) and postfix (e.g. _default
, _1
). Using dash means it doesn't get mixed together with the prefix and postfix parts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree. This seems like an issue of standards. Maybe the project maintainers have an opinion? @jwilder
@@ -1,23 +1,25 @@ | |||
version: '2' | |||
version: '3' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to bump the version here, so that it can still work with older Docker and Docker Compose versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, no reason to rush to v3 until other Stack compatibility issues are fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though version 3 is list as the recommended version
https://docs.docker.com/compose/compose-file/compose-versioning/
For compatibility for Docker Compose v3, replaced the unnamed volume in the nginx service with a named nginx_conf volume that is shared by both services.
https://docs.docker.com/compose/compose-file/#/upgrading