20

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?

6 Answers 6

20

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, it does not appear that you really need docker-compose for what you want to achieve. Just plain docker is probably enough
4

you need to do "docker-compose up -d" then try ur commands

Comments

2

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/

1 Comment

This did it for me, thanks!
0

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.

Comments

-1

I solved this issue by first running sudo systemctl restart docker

Comments

-3

if you are using docker-compose run this command docker-compose run web python manage.py makemigrations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.