Query About Creating Volumes Without Services in Docker Compose v2

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.

You could try, and check what happens, if you declare an empty services top level element

When I try to up docker compose by declaring an empty “services” in that situation services must be a mapping error occur while up docker-compose file

I assume you tried it like this?

services:

or like this:

services: {}

While defining services like services: error message → services must be a mapping
and while defining services like services: {} error message → no service selected

… so you are left with a new specification that doesn’t support it the way you need it anymore.

It is implemented according specs: https://github.com/compose-spec/compose-spec/blob/master/05-services.md.

If you feel this is bug, open an issue and see if the maintainers agree.

I found the goal interesting so I tried something even though I don’t think I would use it, but you can try this workaround:

volumes:
  volume1:

services:
  test:
    network_mode: none
    build:
      context: .
      dockerfile_inline: |
        FROM scratch
    command: fake
    volumes:
      - volume1:/fake
docker compose up --no-start

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.

1 Like

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: