Hi,
I'm running a project with docker-compose. Sometimes, I need to launch commands inside the container to setup some behaviour (eg. To launch "bower install XYZ"). So that's my process:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------
dcpangularmodules_bundle_1 /entrypoint.sh make _gulpserve Up 0.0.0.0:3001->3000/tcp
And:
$ docker ps -a --filter name="dcpangularmodules" --format "{{ .Names }}"
dcpangularmodules_bundle_1
So it's ok for me. Now I launch "run" command:
$ docker-compose run bundle bash
And in another terminal
$ docker ps -a --filter name="dcpangularmodules" --format "{{ .Names }}"
dcpangularmodules_bundle_run_1
dcpangularmodules_bundle_1
$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------------
dcpangularmodules_bundle_1 /entrypoint.sh make _gulpserve Up 0.0.0.0:3001->3000/tcp
dcpangularmodules_bundle_run_1 /entrypoint.sh bash Up
That's ok.
Now, I close the container I created using "docker-compose run ..." by typing "exit", and I check:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------
dcpangularmodules_bundle_1 /entrypoint.sh make _gulpserve Up 0.0.0.0:3001->3000/tcp
$ docker ps -a --filter name="dcpangularmodules" --format "{{ .Names }}"
dcpangularmodules_bundle_run_1
dcpangularmodules_bundle_1
I close the entire docker-compose, and try to cleanup:
$ docker-compose rm
Going to remove dcpangularmodules_bundle_1
Are you sure? [yN] y
$ docker ps -a --filter name="dcpangularmodules" --format "{{ .Names }}"
dcpangularmodules_bundle_run_1
As you can see, the "runned" container is still here, and the next time I will use "run" command, a new container will be created. This may be a llimitation and maybe there are a lot of reason to make docker-compose working like that but I think this is problematic.
It only want to remove the containers that are launched with "up", not "run". So, after a while, I've got a huge list of stopped containers suffixed by "_run_X"...
Is there a way to avoid this ? At this time I use docker rm $(docker ps -a -q --filter name="dcpangularmodules")
to cleanup... But because I'm not alone on the project and not the entire team is aware of the problem... their host are filled by unused containers.
BTW: I Love docker and docker-compose :)
The --rm
flag will remove the container once it exits:
docker-compose run --rm bundle bash
Ho my god, I did seen this in "help" command... A big sorry for that. Thanks a lot.
I have
docker run -rm -it foo --name bar
# -rm flag should remove container after it finishes running...?
after it's done, I do $(docker ps -a)
and I see the container "bar" there, why is that? shouldn't the container be removed automatically?
I am on Docker version 17.03.1-ce, build c6d412e
and MacOS
@oresoftware here is "compose" repository. You should send this issue on docker
docker run --rm -it foo --name bar
... double dash on rm
:)
@ccsalway thanks yeah the problem was likely using -rm
instead of --rm
... it's been awhile but will remember for next time lol
Most helpful comment
The
--rm
flag will remove the container once it exits: