How can a script check if a particular Docker container is running? - docker

I would like to have a shell script that checks if a particular container is running.
For example, I might want to start the container if it's not already running, or query some piece of information about it like what ports are exposed.

The docker ps command takes a -f / --filter option to locate the desired container. To test if the container is running by name we might try
$ docker ps --filter "name=myapp" --filter "status=running"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91959ed76e77 foo/barbaz:latest "/init" 10 minutes ago Up 10 minutes myapp
If we just want the container ID, because we're going to pass it to another command like docker exec, we can use -q / --quiet:
$ docker ps --filter "name=myapp" --filter "status=running" --quiet
91959ed76e77
To just check whether it is running, we can see if the output is non-empty:
if [ -n "$(docker ps -f "name=myapp" -f "status=running" -q )" ]; then
echo "the container is running!"
fi
Or if we want some other piece of information about it, --format:
$ docker ps -f "name=myapp" -f "status=running" --format "{{.Image}}"
foo/barbaz:latest

You might also try using the docker inspect command which works well with myapp as the container's name ... or the container's id :
if [ "$(docker inspect myapp --format '{{.State.Status}}')" = "running" ]; then
echo "the container is running!"
fi

Related

Filter docker containers by name and stop them

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.

How to stop a container harmless?

I use docker container stop xxx. It will cause jenkins fail if docker container xxx is not existed.
I want to know if there is an argument to stop a container without failing.It means when container exists, delete it, otherwise do nothing.
use this :
docker stop CONTAINER_NAME 2> /dev/null || true
will always give you exit code 0
You can use the below command :
docker container ps -all --filter "id=XXXXXX" --format {{.ID}} |xargs --no-run-if-empty docker container stop
Explanation :
docker container ps -all: This command will list all the containers.
docker container ps -all --filter "id=XXXXXX": This command will list the container with container id = XXXXXX.
docker container ps -all --filter "id=XXXXXX" --format {{.ID}} : This command will only print out the container id.
We will use this container id to pass it to the xargs command to run the docker container stop.
xargs --no-run-if-empty docker container stop: This command will stop docker container only if command before "|" (pipe) is non-empty.
You can manage in a simple way as mentioned below:
docker stop XXXX || true && docker rm XXXX || true
This will return true in either case and it will move out with success.
Reference in details at : stop and delete docker container if its running

How to correct docker in makefile which requires at least 1 argument for remove all containers command

The docker command "docker container rm $(docker ps -aq) -f" works fine from the command line. However, when I try to run it from a makefile using the following target ("remove_all_containers")...
remove_all_containers:
docker container rm $(docker ps -aq) -f
I get the error message:
host_name$ make remove_all_containers
docker container rm -f
"docker container rm" requires at least 1 argument.
See 'docker container rm --help'.
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
make: *** [remove_all_containers] Error 1
Clearly, when executed from within the makefile, the "docker ps" command is not being properly being properly executed in a way where its results can be collected and passed into the "container rm" command.
My Question: How do I get the "docker ps" command to run correctly from within the makefile and pass its results correctly into the "docker rm" command, also within the makefile?
Thanks, in advance, for any assistance you can offer.
You need a second $ in your recipe:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
# ^
The single $ is expanded as a makefile variable when the makefile is parsed. It expands to blank. Make therefore passes docker container rm -f to your shell. The second $ sign causes make to expand $$ to $, and it will pass docker container rm $(docker ps -aq) -f to bash, which I'm guessing is what you want.
Notice, if you put the shell in there as #EricMd proposed, it will run a shell command, but that command will be run at Makefile read time, as opposed to the time that the recipe is executed. If the docker ps -aq command is dependent on any other artifacts of your build it would not work.
Sounds like you don't have any containers in docker to remove. I sometimes use a different syntax for this scenario:
remove_all_containers:
docker container ls -aq | xargs --no-run-if-empty docker container rm -f
The xargs syntax will not run docker container rm if there are no containers to delete.
According to the documentation, docker ps -a should list all containers.
You obtained this message "docker container rm" requires at least 1 argument certainly because you forgot to prepend the command at stake with Make's shell builtin:
remove_all_containers:
docker container rm $(shell docker ps -aq) -f
Note also that the docker ps admits a filtering feature: the online doc describes the various flavors of the corresponding -f flag.
For example, below are three Bash alias examples that can be useful to (i) stop all containers, (ii) remove all stopped containers; and (iii) remove dangling images−that would be tagged as <none> when doing docker images ls:
alias docker-stop='docker stop $(docker ps -a -q)'
alias docker-clean='docker rm $(docker ps -a -q -f status=exited)'
alias docker-purge='docker rmi $(docker images -q -f dangling=true)'
I tested for 2 way follow bellow answer:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
remove_all_containers:
docker container rm $(shell docker ps -aq) -f

