Docker Toolbox - hang on `docker push` - docker

I am using Docker Toolbox on Mac.
docker push is hanging. How do I hard restart the daemon or docker-machine VM to get this unhung in a bad manner. It is taking too long to wait for it.

You can just run:
docker-machine stop <name-of-your-docker-machine>
which is usually:
docker-machine stop default
next option:
docker-machine kill default
Did you check that your docker machine is running:
docker-machine ls
Brute force approach:
kill -9 `ps -Af | grep -v grep | grep VBoxHeadless | awk '{print $2}'`

Related

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

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

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?

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

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)

Resources