Use last container name as default in docker client - docker

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

Related

How can I restore all of my lost Docker containers?

After a restart of VM I was not able to run any docker command. I follow some question on stack overflow and run the following command ps axf | grep docker | grep -v grep | awk '{print "kill -9 " $1}' | sudo sh
Now There is no image or container.
Result of commands
docker ps and ps -a
docker images list
docker ls
docker ls -a
All the command return empty list. Everything has been clean up.
Is there any way to find our backups or restore the deleted containers?

Docker get real-time logs of one of the tasks of a service

I can't ssh into docker manager and get service logs. I get "docker service logs" requires exactly 1 argument. error, when I ssh into my manager with
docker-machine ssh manager1
and run
docker logs --follow $(docker ps | grep redis | head -n1 | cut -d " " -f1)
everything works, but when I run the following command I get "docker service logs" requires exactly 1 argument.
docker-machine ssh manager1 "docker service logs $(docker ps | grep redis | head -n1 | cut -d ' ' -f1) --follow"
how can I ssh into manager1 and pass the command to run?
docker ps has some filtering and output options that mean awk/grep etc are rarely needed.
docker-machine ssh manager1 sh -uexc 'echo;
cid=$(docker ps -q -f ancestor=redis -l);
docker service logs -f $cid || echo cid[$cid]'
docker ps options.
--filter , -f Filter output based on conditions provided
--format Pretty-print containers using a Go template
--last , -n Show n last created containers (includes all states)
--latest , -l Show the latest created container (includes all states)

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

Remove all Docker containers except one

I have script that stops containers and then removes them
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
But I don't want to remove the docker container with name "my_docker".
How can I remove all containers except this one?
You can try this, which will
Filter out the unwanted item (grep -v), and then
returns the first column, which contains the container id
Run this command:
docker rm $(docker ps -a | grep -v "my_docker" | awk 'NR>1 {print $1}')
To use cut instead of awk, try this:
docker rm $(docker ps -a | grep -v "my_docker" | cut -d ' ' -f1)
Examples for awk/cut usage here: bash: shortest way to get n-th column of output
The title of the question asks for images, not containers. For those stumbling across this question looking to remove all images except one, you can use docker prune along with filter flags:
docker image prune -a --force --filter "label!=image_name"
replacing image_name with the name of your image.
You can also use the "until=" flag to prune your images by date.
This is what's actually happening docker rm $(List of container Ids). So it's just a matter of how you can filter the List of Container Ids.
For example: If you are looking to delete all the container but one with a specific container Id, then this docker rm $(docker ps -a -q | grep -v "my_container_id") will do the trick.
I achieved this by the following command:
docker image rm -f $(docker images -a | grep -v "image_repository_name" | awk 'NR>1 {print $1}')
For those, who want to exclude more than 1 container just add
grep -v "container_name2" |
after the grep -v "container_name1" command.
The final command might look like
docker rm $(docker ps -a | grep -v "my_docker1" | grep -v "my_docker2" | cut -d ' ' -f1)
I would prefer to test the container name using something along the lines of (untested)
docker inspect --format '{{ .Name }}' $(docker ps -aq)
this will give the names of the (running or not) containers, and you can filter and
docker rm
using this information
Using docker ps with --filter name=<my_docker>
docker rm $(docker ps -a -q | grep -v `docker ps -a -q --filter "name=my_docker"`)
Old question, but I like reviving posts.
For such case you could use Spotify's Docker GC: https://github.com/spotify/docker-gc#excluding-containers-from-garbage-collection
You could do:
echo "my_docker" >> /tmp/docker-gc-exclude-containers
echo '*' > /tmp/docker-gc-exclude
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -v /tmp/docker-gc-exclude-containers:/etc/docker-gc-exclude-containers:ro -v /tmp/docker-gc-exclude:/etc/docker-gc-exclude:ro spotify/docker-gc
(if you would like to get your images cleaned off too, you can avoid mounting the docker-gc-exclude file)
To stop
docker stop $(docker ps -a -q | grep -v "my_container_id")
To remove
docker rm $(docker ps -a -q | grep -v "my_container_id")
Deletes all the images except node image
sudo docker rmi -f $(sudo docker image ls -a | grep -v "node" | awk 'NR>1 {print $3}')
I know its little late response but it can help any windows user. Here is the command I prepared:
docker rmi $(
docker image list -a --no-trunc --format "table {{.ID}},{{.Repository}}" |
Where-Object {
$_ -NOTMATCH "mcr.microsoft.com/dotnet/core/aspnet" -and
$_ -NOTMATCH "ubuntu" -and
$_ -NOTMATCH "busybox" -and
$_ -NOTMATCH "alpine"
} | Select-Object -Skip 1 | %{ $_.Split(',')[0];}
)
Maybe you can start all the containers you don't want to prune. Then, run:
docker container prune -a

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

Resources