Compose: Cannot docker-compose exec when container_name is set

Created on 6 Apr 2017  路  7Comments  路  Source: docker/compose

I cannot run docker-composer exec having set container_name in my docker-compose yml.

This is the situation:

    $ docker ps
    CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
    52e76f68eb08        lrkwz/docker-php-magento-ready:5.6-fpm   "docker-php-entryp..."   2 minutes ago       Up 2 minutes        0.0.0.0:9000->9000/tcp   culti-magento
    $ docker-compose ps
    Name                   Command               State           Ports          
    -------------------------------------------------------------------------------
    culti-magento   docker-php-entrypoint /sta ...   Up      0.0.0.0:9000->9000/tcp 
    $ docker-compose exec culti-magento bash
    ERROR: No such service: culti-magento
    $ docker-compose exec 52e76f68eb08 bash 
    ERROR: No such service: 52e76f68eb08

Instead docker exec works fine

$ docker exec -it culti-magento bash 
root@4fa71b775cc6:/var/www/culti.com/magento# 

The docker-compose.yml is

    version: '3'
    services:
      php:
        container_name: culti-magento
        image: lrkwz/docker-php-magento-ready:5.6-fpm
        volumes:
        - ./magento:/var/www/culti.com/magento
        working_dir: /var/www/culti.com/magento
        ports:
           - "9000:9000"

Versions are

    $ docker-compose --version
    docker-compose version 1.11.2, build dfed245
    $ docker --version 
    Docker version 17.04.0-ce, build 4845c56
kinquestion

Most helpful comment

docker-compose exec <service_name>, not docker-compose exec <container_name>.

All 7 comments

docker-compose exec <service_name>, not docker-compose exec <container_name>.

If you do it like this, then it will say something along the lines of:
container SERVICENAME_1 not found

Because the container is named MY_CONTAINERNAME instead, for example.

If you don't give it a specific name, then this will obviously work, because the containername will be SERVICENAME_1

That shouldn't be an issue.

Is your service created and running? What's the output of docker-compose ps?

I can only tell from my case:
In my case it was indeed a container that is run once, does its job, and then dies. Perhaps docker-compose doesn't spin up the container by itself to make exec work in that case.
Strange enough I can even follow the reasoning behind that, but in my case the documentation of that specific container explicitly said to call docker-compose exec on that service, so I thought it should work on stopped containers as well.

I can't speak for @lrkwz, though.

docker exec / docker-compose exec can only be called on a running container. The error message is misleading in that case, but the behaviour is correct nonetheless.

Well... the poor documentation explains a lot about the overall quality of the specific docker-app I encountered then.
Thanks for clearing up, maybe @lrkwz has the same problem.

@shin- here's the output of ps

$ docker-compose ps
    Name                   Command               State           Ports          
-------------------------------------------------------------------------------
culti-magento   docker-php-entrypoint /sta ...   Up      0.0.0.0:9000->9000/tcp 

and of course you are right: in this example docker-compose exec php bash works, so probably only the messages are misleading or maybe the service name could be added to ps.

Was this page helpful?
0 / 5 - 0 ratings