jvhashe
jvhashe

Reputation: 1121

Piping docker run container ID to docker exec

In my development, I find myself issuing a docker run command followed by a docker exec command on the resulting container ID quite frequently. It's a little annoying to have to copy/paste the container ID between commands, so I was trying to pipe the container ID into my docker exec command.

Here's my example command.

docker run -itd image | xargs -i docker exec -it {} bash

This starts the container, but then I get the following error.

the input device is not a TTY

Does anyone have any idea how to get around this?

Edit: I also forgot to mention I have an ENTRYPOINT defined and cannot override that.

Upvotes: 6

Views: 4347

Answers (2)

aifos324
aifos324

Reputation: 98

If you just want to "bash"-into the container you do not have to pass the container-id around. You can simply run

docker run -it --rm <image> /bin/bash

For example, if we take the ubuntu base image

docker run -it --rm ubuntu /bin/bash
root@f80f83eec0d4:/#

from the documentation

-t : Allocate a pseudo-tty
-i : Keep STDIN open even if not attached
--rm : Automatically remove the container when it exits

The command /bin/bash overwrites the default command that is specified with the CMD instruction in the Dockerfile.

Upvotes: 0

Robert
Robert

Reputation: 36833

Do this instead:

ID=$(docker run -itd image)  && docker exec -it $ID bash

Because xargs executes it arguments without allocating a new tty.

Upvotes: 12

Related Questions