Stopping Docker containers by image name - Ubuntu

On Ubuntu 14.04 (Trusty Tahr) I'm looking for a way to stop a running container and the only information I have is the image name that was used in the Docker run command.
Is there a command to find all the matching running containers that match that image name and stop them?
If you know the image:tag exact container version
Following issue 8959, a good start would be:
docker ps -a -q --filter="name=<containerName>"
Since name refers to the container and not the image name, you would need to use the more recent Docker 1.9 filter ancestor, mentioned in koekiebox's answer.
docker ps -a -q --filter ancestor=<image-name>
As commented below by kiril, to remove those containers:
stop returns the containers as well.
So chaining stop and rm will do the job:
docker rm $(docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}"))
If you know only the image name (not image:tag)
As Alex Jansen points out in the comments:
The ancestor option does not support wildcard matching.
Alex proposes a solution, but the one I managed to run, when you have multiple containers running from the same image is (in your ~/.bashrc for instance):
dsi() { docker stop $(docker ps -a | awk -v i="^$1.*" '{if($2~i){print$1}}'); }
Then I just call in my bash session (after sourcing ~/.bashrc):
dsi alpine
And any container running from alpine.*:xxx would stop.
Meaning: any image whose name is starting with alpine.
You might need to tweak the awk -v i="^$1.*" if you want ^$1.* to be more precise.
From there, of course:
drmi() { docker rm $(dsi $1 | tr '\n' ' '); }
And a drmi alpine would stop and remove any alpine:xxx container.
The previous answers did not work for me, but this did:
docker stop $(docker ps -q --filter ancestor=<image-name> )
You could start the container setting a container name:
docker run -d --name <container-name> <image-name>
The same image could be used to spin up multiple containers, so this is a good way to start a container. Then you could use this container-name to stop, attach... the container:
docker exec -it <container-name> bash
docker stop <container-name>
docker rm <container-name>
This code will stop all containers with the image centos:6. I couldn't find an easier solution for that.
docker ps | grep centos:6 | awk '{print $1}' | xargs docker stop
Or even shorter:
docker stop $(docker ps -a | grep centos:6 | awk '{print $1}')
Two ways to stop running a container:
1. $docker stop container_ID
2. $docker kill container_ID
You can get running containers using the following command:
$docker ps
Following links for more information:
https://docs.docker.com/engine/reference/commandline/stop/
https://docs.docker.com/v1.8/reference/commandline/kill/
This will only stop all containers with image = "yourImgName" :
sudo docker stop $(sudo docker ps | grep "yourImgName" | cut -d " " -f 1)
This will stop and remove all containers with image = "yourImgName" :
sudo docker rm $(sudo docker stop $(sudo docker ps -a | grep "yourImgName" | cut -d " " -f 1))
I made a /usr/local/bin/docker.stop that takes in the image name (assumes you only have one running).
docker stop $(docker ps -q -f "name=$1")
Stop docker container by image name:
imagename='mydockerimage'
docker stop $(docker ps | awk '{split($2,image,":"); print $1, image[1]}' | awk -v image=$imagename '$2 == image {print $1}')
Stop docker container by image name and tag:
imagename='mydockerimage:latest'
docker stop $(docker ps | awk -v image=$imagename '$2 == image {print $1}')
If you created the image, you can add a label to it and filter running containers by label
docker ps -q --filter "label=image=$image"
Unreliable methods
docker ps -a -q --filter ancestor=<image-name>
does not always work
docker ps -a -q --filter="name=<containerName>"
filters by container name, not image name
docker ps | grep <image-name> | awk '{print $1}'
is problematic since the image name may appear in other columns for other images
list all containers with info and ID
docker ps
docker stop CONTAINER ID
For Docker version 18.09.0
I found that format flag won't be needed
docker rm $(docker stop $(docker ps -a -q -f ancestor=<image-name>))
I was trying to wrap my Docker commands in gulp tasks and realised that you can do the following:
docker stop container-name
docker rm container-name
This might not work for scenarios where you have multiple containers with the same name (if that's possible), but for my use case it was perfect.
In my case --filter ancestor=<image-name> was not working, so the following command cleaned up the Docker container for me:
docker rm $(docker stop $(docker ps -a -q --filter "name=container_name_here" --format="{{.ID}}"))
Adding on top of #VonC superb answer, here is a ZSH function that you can add into your .zshrc file:
dockstop() {
docker rm $(docker stop $(docker ps -a -q --filter ancestor="$1" --format="{{.ID}}"))
}
Then in your command line, simply do dockstop myImageName and it will stop and remove all containers that were started from an image called myImageName.
use: docker container stop $(docker container ls -q --filter ancestor=mongo)
(base) :~ user$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d394144acf3a mongo "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 0.0.0.0:27017->27017/tcp magical_nobel
(base) :~ user$ docker container stop $(docker container ls -q --filter ancestor=mongo)
d394144acf3a
(base) :~ user$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(base) :~ user$
This is my script to rebuild docker container, stop and start it again
docker pull [registry]/[image]:latest
docker build --no-cache -t [localregistry]/[localimagename]:latest -f compose.yaml context/
docker ps --no-trunc | grep [localimagename] | awk '{print $1}' | xargs docker stop
docker run -d -p 8111:80 [localregistry]/[localimagename]:latest
note --no-trunc argument which shows the image name or other info in full lenght in the output
Here's a concise command which doesn't require you to specify the image tag (as most of these answers do):
docker stop $(docker ps -a | awk -v i="^${image_name}.*" '{if($2~i){print$1}}')
docker stop $(docker ps -a | grep "zalenium")
docker rm $(docker ps -a | grep "zalenium")
This should be enough.
If you want to prefer a simple AWK approach, here Is my take:
docker rm -f $(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}')
$(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}') - prints the docker container names based on input image
docker ps - list all containers
awk '{ if($2 == "<your-image-name>") { print $NF}}' - The second parsed column of docker ps gives the image name. Comparing it with your image name will execute print $NF which prints the container name.
docker rm -f removes the containers
For example, removing all running containers of ubuntu image, can be done simply as:
docker rm -f $(docker ps | awk '{ if($2 == "ubuntu:latest") { print $NF}}')
PS: Remember to include the image tag in AWK, since it's a equal comparator.
if you know a part of the container name you can use AWK with docker as following :
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /container.*/) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "No containers available for deletion"
else
docker rm -f $CONTAINER_IDS
fi
image: docker
services:
- docker:dind
stages:
- deploy
step-deploy-prod:
stage: deploy
tags:
- docker
script:
- container_id=$(docker ps -q -a -f status=running --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker stop $container_id;
docker rm -f $container_id;
fi
- container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker rm -f $container_id;
fi
- docker build -t app/creative .
- docker run -d -p 8081:80 --name todoapp app/creative
First, check for a running container with the command docker ps -q -a -f status=running --filter name=todoapp , if it finds one it stops and deletes the running container then check for any containers that are stopped and have the name todoapp using the command docker ps -q -a -f status=exited --filter name=todoapp, then it will remove the container if it's found.
Then it will build a new image and start a new container with the new build image.
As I have found out, if you stop the container, it can't be found with docker rm just incase anyone stumbles across this if you are wanting to replace a newly deployed image via gitlab-ci
There is an option in docker ps command -f status=exited which shows all the containers which are in stopped state.
container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
This command would only return container ids that are stopped and has name todoapp
Also, a better way to remove the stopped container is by using the -f or --force option with the docker rm command. This option will remove the container even if it is in a stopped state.
You can use the ps command to take a look at the running containers:
docker ps -a
From there you should see the name of your container along with the container ID that you're looking for. Here's more information about docker ps.

See full command of running/stopped container in Docker

How can I see the full command of a running container/process in Docker?
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b6291859b61 nginx:1.7.8 "nginx -g 'daemon of 4 minutes ago Exited (0) 4 minutes ago thirsty_brattain
I can only see "nginx -g 'daemon of".. here, not the full command.
docker ps --no-trunc will display the full command along with the other details of the running containers.
Use:
docker inspect -f "{{.Name}} {{.Config.Cmd}}" $(docker ps -a -q)
... it does a "docker inspect" for all containers.
Use:
docker inspect -f "{{.Path}} {{.Args}} ({{.Id}})" $(docker ps -a -q)
That will display the command path and arguments, similar to docker ps.
Moving Dylan's comment into a full-blown answer because TOO USEFUL:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike YOUR-CONTAINER
What does it do? Runs https://github.com/lavie/runlike inside a container, gets you the complete docker run command, then removes the container for you.
TL-DR
docker ps --no-trunc and docker inspect CONTAINER provide the entrypoint executed to start the container, along the command passed to, but that may miss some parts such as ${ANY_VAR} because container environment variables are not printed as resolved.
To overcome that, docker inspect CONTAINER has an advantage because it also allow to retrieve separately env variables and their values defined in the container from the Config.Env property.
docker ps and docker inspect provide information about the executed entrypoint and its command. Often, that is a wrapper entrypoint script (.sh) and not the "real" program started by the container. To get information on that, requesting process information with ps or /proc/1/cmdline help.
1) docker ps --no-trunc
It prints the entrypoint and the command executed for all running containers.
While it prints the command passed to the entrypoint (if we pass that), it doesn't show value of docker env variables (such as $FOO or ${FOO}).
If our containers use env variables, it may be not enough.
For example, run an alpine container :
docker run --name alpine-example -e MY_VAR=/var alpine:latest sh -c 'ls $MY_VAR'
When use docker -ps such as :
docker ps -a --filter name=alpine-example --no-trunc
It prints :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b064a6de6d8417... alpine:latest "sh -c 'ls $MY_VAR'" 2 minutes ago Exited (0) 2 minutes ago alpine-example
We see the command passed to the entrypoint : sh -c 'ls $MY_VAR' but $MY_VAR is indeed not resolved.
2) docker inspect CONTAINER
When we inspect the alpine-example container :
docker inspect alpine-example | grep -4 Cmd
The command is also there but we don't still see the env variable value :
"Cmd": [
"sh",
"-c",
"ls $MY_VAR"
],
In fact, we could not see interpolated variables with these docker commands.
While as a trade-off, we could display separately both command and env variables for a container with docker inspect :
docker inspect alpine-example | grep -4 -E "Cmd|Env"
That prints :
"Env": [
"MY_VAR=/var",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"sh",
"-c",
"ls $MY_VAR"
]
A more docker way would be to use the --format flag of docker inspect that allows to specify JSON attributes to render :
docker inspect --format '{{.Name}} {{.Config.Cmd}} {{ (.Config.Env) }}' alpine-example
That outputs :
/alpine-example [sh -c ls $MY_VAR] [MY_VAR=/var PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]
3) Retrieve the started process from the container itself for running containers
The entrypoint and command executed by docker may be helpful but in some cases, it is not enough because that is "only" a wrapper entrypoint script (.sh) that is responsible to start the real/core process.
For example when I run a Nexus container, the command executed and shown to run the container is "sh -c ${SONATYPE_DIR}/start-nexus-repository-manager.sh".
For PostgreSQL that is "docker-entrypoint.sh postgres".
To get more information, we could execute on a running container
docker exec CONTAINER ps aux.
It may print other processes that may not interest us.
To narrow to the initial process launched by the entrypoint, we could do :
docker exec CONTAINER ps -1
I specify 1 because the process executed by the entrypoint is generally the one with the 1 id.
Without ps, we could still find the information in /proc/1/cmdline (in most of Linux distros but not all). For example :
docker exec CONTAINER cat /proc/1/cmdline | sed -e "s/\x00/ /g"; echo
If we have access to the docker host that started the container, another alternative to get the full command of the process executed by the entrypoint is :
: execute ps -PID where PID is the local process created by the Docker daemon to run the container such as :
ps -$(docker container inspect --format '{{.State.Pid}}' CONTAINER)
User-friendly formatting with docker ps
docker ps --no-trunc is not always easy to read.
Specifying columns to print and in a tabular format may make it better :
docker ps --no-trunc --format "table{{.Names}}\t{{.CreatedAt}}\t{{.Command}}"
Create an alias may help :
alias dps='docker ps --no-trunc --format "table{{.Names}}\t{{.CreatedAt}}\t{{.Command}}"'
Use runlike from git repository https://github.com/lavie/runlike
To install runlike
pip install runlike
As it accept container id as an argument so to extract container id use following command
docker ps -a -q
You are good to use runlike to extract complete docker run command with following command
runlike <docker container ID>

Resources