Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running `bash` using docker exec with xargs command

Tags:

docker

xargs

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 =)

like image 259
Abraxas5 Avatar asked Apr 19 '17 17:04

Abraxas5


Video Answer


1 Answers

  • 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
like image 65
NHK Avatar answered Oct 13 '22 01:10

NHK