user1496984
user1496984

Reputation: 11575

How stop containers run with `docker-compose run`

I'm trying to use docker-compose to orchestrate several containers. To troubleshoot, I frequently end up running bash from within a container by doing:

$ docker-compose run --rm run web bash

I always try pass the --rm switch so that these containers are removed when I exit the bash session. Sometimes though, they remain, and I see them at the output of docker-compose ps.

           Name                          Command                State      Ports
----------------------------------------------------------------------------------
project_nginx_1            /usr/sbin/nginx                  Exit 0
project_nginx_run_1        bash                             Up         80/tcp
project_web_1              python manage.py runserver ...   Exit 128
project_web_run_1          bash                             Up         8000/tcp

At this point, I am trying to stop and remove these components manually, but I can not manage to do this. I tried:

$ docker-compose stop project_nginx_run_1
No such service: project_nginx_run_1

I also tried the other commands rm, kill, etc..

What should I do to get rid of these containers?

Edit:

Fixed the output of docker-compose ps.

Upvotes: 55

Views: 57822

Answers (4)

Evedel
Evedel

Reputation: 705

In my particular case,

docker compose down --remove-orphans

worked the best (but that is assuming that you don't need to continue running any of the services from a compose file)

Upvotes: 0

The docker-compose, unlike docker, use the names for it's containers defined in the yml file. Therefore, to stop just one container the command will be:

docker-compose stop nginx_run

Upvotes: 5

MediaVince
MediaVince

Reputation: 499

docker-compose down

from within the directory where it was launched, is the only way I managed to confirm it was stopped, as in docker-compose ps no longer yields it!

Upvotes: 1

Thomasleveil
Thomasleveil

Reputation: 104155

just stop those test containers with the docker stop command instead of using docker-compose.

docker-compose shines when it comes to start together many containers, but using docker-compose to start containers does not prevent you from using the docker command to do whatever you need to do with individual containers.

docker stop project_nginx_run_1 project_web_run_1 

Also, since you are debugging containers, I suggest to use docker-compose exec <service id> bash to get a shell in a running container. This has the advantage of not starting a new container.

Upvotes: 59

Related Questions