Kill a docker container using awk command in gitlab-ci.yml - docker

I would like to use awk in gitlab-ci.yml to kill a docker container. However, awk does not work as expected.
For example, I want to kill a docker container called ADockerContainer using awk. Therefore I use the following command:
docker kill $(docker ps | grep ADockerContainer | awk '{print $1}')
After the execution of the command, I get:
"docker kill" requires at least 1 argument.
Does anyone know how to fix this?

docker kill (and other commands) will take the container name directly, so you don't need any sort of command substitution here. It's enough to run
docker kill AContainerName

Related

'Docker cp' with parameter expansion (PowerShell)

I want to copy a directory into a docker container, which is only defined by a certain query. I tried this:
docker cp ../from-here/. $(docker ps -aqf "name=my-ending$"):/to-here
Unforunately this throws this error:
"docker cp" requires exactly 2 arguments.
Running the same command with pasting the real Container ID works.
It seems, that PowerShell in combination with Docker doesn't allow parameter expansion.
Is there an easy work around for this problem in a single line?
in power shell you need to use | (pipe) to continue commands
docker cp ../from-here/. | % {docker ps -aqf "name=my-ending$"}:/to-here
This works:
docker #("cp", "../from-here/.", "$(docker ps -aqf "name=my-ending$"):/to-here")

How can stop group of containers with regex in docker-compose

Question:
How can I stop containers that their names start with server-?
Containers
> sudo docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------------------------
server-myservername1_1 some commands Up
server-myservername2_1 some commands Up
server-myservername3_1 some commands Up
server-myservername4_1 some commands Up
server-myservername5_1 some commands Up
server-myservername6_1 some commands Up
console-myconsolename1_1 some commands Up
console-myconsolename2_1 some commands Up
First check the output of below command if it's gives the names of only those containers that you have to stop
docker-compose ps | grep server | awk '{print $1}'
If the list is right, then run
docker stop $(docker-compose ps | grep server | awk '{print $1}')
P.S. I haven't tested the above command. Let me know if it doesn't
You can simply use --filter option of ps command
Suppose you wanna look for 3 containers, which their names start with site
docker ps --filter name=site*
will show you stat of those three containers.
so use one of this to stop the containers
- docker ps --filter name=site* -aq | xargs docker stop
- docker stop $(docker ps --filter name=site* -aq)

Stop docker containers with name matching a pattern

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

Shell command to Get container id from "docker ps"

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

How to kill multiple Docker containers of an image

Once in a while I bash into my containers to manually run some scripts on my servers and sometimes my session times out and those containers stay up.
I'm trying to run a command to kill all previous running containers of that image. Let's say my image is called "WEB" and this is what docker ps is showing:
ID NAMES
1 project_web_1
2 project_web_2
3 project_web_3
I want to kill all of those with a single command. I have tried this
docker rm -f $(docker ps -aqf "name=web") but this only kills the first one. Is there a way to kill all of them?
It seems my command actually removes all of them
docker rm -f $(docker ps -aqf "name=web")
I just had a typo in my image name. I leave this question/answer in case someone in the future needs it.
docker container rm -f $(docker container ps | awk '/yourname/ {print $1}')
Replace yourname with the name or anything you want to match from docker ps.

Resources