Check docker instance in Bash - docker

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.

Related

Get the last Docker image built

This command gives a list of image and container IDs ordered from top to bottom by last build time:
docker images
All my docker images are appended with the hash git head commit.
The results
REPOSITORY TAG IMAGE ID CREATED SIZE
username/play-table-of-contents-1474f94 latest 6141b8177c2f 34 minutes ago 149MB
username/play-table-of-contents-2616f5f latest 2b5422dd91ba About an hour ago 149MB
Is there a way to get only the last image by name ? ( ie: case 6141b8177c2f )
I tried with
docker images --format "{{.Names}}"
My end goal is to run the last docker image built. To do this, I need to
get the last image name in bash script variable.
docker run ... $last_image ...
Docker command docker images list out most recently created images.
The following command list out the first image from the above list. I believe you are looking for this command.
docker images | awk '{print $1}' | awk 'NR==2'
You would probably deploy a container of the image from above command
docker run $(docker images | awk '{print $1}' | awk 'NR==2')
All the other answers' solution relies on the fact docker image sorts the output by date already. This may not be true. A consistent solution would be to sort them by the creation date and get the latest one. I used the following command, this is consistent.
docker images --format "{{.ID}} {{.CreatedAt}}" | sort -rk 2 | awk 'NR==1{print $1}'
This command sorts the output of the docker images command by CreatedAt column and print the id of the latest image
Short Answer
docker run ... $(docker ps -a --format "{{.Names}}" | head -1) ...
docker ps -a return the stopped and running containers in the order "Last to First".
Powershell
docker images --format "{{.ID}}" | select -first 1
example use with docker run:
docker run -it (docker images --format "{{.ID}}" | select -first 1)
Bash
docker images --format='{{.ID}}' | head -1
example use with docker run:
docker run -it $(docker images --format='{{.ID}}' | head -1)
This returns the IMAGEID of the latest built docker image:
docker images -q --format='{{.ID}}' | head -1
You can even collect it in a variable and use it as you like:
IMAGE_ID=$(docker images -q --format='{{.ID}}' | head -1)
If you want to enter the last docker image you ran :
docker run -it $(docker images | awk '{print $3}' | awk 'NR==2') /bin/sh
OR
docker run -it $(docker images | awk '{print $3}' | awk 'NR==2') bash
if you need get last build from an specific image name.. you can do:
IMG_NAME="my-image-name"
IMG_LAST_BUILD=$(docker images | grep $IMG_NAME | awk 'NR==1{printf("%s\:%s",$1,$2)}')
echo $IMG_LAST_BUILD
#my-image-name:tag_version

Running `bash` using docker exec with xargs command

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

Is there a way to list all the running docker containers by name?

I'm aware of how to get a list of "container ID's" of all running docker containers.
$ docker ps -q
This should do it. Apologies for the slash at the start.
$ docker inspect -f {{.Name}} $(docker ps -q)
/test
/test2
maybe docker ps | awk 'NR>1 {print $2}' the NR>1 avoids print ID for the first line
I haven't found a solution using docker ps, but you can do it using docker-compose (formerly fig):
docker-compose ps | awk '{print $1}' will return something like this:
Name
-------------------------------------------------------------------------------
src_bus_1
src_db_1
src_images_1
src_nginx_1
src_python_1
docker ps | awk 'NR>1 {print $(NF)}'
does it
NR>1 avoids printing the title row and print $(NF) prints the last column of the output.

How to filter docker process based on image

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" )'

Use last container name as default in docker client

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

Resources