V2: Remove anonymous volumes when using run --rm
Description
Running one-off commands with docker-compose does not delete volumes used by the container. This is different behavior from docker run --rm or docker-compose v1, which removes volumes after the container is deleted.
https://github.com/docker/compose/issues/3611 fixes same issue on docker-compose v1.
Steps to reproduce the issue:
- prepare docker-compose.yml as follows:
version: "3.9"
services:
test:
image: busybox
command: echo "create anonymous volume"
volumes:
- /test
- run
docker-compose run --rm test
Describe the results you received:
Docker-compose v2 doesn't delete anonymous volumes.
$ docker volume ls
DRIVER VOLUME NAME
$ docker-compose run --rm test
[+] Running 1/0
⠿ Network devops_default Created 0.1s
create anonymous volume
$ docker volume ls
DRIVER VOLUME NAME
local d453f2ccd0cee093a001f56704a657ed96e3da43543fda0153b83519b8a59e50
Describe the results you expected:
Like docker-compose v1, docker-compose run --rm deletes anonymous volumes.
$ docker volume ls
DRIVER VOLUME NAME
$ /tmp/docker-compose run --rm test
Creating devops_test_run ... done
create anonymous volume
$ docker volume ls
DRIVER VOLUME NAME
Output of docker compose version:
# Docker
$ docker version
Client:
Version: 20.10.7
API version: 1.41
Go version: go1.15.14
Git commit: f0df350
Built: Wed Nov 17 03:05:36 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server:
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.15.14
Git commit: b0f5bc3
Built: Wed Nov 17 03:06:14 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.6
GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc:
Version: 1.0.0
GitCommit: 84113eef6fc27af1b01b3181f31bbaf708715301
docker-init:
Version: 0.19.0
GitCommit: de40ad0
# docker-compose v2
$ docker-compose version
Docker Compose version v2.3.3
# docker-compose v1
$ /tmp/docker-compose version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
Output of docker info:
$ docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 2
Server Version: 20.10.7
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
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: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
Default Runtime: runc
Init Binary: docker-init
containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc version: 84113eef6fc27af1b01b3181f31bbaf708715301
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.14.268-205.500.amzn2.x86_64
Operating System: Amazon Linux 2
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.793GiB
Name: *********
ID: GGVV:O63J:PFCH:6NZX:GH7P:H6JT:5Y7Z:G7AJ:I7CD:N5QK:IJ3X:MFQ2
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
+1 to align docker compose run with docker run regarding --rm flag
@kangaechu
I tried to reproduce the issue, but it seems that everything is working fine when using the latest version of compose. Anonymous volume are well cleaned up.
I think it has been fixed in https://github.com/docker/compose/pull/9258, which is available since v2.3.4
Are you able to confirm ?
I was seeing this behavior in v2.2.1, but after updating just now to v2.4.1 the behavior is correct. Running with --rm correctly removes all anonymous volumes associated with that run.
After updating docker-compose to 2.4.1, I confirmed docker compose run --rm correctly remove anonymous volumes.
Thank you!