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 =)
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
This worked for me:
sudo docker ps -q | xargs -I'{}' docker exec -t {} du -hs /tmp/
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
Related
I am trying to ssh into server, and into a docker container to run the service. however I am not able to store containerId into a variable to pass it to enter the container.
#!/bin/bash
ssh test_server << EOF
ls
sudo docker ps | grep 'tests_service_image' | colrm 13 # This command works
containerId=$(sudo docker ps | grep 'tests_service_image' | colrm 13) # This doesn't
sudo docker exec -i "$containerId" bash # Throws error since containerId is empty
./run.sh
EOF
exit
The problem is that you are doing variable/function expansions on your own side. You need to escape those so that those expansions happen on the server side.
#!/bin/sh
ssh test_server << EOF
containerId=\$(sudo docker ps | grep 'tests_service_image' | colrm 13)
sudo docker exec -i "\$containerId" bash
./run.sh
EOF
exit
Edit:
Pass it directly to docker exec command like so
sudo docker exec -i $(sudo docker ps | grep 'tests_service_image' | colrm 13) bash
Original Answer:
This is written assuming that the script execution is done post sshing into the server. but modified the answer to above based on the specific query
container ID is stored in variable containerId, you are getting the error Error: No such container: because you are passing a different variable $container instead of $containerId to docker exec command.
Having docker ps -a
I want to match the NAMES VERSION and STATUS.
docker ps -a --format "{{.Image}}\t{{.Status}}" | awk -F$"\t" '{printf "%s|%s\n", $1, $2}'
Output:
registry.com/project/glass/glass_front:2.2.15.4|Up 6 days
registry.com/project/glass/glass_proxy:2.2.15.4|Up 6 days
registry.com/project/glass/glass_modeles_front:2.1.5.2|Up 6 days
How can i modify my command to have this:
glass_front | 2.2.15.4 | Up 6 days
glass_proxy | 2.2.15.4 | Up 6 days
glass_modeles_front| 2.1.5.2 | Up 6 days
Try using colon as the separator in the docker ps command, then use sed to transform the colon to pipe and remove the prefix:
docker ps -a --format "{{.Image}}:{{.Status}}" \
| sed -e 's/:/ | /g' -e 's,^.*/,,'
Could you please try following, not tested it as don't have docker command. Its based on completely shown sample output of OP only.
docker ps -a --format "{{.Image}}\t{{.Status}}" \
| awk -F'\t' '{num=split($1,arr,"[/:]");print arr[num-1],arr[num],$2}'
OR(only using field separator capability of awk)
docker ps -a --format "{{.Image}}\t{{.Status}}" \
| awk -F"[/:\t]" '{print $4,$5,$NF}'
Explanation(1st solution): Running docker program(what OP shown) then passing its output as an input to awk command. In awk command setting field separator as TAB. Then splitting 1st field into an array(arr) with delimiter of /,:, then finally printing arr's 2nd last and last items here with 2nd field of current line.
I'm executing this command from docker host which is finally not giving me any error on stdout. And completes successfully on prompt but doesn't executes what it is supposed to do inside container.
Can someone please help me identify what am i doing wrong?
docker exec -dt SPSSC /bin/bash -c "grep -ril 'LOCALIZATION_ENABLED="false"' /opt/tpa/confd/config/* | grep -v 'diameter' | xargs sed -i 's/LOCALIZATION_ENABLED="false"/LOCALIZATION_ENABLED="true"/g'"
I have been trying to get the container id of docker instance using docker process command, but when i'm trying with filter by name it works fine for me.
sudo -S docker ps -q --filter="name=romantic_rosalind"
Results container id :
3c7e865f1dfb
But when i filter using image i'm getting all the instance container ids :
sudo -S docker ps -q --filter="image=docker-mariadb:1.0.1"
Results Container ids :
5570dc09b581
3c7e865f1dfb
But i wish to get only container id of mariadb.
How to get container id of docker process using filter as image ?
Use "ancestor" instead of "image" that works great. Example:
sudo -S docker ps -q --filter ancestor=docker-mariadb:1.0.1
The Docker team may have added it in the last versions:
http://docs.docker.com/engine/reference/commandline/ps/
You can use awk and grep to filter specified container id.
For example:
docker ps | grep "docker-mariadb:1.0.1" | awk '{ print $1 }'
This will print id of your container.
docker ps -a | awk '{ print $1,$2 }' | grep imagename | awk '{print $1 }'
This is perfect. if you need you can add a filter of running images of a particular stsatus alone, like below
docker ps -a --filter=running | awk '{ print $1,$2 }' | grep rulsoftreg:5000/mypayroll/cisprocessing-printdocsnotifyconsumer:latest | awk '{print $1 }'
Various other filter options can be explored here
https://docs.docker.com/v1.11/engine/reference/commandline/ps/
With a command docker container ls for listing containers( which is a replacement for docker ps) solution would be:
docker container ls | grep "docker-mariadb:1.0.1" | awk '{ print $1 }'
you may also use * sign(if needed) like this:
docker container ls | grep "docker-mariadb:*" | awk '{ print $1 }'
See https://docs.docker.com/engine/reference/commandline/container_ls/
Example to command
docker container ls -af 'name=mysql' --format '{{.ID}}'
The following answer is accurate.
docker ps --all --format='{{json .}}' | jq -c '. | select( .Image=="docker-mariadb:1.0.1" )'
docker client for docker ps has very useful flag -l which shows container information which was run recently. However all other docker commands requires providing either CONTAINER ID or NAME.
Is there any nice trick which would allow to call:
docker logs -f -l
instead of:
docker logs -f random_name
You can you docker logs -f `docker ps -ql`
For the last container
docker ps -n 1
or variants such as
docker ps -qan 1
can be handy
After a while playing with docker tutorial, I created small set of aliases:
alias docker_last="docker ps -l | tail -n +2 | awk '{ print \$(NF) }' | xargs docker $1"
alias docker_all="docker ps -a | tail -n +2 | awk '{ print \$(NF) }' | xargs docker $1"
alias docker_up="docker ps | tail -n +2 | awk '{ print \$(NF) }' | xargs docker $1"
alias docker_down="docker ps -a | tail -n +2 | grep -v Up | awk '{ print \$(NF) }' | xargs docker $1"
Which allow to call command on last, all, up and down containers:
docker_last logs # Display logs from last created container
docker_down rm # Remove all stopped containers
docker_up stop # Stop all running containers