6

I've been trying to execute bash on running docker container which has specific name as follows. --(1)

docker ps | grep somename | awk '{print  $1 " bash"}' | xargs -I'{}' docker exec -it '{}'

but it didn't work and it shows a message like

"docker exec" requires at least 2 argument(s)




when I tried using command as follows --(2)

docker ps | grep somename | awk '{print  $1 " bash"}' | xargs docker exec -it

it shows another error messages like

the input device is not a TTY




But when I tried using $() (sub shell) then it can be accomplished but I cannot understand why it does not work with the two codes (1)(2) above (using xargs)

Could any body explain why those happen?

I really appreciate any help you can provide in advance =)




EDIT 1:

I know how to accomplish my goal in other way like

docker exec -it $(docker ps | grep perf | awk '{print  $1 " bash"}' )

But I'm just curious about why those codes are not working =)

Abraxas5
  • 63
  • 1
  • 1
  • 6

3 Answers3

15
  • First question

"docker exec" requires at least 2 argument(s)

In last pipe command, standard input of xargs is, for example, 42a9903486f2 bash. And you used xargs with -I (replace string) option. So, docker recognizes that 42a9903486f2 bash is a first argument, without 2nd argument.

Below example perhaps is the what you expected.

docker ps | grep somename | awk '{print  $1 " bash"}' | xargs bash -c 'docker exec -it $0 $1'
  • Second question

the input device is not a TTY

xargs excutes command on new child process. So you need to reopen stdin to child process for interactive communication. (MacOS: -o option)

docker ps | grep somename | awk '{print  $1 " bash"}' | xargs -o docker exec -it
NHK
  • 769
  • 1
  • 7
  • 16
  • This is perfect answer that I expected. I really appreciate your kind answer =) It really helped me a lot – Abraxas5 Apr 21 '17 at 00:54
2

This worked for me:

sudo docker ps -q | xargs -I'{}' docker exec -t {} du -hs /tmp/
neisantos
  • 492
  • 1
  • 5
  • 16
0

The exec command you run is something like this:

docker exec -it 'a1b2c3d4 bash'

And that is only one argument, not two. You need to remove the quotes around the argument to docker exec.

... | xargs -I'{}' docker exec -it {}

Then you will exec properly with two arguments.

docker exec -it a1b2c3d4 bash
                 ------   ---
         first arg ^       ^ second arg
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • Thanks Dan. I also tried using "docker ps | grep somename | awk '{print $1 " bash"}' | xargs -I'{}' docker exec -it {}" but It didn't work for me ^^; It show the message `"docker exec" requires at least 2 argument(s)` like the result of first try. – Abraxas5 Apr 19 '17 at 18:03
  • @user3400490 What error do you get in that attempt? – Dan Lowe Apr 19 '17 at 18:04
  • `"docker exec" requires at least 2 argument(s) like the result of first try. ` as the edited comment above =) – Abraxas5 Apr 19 '17 at 18:13
  • @Abraxas5 Above you have `docker exec -it` without the `{}` following. Two different things... did you try with `{}` but with no quotes around it? – Dan Lowe Apr 19 '17 at 18:34
  • on a sidenote, you shoudl use `docker ps --filter` see this (outdated) doc https://docs.docker.com/v1.11/engine/reference/commandline/ps/ and the examples – user2915097 Apr 19 '17 at 20:28
  • @DanLowe My answer is YES. and there is following **`{}`** after that `docker exec -it` (maybe you didn't see because of the line change) @user2915097 Thanks, but sorry. I don't understand what you intended. I know the filter and format option with docker ps. But I want to do partial match of Image name and even if I use that ps filter what I really wanted to do is execute **`docker exec`** command with filtered id using xargs. I can accomplish my goal with `docker exec -it $(docker ps | grep perf | awk '{print $1 " bash"}' )` but I want to know why the codes above are not working. – Abraxas5 Apr 20 '17 at 00:16
  • the name of a container is unique, maybe you could start it with a specific name, find it with the filter, as it would be more reliable – user2915097 Apr 20 '17 at 05:16
  • @user2915097 I can understand what you mean, but I think you didn't get my point ^^; But I really appreciate for your concern =) – Abraxas5 Apr 21 '17 at 02:31