How do docker exec -ti CONTAINER_NAME / bin / bash in deployed docker stack?

I have a deployed docker stack on AWS with a swarm:

docker stack deploy --with-registry-auth -c docker-stack.yml pipeline


I want to get an interactive bash session into one of the containers defined in the docker-stack.yml file, but the various calls docker exec -ti CONTAINER_NAME /bin/bash

I've tried all fail.

What is the correct method to get the container name to be passed:

docker exec -it CONTAINER_NAME /bin/bash


considering that:

docker service ps pipeline_django


returns valid service information and:

docker stack ps pipeline


returns valid stack information.

None of the documented methods for obtaining instance_name from these commands work when the command is passed docker exec -it

. They all fail:

Error response from daemon: no such container

I've tried the things listed here:

execute command in docker roaming service


source to share

2 answers

You can use below command and replace SERVICE_NAME with your service name:

docker exec -it \
    $(docker stack ps pipeline| grep SERVICE_NAME | cut -d' ' -f1) /bin/bash


this command finds your service container id:

docker stack ps pipeline| grep SERVICE_NAME | cut -d' ' -f1




Here is an example of my local macOS.

$ docker stack ls
NAME                SERVICES
gospot_web          1

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                               PORTS
qigx492p2lbe        gospot_web_web      global              1/1                 gospot/gospot-web:0.1.20.g225306b   *:80->80/tcp, *:443->443/tcp


You can use container id or container name to access the container.

$ docker ps
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                 PORTS               NAMES
b3824a85d3c8        gospot/gospot-web:0.1.20.g225306b   "nginx -g 'daemon ofโ€ฆ"   2 hours ago         Up 2 hours (healthy)   80/tcp, 443/tcp     gospot_web_web.3cqcd1v22vaon517j7p5h1d9a.wrel676fcllssrm3151ymkse9

$ docker exec -it b3824a85d3c8 sh
/ # ls
bin    etc    lib    mnt    root   sbin   sys    usr
dev    home   media  proc   run    srv    tmp    var
/ #

$ docker exec -it 
gospot_web_web.3cqcd1v22vaon517j7p5h1d9a.wrel676fcllssrm3151ymkse9 sh
/ # ls
bin    dev    etc    home   lib    media  mnt    proc   root   run    
sbin   srv    sys    tmp    usr    var
/ #


If the target service name is unique enough, you can use the following one-line file.

$ docker exec -it $(docker ps | grep gospot_web_web | awk '{print $1}') sh
/ # ls
bin    etc    lib    mnt    root   sbin   sys    usr
dev    home   media  proc   run    srv    tmp    var
/ #


Hope it helps.



All Articles