I am basically looking to achieve this 2 steps:
1. Run the docker image:
docker run -p 80:80 some-image-name:25
2. Now "docker ps" returns whole data about the container but I was looking for just the container ID to
3. run some test on it.. ex.
docker exec -it /usr/bin/npm run test
So my question is how I can get just the container id from step 2.
Note: I need this flow for my pipeline script in Jenkins.
docker ps -a -q
This will give you only container's id
You could use awk to get the container ID's as follows:
docker ps | awk 'NR > 1 {print $1}'
This one-liner outputs all the container ID's printed by docker ps. To get only the first one you would use:
docker ps | awk 'NR > 1 {print $1; exit}'
Even though that answers your question I recommend that you use container names instead of relying on container ID's.
P.S.: This solution is on average 1 millisecond slower than docker ps -q, but it is significantly more flexible.
docker ps --format {{.ID}}
Will return only the ids of running containers.
you can use docker functionality to get this done:
docker ps --filter volume=remote-volume --format "table {{.ID}}\t{{.Mounts}}"
with --format "{{.ID}}" you'd get the ids only. You can also filter. Read the documentation of docker ps for more details
All the below command give you container id's
docker ps -aqf "name=containername"
docker ps --no-trunc -aqf name=containername
docker container ls | grep 'container-name' | awk '{print $1}'```
You can get container ID using following command:
docker ps -q
Related
I'm trying to stop all the containers starting with the name app_
I though this would work: docker stop $(docker ps -f name="app_*"), but it shows:
unknown shorthand flag: 'f' in -f
See 'docker stop --help'.
Is there a way to do this?
You must put the complete filter expression into parantheses:
docker ps -f "name=app_*"
The search is fuzzy by default, so e.g. name=app will also return my-app.
You can use a regex to indicate that the match should be at the start:
docker ps -f "name=^app_"
You should further add the quiet flag q so that the command only returns ids to make it work with docker stop:
docker stop $(docker ps -qf "name=^app_")
Your command would work, if you had the docker ps show only the ID of containers.
I started some sleeping containers like this:
for i in $(seq 1 10); do
docker run --rm -d --name sleep_$i bash sleep 100000;
done
And since they were all named sleep_* I could stop them like this:
docker ps -f Name=sleep_* --format '{{.ID}}' | xargs docker stop
I usually use xargs rather than $() when possible for this kind of thing - for one reason, I can easily parallelize it with -n 3 -P 10 (10 concurrent executions of 3 each):
$ docker ps -f Name=sleep_* --format '{{.ID}}' | xargs -n 3 -P 10 docker stop
c58a1fc538cf
98a9358734e4
e34828d3a7d7
ffbd2ec18775
1ba1e8a304e7
5e78aecb4ed6
cf86c4ea46aa
624a7d0342c2
7cb17ece1f0a
01665a084d02
Quite a lot faster for many containers.
My docker sometimes create randomw container name based on my docker image e.g. yeeyi
How to docker rm all off the containers where the image is yeeyi?
is there something like? docker rm all --image yeeyi in a single command line?
You can do this using this command:
docker rm $(docker ps -a -q --filter "ancestor=ubuntu")
replace ubuntu with your image name.
This basically gets all the container ids (running or otherwise) that use the image ubuntu and then removes them.
Try the below workaround(update the grep string according to your need);
docker ps --filter "status=exited" | grep yeeyi
Check the output of above command, if you have the correct list, then use the below command;
docker rm (docker ps --filter "status=exited" | grep yeeyi | awk '{print $1}')
Another option is to check the exit code of such randomly created container. If that code is different then rest you can use the below command to get list of such containers;
docker ps -a --filter "exited=<status code>"
I'm using puckel/docker-airflow with CeleryExecutor. It launches a total of 5 containers named like this
docker-airflow_flower_1_de2035f778e6
docker-airflow_redis_1_49d2e710e82b
..
While development, I often have to stop all above containers. However, I can't do a docker stop $(docker ps -aq) since I have other containers running on my machine too.
Is there a way to stop all containers who's names match a given pattern (for instance all containers who's names start with docker-airflow in above)?
From this article by #james-coyle, following command works for me
docker ps --filter name=docker-airflow* --filter status=running -aq | xargs docker stop
I believe docker CLI natively does not provide such a functionality, so we have to rely on filtering and good-old bash PIPE and xargs
UPDATE-1
Note that depending on your environment, you might have to do these
run docker commands with sudo (just prefix both docker .. commands above with sudo)
enclose name pattern in double-quotes --filter name="docker-airflow*" (particularly on zsh)
Better late than never ;). From this article. The following works for me:
Stop containers with names matching a given pattern:
$ docker container stop $(docker container ls -q --filter name=<pattern>)
On the other hand, if we want to start containers with names matching a given pattern:
$ docker container start $(docker container ls --all -q --filter name=<pattern>)
NOTE: For different environments related tips, #y2k-shubham's update is a good starting point.
Another approach using grep and docker ps:
To stop docker container matching the given pattern/list of pattern":
docker ps | grep -E "name_1|name_2|name_3" | awk '{print $1}' | xargs docker stop
To stop docker container excluding the given pattern/list of pattern:
docker ps | grep -Ev "name_1|name_2|name_3" | awk '{print $1}' | xargs docker stop
Reference: Grep
I want to get a container ID of docker service.
Is there any command available for this ?
I tried
docker service ps MyService
but this one only gives the service ID, I am interested in the container id in which the service is running
try combination of docker process filtering and formatting:
docker ps -f name=YOUR_SERVICE_NAME --format "{{.ID}}"
UPDATE
thanks to ahivert for even shorter solution:
# same behavior with
docker ps -f name=YOUR_SERVICE_NAME --quiet
try from
https://github.com/moby/moby/issues/31369
for f in $(docker service ps -q $service);do docker inspect --format '{{.NodeID}} {{.Status.ContainerStatus.ContainerID}}' $f; done
and
docker network inspect --verbose
from https://github.com/moby/moby/pull/31710
docker service ps -q -f desired-state=running SERVICE_NAME | xargs docker inspect --format '{{.Status.ContainerStatus.ContainerID}}'
docker ps | grep "<service-name>\." | awk '{print $1}'
I tried this, it gives me a list of running services:
docker container ls
how to find which docker container is using/referencing a particular image?
To give more detail, say I have some 10 docker images and there are some 30 docker containers.. How can find which containers are created using docker image ID XXXXX using a simple command?
You need to dig through the docker history output for other images to see what is linked back. There's an image out there that automates much of this that you can run with the following:
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock \
nate/dockviz images -t -l
More details on the above command can be found on this github repo.
Here you go:
docker ps -a | awk '{ print $1,$2 }' | grep $(docker images | grep *image-id* | awk '{ print $1}')