compose icon indicating copy to clipboard operation
compose copied to clipboard

docker-compose exec -d (detached) doesn't return control of terminal

Open rickshawhobo opened this issue 9 years ago • 7 comments

According to the documentation docker-compose exec -d should run in detached mode, but after running with -d the control of the terminal is not returned. Am I missing something?

rickshawhobo avatar Mar 31 '17 19:03 rickshawhobo

It works for me.

You're missing the version of Compose you're using, the command you're running, and basically any other details that would allow me to attempt reproducing your issue.

shin- avatar Mar 31 '17 20:03 shin-

docker-compose --version
docker-compose version 1.11.2, build dfed245
docker --version
Docker version 17.03.0-ce, build 60ccb22

The command is basically any.

docker-compose exec -d [service_name] bash hangs and doesn't return the terminal. docker-compose exec -d [service_name] ls hangs and doesn't return the terminal.

For example I have a very simple service called mariadb. My yml looks like this

mariadb:
    image: mariadb:latest
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: database
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root

    volumes:
      - mariadb:/var/lib/mysql

Then I run docker-compose up -d mariadb

Then I run docker-compose exec -d mariadb bash and it just hangs and doesn't return the terminal. When I run without the -d option it takes me into the bash shell interactively.

rickshawhobo avatar Apr 01 '17 02:04 rickshawhobo

Maybe a similar issue : let's suppose the following script "telnet_client.sh" into a container named "telnet-client" :

while true; do
        if [ $# -eq 4 ]
        then
        TELNET_SERVER=$1
        NAME=$2
        PASSWORD=$3
        COMMANDS=$4
        eval '{ echo;
                sleep 3;
                echo $NAME ;
                sleep 1;
                echo $PASSWORD ;
                sleep 1;
                echo $COMMANDS ;
                sleep 1; }' | telnet $TELNET_SERVER


        else
                echo "Not enough arguments ! "
        fi

done;

If I call the following : docker-compose exec -d telnet_client bash -c '/telnet_client.sh telnet_server jack jack ls'

The control in the terminal is not returned.
Otherwise , if I call this : docker exec -d lab3_telnet_client_1 bash -c '/telnet_client.sh telnet_server jack jack ls'
the script is called correctly in background mode .

Docker version : Docker version 17.03.1-ce, build c6d412e
docker-compose version 1.11.2, build dfed245 On mac pro book , Sierra

giper45 avatar Apr 14 '17 11:04 giper45

A possible workaround could be

docker exec -d $(docker-compose ps -q service_name) command

giper45 avatar Apr 14 '17 13:04 giper45

@giper45 Does it work with docker-compose exec -T -d telnet_client bash -c '/telnet_client.sh telnet_server jack jack ls'?

By default, Compose allocates a TTY, while the Docker CLI doesn't, so the two commands you're using are not equivalent unless you add -T in the Compose case. This might be the reason for the discrepancies you're seeing.

shin- avatar Apr 14 '17 18:04 shin-

@shin-
Unfortunately it doesn't work , I just try ... this is the definition of container in docker-compose yaml :

 telnet_client:
    image: '...debian_bot:latest'
    stdin_open: true
    tty: true
    networks:
      internal_network:
        ipv4_address: 192.168.1.3
    cap_add:
      - NET_ADMIN

Maybe stdin_open and tty options could cause this ? Anyway, with docker exec command I resolved, let me know if I can give you further infos in order to discover if there is a problem !

giper45 avatar Apr 14 '17 19:04 giper45

Thanks, I was able to reproduce and identify the issue. This should be fixed by #4737

shin- avatar Apr 14 '17 22:04 shin-