4

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
Community
  • 1
  • 1
scubbo
  • 4,969
  • 7
  • 40
  • 71

2 Answers2

2

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/

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • Thanks, that's a neat set of commands! I'm still not sure why I can't pipe the id into `xargs docker exec`, though. – scubbo Jul 27 '16 at 17:15
1

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.

toad013
  • 1,350
  • 9
  • 10