'Docker cp' with parameter expansion (PowerShell) - docker

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

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.

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

Stop all docker containers at once on Windows

How can I stop all docker containers running on Windows?
docker stop is for 1 container only.
Any command/script to make it stop all containers?
You could create a batch-file (.bat or .cmd) with these commands in it:
#ECHO OFF
FOR /f "tokens=*" %%i IN ('docker ps -q') DO docker stop %%i
If you want to run this command directly in the console, replace %%i with %i, like:
FOR /f "tokens=*" %i IN ('docker ps -q') DO docker stop %i
In Git Bash or Bash for Windows you can use this Linux command:
docker stop $(docker ps -q)
Note: this will fail if there are no containers running
For PowerShell, the command is very similar to the Linux one:
docker ps -q | % { docker stop $_ }
For those who are interested this can be accomplished in Powershell using
docker ps -q | % { docker stop $_ }
In PowerShell, you could also use this syntax
docker container stop $(docker container list -q)
If the motivation of the question is to recover the memory occupied by Docker (in my case, this was why I arrived at this page), I found that the only way was to stop Docker Desktop completely. You do that by right-clicking the whale icon in the notification area (bottom right) > Quit Docker Desktop.
When you restart Docker Desktop, all the containers reappear, and Docker even sets them to up again automatically.
My two cents.
If you want to stop them filtered by some criteria
docker ps -a -q --filter "name=container_name" --format="{{.ID}}" | ForEach-Object -Process {docker stop $_ }
or if you want to stop and remove them all together
docker ps -a -q --filter "name=container_name" --format="{{.ID}}" | ForEach-Object -Process {docker rm $_ -f}
By using pipe and foreach I avoid the error returned when there are no containers of this kind on the specific machine because docker stop or docker rm require at least one argument.
This script is used with combination of
docker container run image_tag --name=container_name
in order to use the filter later on when you want to stop and remove the containers.
docker stop $(docker ps -aq)
to stop all running containers.
docker rm $(docker ps -aq)
to delete all containers.

sh: grep: command not found

I'm executing a command docker ps -a | grep <imagename> from another container.
It shows
sh: grep: command not found
sh: docker: command not found
Can anyone support me on it?
When you create image1, depending on its base image:
it might not have docker installed in it (see "How to install docker in docker container?")
it might not have grep installed (depending on your base image, or your $PATH defined in that image )
So start by checking those two factors.
If you are searching for an image name, you can try:
docker ps --filter ancestor=image_name
If you are searching for a container name, you can try:
docker ps --filter "name=container_name"
But the fellas are right, if you don't have docker installed on that container, you won't be able to execute the docker ps command.

Resources