Matt Adelman
Matt Adelman

Reputation: 79

Docker exec with ssh gives no such container Error

I am trying to ssh directly into a docker container using the following commands:

CONTAINER_ID=`ssh user@host -t "docker ps | head -n 2 | tail -n 1 | cut -d' ' -f1"`
ssh -tt user@host "docker exec -it $CONTAINER_ID /bin/bash"

When I do this I get:

Error: No such container: <container_id>

Even though if I run the exec on the instance itself, the container is there, and it does let me connect.

Upvotes: 0

Views: 3344

Answers (1)

Simone Zabberoni
Simone Zabberoni

Reputation: 2113

I bet that your $CONTAINER_ID contains CRLF characters. Remove them by using tr:

CONTAINER_ID=`ssh user@host -t "docker ps | head -n 2 | tail -n 1 | cut -d' ' -f1 | tr -d '\r\n' " `

And it should do the trick.

Of course, you'll connect to the first result of docker ps, whichever it is...

Upvotes: 2

Related Questions