compose
compose copied to clipboard
Query About Creating Volumes Without Services in Docker Compose v2
Description
In Docker Compose v1, I was able to create volumes independently using the following syntax:
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.
Steps To Reproduce
- Up Docker Compose with the YAML file using v1 syntax to define volumes. Use the following YAML code:
version: '3.2'
volumes:
volume1:
driver: local
name: "volume1"
- Attempt to use the same file in Docker Compose v2 environment.
- Observe the "no service selected" error.
Compose Version
Docker Compose version v2.20.3
Docker Environment
Client:
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
Server:
Containers: 16
Running: 16
Paused: 0
Stopped: 0
Images: 63
Server Version: 23.0.1
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: journald
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 2456e983eb9e37e47538f59ea18f2043c9a73640
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: builtin
cgroupns
Kernel Version: 6.1.0-11-amd64
Operating System: Debian GNU/Linux 12 (bookworm)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.27GiB
Name: DEB-NUC11PAH-G6PA250003P9
ID: 09e27f09-76c5-45dc-b50d-bb2b05611aac
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Default Address Pools:
Base: 172.128.0.1/9, Size: 16
Base: fdd0:df31:dff5::1/104, Size: 112
Anything else?
No response
Docker Compose v2 removes unused resources from the model so that only required resources get created. This is why an "unused" volume won't be created. This decision makes more sense if you consider the introduction for profiles, where some service might be defined with related resources but disabled by default.
There's no workaround. Can you please elaborate on the use-case where you need volumes to be created but no service attached ?
Docker Compose v2 removes unused resources from the model so that only required resources get created. This is why an "unused" volume won't be created. This decision makes more sense if you consider the introduction for profiles, where some service might be defined with related resources but disabled by default.
There's no workaround. Can you please elaborate on the use-case where you need volumes to be created but no service attached ?
Since I use multiple compose files to manage multiple applications, sometimes I need a persistent volume for sharing files between applications, e.g. I want there to be a media volume that should not be belong to any of the fixed applications. Thus it may be a better way to create it in an independent compose file. Is it possible to provide an option of creating volume without checking whether it is used?
@L-Trump IIUC you use a single compose file as a yaml version for docker volume create XXX
the refer to this volume as an external one in your other compose applications ?
@ndeloof Yes, that's what I want. This way I can avoid manually create volumes via the docker volume
command and manage every thing in docker compose. A compose file is easier to manage than storing/remembering shell commands. Similarly, I think networks can be created in such a way so I don't need to bind a network to a specific proxy server.
Note that this also affects the situation when I no longer have any services in the compose file and I want to clean up any leftover containers with docker compose up --remove-orphans
. The new flag --all-resources
works for this use case. Command docker compose down --remove-orphans
works even without the new flag.
I am generating the compose file, so it more suitable for me to always run the same command and let docker compose figure out what it should do.