Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to docker exec -ti CONTAINER_NAME /bin/bash on deployed docker stack?

I have a deployed docker stack on AWS with 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, but the various docker exec -ti CONTAINER_NAME /bin/bash invocations that I have tried all fail.

What is the right method to derive a container name to be passed to:

docker exec -it CONTAINER_NAME /bin/bash

given that:

docker service ps pipeline_django

returns valid service information and:

docker stack ps pipeline

returns valid stack information.

None of the documented methods of deriving the container_name from these commands work when passed to the docker exec -it command. They all fail with:

Error response from daemon: No such container

I've tried the things listed here:

execute a command within docker swarm service

like image 931
David Watson Avatar asked Oct 18 '22 15:10

David Watson


1 Answers

The other answers failed, because they no not extract the container it from the task. Swarm wraps a task object around a container:

taskid=$(docker service ps -q ${servicename} |head -1)
containerid=$(docker inspect -f '{{.Status.ContainerStatus.ContainerID}}' ${taskid})
docker exec -it ${containerid} "$*"

Of course, this will only take the first container in the list.

like image 139
rhoerbe Avatar answered Oct 20 '22 23:10

rhoerbe