scubbo
scubbo

Reputation: 5837

Cannot pipe docker pid to bash command

I commonly want to open a bash shell on a docker image. A multi-command process for this would be:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
bba983d72d48        scubbo/datenight    "apachectl -DFOREGROU"   7 days ago          Up 7 days           0.0.0.0:80->80/tcp   pensive_bell
$ docker exec -it bba983d72d48 bash

I'd like to shortcut this. However, I get the following error:

$ docker ps | awk 'NR > 1 {print $1}' | xargs -I {} docker exec -it {} bash
cannot enable tty mode on non tty input

From a little Googling, I found this issue - however, if I drop the -t option, the command "completes" immediately.

I have confirmed that manually copy-pasting the output of $ docker ps | awk 'NR > 1 {print $1}' into the appropriate position of docker exec -it {} bash is successful.

EDIT: Cutting out the docker ps from the pipe, the following also fails:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
4f20409c37b7        scubbo/datenight    "apachectl -DFOREGROU"   8 days ago          Up 8 days           0.0.0.0:80->80/tcp   drunk_northcutt
$ docker ps -q
4f20409c37b7
$ echo '4f20409c37b7' | xargs -I {} docker exec -it {} bash
cannot enable tty mode on non tty input

Upvotes: 4

Views: 1545

Answers (2)

user2915097
user2915097

Reputation: 32196

Notice that docker ps -q outputs only the ids of the running containers

If you have only one container launched, just do

docker exec -it $(docker ps -q) bash

if you want to enter the last launched container

docker exec -it $(docker ps -lq) bash

If you give a name to your container when you launch it

docker run --name nostalgic...

then you can filter it with

docker exec -it $(docker ps -q --filter "name=nostalgic") bash

Check the doc

https://docs.docker.com/engine/reference/commandline/ps/

Upvotes: 2

toad013
toad013

Reputation: 1350

I tend do this but I nest my commands

docker exec -it $(docker ps | awk 'NR > 1 {print $1}') bash

word of warning though, that you'll get errors if there's more than one container running.

Upvotes: 1

Related Questions