docker containers stops a very long time - node.js

when Im trying to stop docker container it take a long time, a few min, why?
I use:
sudo aa-remove-unknown
sudo docker stop $(docker ps -a -q)
sudo docker rm $(docker ps -a -q)
sudo docker stop $(docker ps -a -q) -f
sudo docker rm $(docker ps -a -q) -f
Containers restarting every 30 min. and about 3-5 min. takes to killing.

docker stop sends a SIGTERM signal to the container, which will allow your processes to gracefully stop, this can often take time.
There's an excellent blog post from repl.it that dives deep into this topic.

Related

docker logs within a bash script doesn't work

I'm experimenting a weird behaviour of Docker in a bash script.
Let's see these two examples:
logs-are-showed() {
docker rm -f mybash &>/dev/null
docker run -it --rm -d --name mybash bash -c "echo hello; tail -f /dev/null"
docker logs mybash
}
# usage:
# $ localtunnel 8080
localtunnel() {
docker rm -f localtunnel &>/dev/null
docker run -it -d --network host --name localtunnel efrecon/localtunnel --port $1
docker logs localtunnel
}
In the first function logs-are-showed the command docker logs returns me the logs of the container mybash
In the second function localtunnel the command docker logs doesn't return me anything.
After having called the localtunnel function, if I ask for the container logs from outside the script, it shows me the logs correctly.
Why does this happen?
Processes take time to react. They may be no logs right after starting a process - it has not written anything yet. Wait a bit.

Docker container name is allready in use

I can not create a certain docker container because jenkins tells me that the name is allready in use.
docker run -d --name branchtest_container -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro branchtest_image
docker: Error response from daemon: Conflict. The container name "/branchtest_container" is already in use by container "256869981b65b979daf203624b8c0b5a8e475464a647814ff12b32c322844659". You have to remove (or rename) that container to be able to reuse that name.
I allready tried finding or deleting this container, but i am not able to do so:
jenkins#jenkins-slave4oed:~$ docker rm 256869981b65b979daf203624b8c0b5a8e475464a647814ff12b32c322844659
Error response from daemon: No such container: 256869981b65b979daf203624b8c0b5a8e475464a647814ff12b32c322844659
jenkins#jenkins-slave4oed:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
jenkins#jenkins-slave4oed:~$
The container gets build via jenkins, and in different build there is allways the same container id that gets disclaimed as in use. We have eight different jenkins nodes and this job works on seven of them, creating and removing docker images with that name.
What can be done to remove this "ghost" container? Allready tried without success:
systemctl restart docker
docker rm $(docker ps -aq --filter name=branchtest_container)
docker container prune
You can not just remove running container. You need to stop it at first.
To get all containers run:
docker ps -a
To remove container:
docker stop $(docker ps -a -q --filter name=branchtest_container) || true
docker rm -f $(docker ps -a -q --filter name=branchtest_container) || true

`sudo` with command substitution

I'm working with a remote machine that requires sudo for most docker commands. I often have to perform multiple repetitive commands to stop docker containers, etc: sudo docker stop <container_name>.
Normally, I'd use docker stop $(docker ps -q) to stop all the results of docker ps -q, but simply prefixing this with sudo doesn't do what I'm after: sudo docker stop $(docker ps -q) results in a permission denied error.
Is there a way to pipe this through sudo so that I don't have to stop everything individually?
You also need to specify sudo` in the inner command. So the following should work:
sudo docker stop $(sudo docker ps -q)
xargs also should work:
$ sudo docker ps -q | xargs sudo docker stop

Using $() in docker commands doesn't seem to work

I want to stop all running docker containers with the command sudo docker stop $(docker ps -a -q). But when I run it, docker outputs
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Just running docker ps -a -q outputs the Docker IDs, but when I combine it with a Docker command, it doesn't work. Thank you.
I didn't realize that the sudo is required in the command substitution also:
sudo docker stop $(stop docker ps -a -q)
Aren't you trying to run docker ps -a -q and docker stop $(docker ps -a -q) in two different consoles/users? The error shown is in fact two different errors:
docker ps -q -a cannot complete due to insufficient permissions
docker stop ... gets empty argument list due to error in subshell
Edit:
If using sudo each command is running in different shell/subshell which inherits privileges/environment. But the subshells are invoked in order from the outermost. So the script will be invoket in order docker ps and then sudo docker stop. The first one will not have privileges elevated.

Docker Compose - Remove dangling service

While getting to know docker and docker compose, I removed a volume that was still in use by docker compose.
Now, docker compose prints the following error message for everything I try to do (stop, start, ps, rm, ...):
ERROR: Named volume "db_data:/var/lib/mysql:rw" is used in service "db" but no declaration was found in the volumes section.
Therefore, I am now unable to work with docker compose in any way. As I am out of ideas, I am reaching out for some support.
I usually have this bash script at hand:
#!/bin/bash
# Clean containters and images of docker.
#Stop containers
docker stop $(docker ps -a -q)
#Remove docker containers:
docker rm $(docker ps -a -f status=exited -q)
# Remove docker images:
docker rmi -f $(docker images -a -q)
Example source: https://github.com/filfreire/scripts/blob/master/docker-clean-all

Resources