How stop containers run with `docker-compose run`

DockerDocker Compose

Docker Problem Overview


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.

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

With docker-compose, services can be stopped in two ways, but I would like add some detailed info about both options.

> #In short > > ##docker-compose down Stop and remove containers, networks, images, and volumes
> > ##docker-compose stop
Stop services


#In detail

If docker-compose run starts services project_nginx_run_1 and project_web_run_1, then

##docker-compose down log will be

$ docker-compose down
Stopping project_nginx_run_1 ...
Stopping project_web_run_1 ...

.
. some service logs goes here

Stopping project_web_run_1 ... done
Stopping project_nginx_run_1 ... done
Removing project_web_run_1 ... done
Removing project_nginx_run_1 ... done
Removing network project_default

##docker-compose stop log will be

$ docker-compose stop
Stopping project_nginx_run_1 ...
Stopping project_web_run_1 ...

.
. some service logs goes here

Stopping project_web_run_1 ... done
Stopping project_nginx_run_1 ... done

Solution 3 - Docker

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

Solution 4 - Docker

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1496984View Question on Stackoverflow
Solution 1 - DockerThomasleveilView Answer on Stackoverflow
Solution 2 - DockermrsrinivasView Answer on Stackoverflow
Solution 3 - DockerЖеня КолокольцевView Answer on Stackoverflow
Solution 4 - DockerMediaVinceView Answer on Stackoverflow