0

i want to pass docker container id from output of another linux command

example (as pseudo-code):

save output of docker ps cmd to a file
docker ps > docker.log
grep only container id : awk 'FNR == 2{print $1}' 

now i want to pass the output of awk cmd to docker exec

like docker exec -it awk 'FNR == 2{print $1}' /bin/bash (i am not able to pass container id through cmd)

this is to automate a process that needs ssh in docker containers

azbarcea
  • 3,323
  • 1
  • 20
  • 25

1 Answers1

1

Why not something like this:

docker exec -it $(docker ps | grep dac705114996 | awk '{print $1}') /bin/sh

As you can see, in order to pass the container-id to the docker exec cmd, you need to substitute the container-id with $(command that extracts the container-id) Command Substitution (more official docs)

azbarcea
  • 3,323
  • 1
  • 20
  • 25
  • 1
    i wanted to run a shell script to do ssh in docker container regard less of container id. i adopted your solution and changed a little bit and works for me thank you @azbarcea `docker exec -it $(docker ps | aws 'FNR ==2 {print $1}') /bin/bash` – Nagasrinivas Vemula Aug 29 '19 at 02:01