19

I'm unable to connect to a container that's running on a swarm. Seems like the following doesn't work:

docker exec -it <container_ID> bash

Here is some output:

>$ docker service ls
ID            NAME          REPLICAS  IMAGE                              COMMAND
4rliefwe74o5  login         1/1       login-arm64:1.0


>$ docker service ps login
ID                         NAME     IMAGE                       NODE               DESIRED STATE  CURRENT STATE          ERROR
2jk3s2xs7ce62piunbkiptypz  login.1  login-arm64:1.0  odroid64-cluster4  Running        Running 5 minutes ago

Then I'll run:

$ docker exec -it 2jk3s2xs7ce62piunbkiptypz bash

or

$ docker exec -it login.1 bash

and see the following errors

Error response from daemon: No such container: 2jk3s2xs7ce62piunbkiptypz

Error response from daemon: No such container: login.1

Murmel
  • 5,402
  • 47
  • 53
Andi Jay
  • 5,882
  • 13
  • 51
  • 61

2 Answers2

23

Use docker ps to find the names you can use. Look under both CONTAINER ID and NAMES, either will work.

>$ docker ps
CONTAINER ID        IMAGE             COMMAND                  CREATED             STATUS              PORTS               NAMES
e53bff8bebfc        login-arm64:1.0   "/bin/sh -c 'node ser"   27 seconds ago      Up 25 seconds                           login.1.cg7fltcu3wfe7ixtnqzg8myy1

>$ docker exec -it e53bff8bebfc bash
root@e53bff8bebfc:/#

The long name is of the form $SERVICE_NAME.$REPLICA_NUMBER.$ID_FROM_SERVICE_PS

>$ docker exec -it login.1.cg7fltcu3wfe7ixtnqzg8myy1 bash
root@e53bff8bebfc:/#
Sam Myers
  • 2,112
  • 2
  • 15
  • 13
  • In my case, the long name did not work, but the "container id" does work. – TGarrett Oct 06 '17 at 15:22
  • This is probably obvious to everyone but me, but still, if anyone is dumbed down in the same way, the `docker ps` has to be run on a swarm **worker** node, otherwise on master `docker ps` results to nothing for me, as there are no running containers. – Aliaksei Kaniaveha Jan 28 '22 at 12:41
  • 1
    A minor supplement: If you see the container you want with `docker service ps` but not `docker ps`, check the `NODE` listed by `docker service ps` and log into _that_ host instead of the master node you started with. From there you can use `docker exec` as @sam-myers describes. – Sarah Messer Sep 28 '22 at 18:22
11

Quite an older question, but just my two cents here: I very often run:

docker exec -it $(docker ps -q -f name="login*") sh

-q only returns the container id

-f name="login*" applies a filter based on container name, using a regex

This comes in handy because starting a new container will change the container name with some random characters in it. It's important that your filter returns just 1 container, so specify the name in a way that there will be just 1 result. For example: if you have a container "monster" and a container "monitor", you need -f name="moni*" to exclude the "monster" container.

The command will result in something like:

docker exec -it login.1.cg7fltcu3wfe7ixtnqzg8myy1 sh

Kenny
  • 571
  • 5
  • 18
  • Is the `*` really useful ? I get the container ID without using it. – AymDev Mar 18 '20 at 17:02
  • Perhaps the docker binaries do something smart when filtering based on a regex, I don't know that. This is just the way I use it :). – Kenny Mar 22 '20 at 20:23