Hi,
In Docker Compose v1, I was able to create volumes independently using the following docker compose file :
version: '3.2'
volumes:
volume1:
driver: local
name: "volume1"
However, upon migrating to Docker Compose v2, I attempted to use the same approach. Unfortunately, I encountered an error message “no service selected.” It seems that the behavior has changed in v2 regarding how volumes are define
I’m looking for advice on how to create volumes in Docker Compose v2 without associating them with any specific service in Docker Compose v2. This was possible in v1, and I’m curious whether there’s a workaround or new syntax in v2 that achieves the same result.
Additional information
Compose version Docker Compose version v2.20.3
It is a combination of the compose specification now requires the services top level element (see: Compose file | Docker Docs) and the fact that in compose v2, the version is ignored, so that always the latest version of the schema is used.
You could try, and check what happens, if you declare an empty services top level element.
While defining services like services: error message → services must be a mapping
and while defining services like services: {} error message → no service selected
network_mode: none is required so compose will not create a network bridge.
The build section is to avoid pulling images. You can’t use “scratch” directly in a compose file, but you can use it in a Dockerfile and compose supports inline dockerfiles so you don’t even need a separate Dockerfile.
Doesn’t matter what you pass as a command but a command is required, otherwise you will get another error message. Since this couldn’t actually run and you don’t even want a container to run, you need to add --no-start to the compose up command.
It will still create an empty image and an empty container but it won’t start the container.
Edited:
I fixed the code because the volume mounting definition was missing which is required for the volume to be created.
Based on this topic I made a blog post in which I talk about volumes in general, and volume-only topics in more details so I could mention some possible issues as well and also how init containers could help: