Reputation: 24794
I was trying to open a second terminal un a docker with docker-compose.
First run the container with
docker-compose run my-centos bash
And when I try to open a second terminal
docker-compose exec my-centos bash
I get the message
ERROR:No container found for my_centos_1
If I search the name of running container I get
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34a95b44f0a2 centos6 "bash" 9 minutes ago Up 9 minutes docker_my-centos_run_1
why docker-compose exec search docker_my_centos_1
and not docker_my-centos_run_1
?
Upvotes: 20
Views: 36708
Reputation: 18973
Another possible reason why you might run into this error is if you have running docker compose projects such that re.sub(r'[_-]', '', project_name_a) == project_name_b
(e.g. you were running a project without hyphens, but then decided to add them) and docker-compose
<=1.29.2
(likely) and at least >=1.27.4
. E.g.:
services:
a:
image: alpine:3.19
command: sleep infinity
init: true
$ docker-compose -p p up -d
Creating network "p_default" with the default driver
Creating p_a_1 ... done
$ docker-compose -p p- up -d
Creating p-_a_2 ... done
# do note number 2 in the output above
$ docker-compose -p p- exec a echo test
ERROR: No container found for a_1
That happens because some parts of the code support the legacy convention to remove [_-]
from project names. More on it here.
Upvotes: 0
Reputation: 1
if you are using docker-compose run this command docker-compose run web python manage.py makemigrations
Upvotes: -3
Reputation: 841
I think you may be having trouble with the project name, because you're not telling Docker with project are you referring to.
I was facing this problem and it can be fixed adding the project name like:
docker-compose -p myprojectname ps
or
docker-compose -p myprojectname exec php_service composer install
This post explain it: https://codereviewvideos.com/blog/how-i-fixed-docker-compose-exec-error-no-container-found-for/
Upvotes: 2
Reputation: 524
docker-compose
is meant to run multi-container applications and is supposed to be used with docker-compose up
. When you use docker-compose run
, you make a special container that's not really meant for normal use.
Since docker-compose is just a wrapper around docker, you can still access this special container via the normal docker command:
docker exec docker_my-centos_run_1 bash
Otherwise I'd suggest start your container with docker-compose up
. This makes it so that you can run the second bash in the way that you specified:
docker-compose exec my-centos bash
Note: I don't know if you can attach a TTY directly with docker-compose up
, so you might need to run an extra docker-compose exec my-centos bash
to get two TTYs.
Upvotes: 20
Reputation: 47
I solved this issue by first running sudo systemctl restart docker
Upvotes: -1