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.


Solution 1:

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:

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