docker stop and remove container - docker

This is a very basic question. One can stop and remove a docker container like this.
docker stop <container_id>
docker rm <container_id>
Is it the same as this?
docker rm -f <container_id>
If they are not the same, what are the negative effects of using the latter?
I already saw single command to stop and remove docker container. But it doesn't answer my question.

They are similar you can check the man pages:
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
-f, --force Force the removal of a running container (uses SIGKILL)
and
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop a running container.
Sending SIGTERM and then SIGKILL after a grace period
--help Print usage
-t, --time=10 Seconds to wait for stop before killing it
I would say
docker stop <container_id>
docker rm <container_id>
would be a little more "graceful" since it sends first a SIGTERM prior to SIGKILL this would give docker a chance to clean things up.

Related

Docker command that purges everything

Is there a single docker command that can be used to purge everything? Stop all containers if running, delete all images, delete all volumes... ect.
I don't think there is a single command to do that. You first need to stop all containers using
$ docker stop `docker ps -qa` > /dev/null 2>&1; ## Stop all running containers
$ docker system prune --volumes --all; ## Remove all unused docker components
Usage on prune can be found in docker official documentation.
You can still run the above 2 commands in a single line.
$ docker stop `docker ps -qa` > /dev/null 2>&1; docker system prune --volumes --all;
Or an alias should help
alias dockerRemoveAll="docker stop `docker ps -qa` > /dev/null 2>&1; docker system prune --volumes --all;"
Edit-1:
Based on #Zeitounator's comment, I've added > /dev/null 2>&1 to redirect whatever the output is to null and stderr. This still continues to remove all the docker contents even when the exit code is 1 for the stop command when there are no containers.
Also #Zeitounator's wipedocker function is even more elegant.

Docker not killing containers

I'm trying to kill my docker containers with the command:
$ docker container kill $(docker ps -q)
however, the containers aren't responding.
I'm able to log into them with:
$ docker exec -it container_id bash
but any commands within the terminal hangs.
Whats interesting is somehow the process also doesn't seem to exist. I get the list of running containers with the process ids as so:
$ for i in $(docker container ls --format "{{.ID}}"); do docker inspect -f '{{.State.Pid}} {{.Name}}' $i; done
12821 /brave_carson
12661 /trusting_hoover0
12617 /peaceful_franklin
12534 /frosty_volhard
12702 /zealous_sammet
12678 /flamboyant_jang
12690 /dreamy_driscoll
When I try to kill it with kill -9 pid I get the error:
$ kill -9 12821
-bash: kill: (12821) - No such process
This is very unusual. How do I resolve this? I'd prefer not to restart docker unless it is the last and only option.
If you kill the container, you cannot exec into it. The container must be running for exec to work (you should get an error message from this).
When the container is not running, there should be no process. However the container definition in docker, including logs, and changes to the container filesystem, will remain until you remove it with docker container rm (same as docker rm), e.g.:
docker container rm brave_carson
As a side note you can use docker run ... --rm ... to automatically remove containers after stop

What is the difference between "docker container prune" vs "docker rm $(docker container ls -aq)"

I'm reading through the Docker documentation and I don't understand the difference between:
docker container prune
and
docker rm $(docker container ls -aq)
Note that in the link, the second command I've listed is docker rm $(docker ps -a -q), but there is no difference between that and what I've written. container ls is just the newer version of the ps command.
It seems that both of these commands remove all stopped containers. Is there more to it than that, or are these just synonyms?
I don't think there is substantial difference. This -a though means list all containers and as a result docker rm ... will also try to remove running containers. This gives the error that you see below:
Error response from daemon: You cannot remove a running container [...] Stop the container before attempting removal or force remove
example:
$ docker container run --rm -itd alpine:latest
0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ec4d7459d35 alpine:latest "/bin/sh" 4 seconds ago Up 1 second jovial_ritchie
$ docker rm $(docker container ls -aq)
Error response from daemon: You cannot remove a running container 0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4. Stop the container before attempting removal or force remove
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
But... difference when --force, -f is used:
In this case, the commands do 2 different things:
docker rm -f ... Forces the removal of a running container (uses SIGKILL) which means that it will remove running containers.
$ docker rm -f $(docker container ls -aq)
0ec4d7459d35
docker container prune -f will remove all stopped containers without asking for confirmation (no [y/N] prompt will be printed).
$ docker container prune -f
Total reclaimed space: 0B
The effects of the two commands are indeed similar, but there are some nuances to consider:
docker container prune can be used with the --filter option.
docker container prune has a synchronous protection that blocks concurrent prune executions on the daemon.
docker container prune attempts to remove only the containers that are not running, instead of trying to delete all containers and relying on the daemon to throw an exception for those that are not stopped, therefore is quicker and does not generate unnecessary error logs in case someone is tracking the daemon logs.
docker container prune builds a report at the end of its execution, providing the reclaimed space. The report is added in daemon.EventsService and implicitly displayed on the screen.
docker container prune is shorter
In the end of this answer I have a question: Why would someone type 15 additional characters to get the same result or worse?
docker system prune -f : to remove all the stopped containers (docker do not touch the running containers)
docker system prune -a : to remove all the stopped containers (docker do not touch the running containers) + unused images
docker rm <container_id> : remove a specific container, it should be stopped before (docker stop <container_id>)

What is the meaning of running container?

'docker exec' can only used on running container, but what is the meaning of running container? Is that means the container should be computing something? or is the issue about the [command] which I define for the container? Why my TensorFlow container always be stopped status?
After I used 'docker run' to build a tensorflow container, the container stopped automatically. I need to restart it and then execute command on it. Why the container cannot be always in running since I build it?
docker run -it --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3
It will then pops up a bash which I can use to control the container. But after I exit, the container stopped itself. Which means, I can only use docker ps -a to see my container but docker pscan not. I have to restart the container if I want to use my container again.
UPDATE1: If I want create a container like VM, I cannot use docker run with a temporal [command] like python ... The container will lose control permanently after the command finished. docker restart cannot start the container again. Hence, docker exec cannot apply on it. Instead, using bash or nothing as the [command] can create a container which can be restart, therefore, can be applied withdocker exec.
UPDATE2: docker run -d -it can create a running container (but the bash shell won't pops up, neither even with bash). Directly using docker exec -it container_name bash can take the control of the running container again, without docker restart. In this time, exiting bash shell will not stop the container.
A container is running when there is an active process running inside it.
When you are running this tensorflow container, it will exit due to there being no running process
If you were to run
docker run -it --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3 bash
or
docker run -it --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3 python <python script name>
then the container would run the bash/python script as a process and therefore remain up whilst that process is running
View running processes with:
docker ps
See all containers (including stopped/exited tasks) with:
docker ps -a
The difference between docker ps -a and docker ps is exactly what you are looking for:
From the documentation:
--all , -a Show all containers (default shows just running)
So
docker ps gives you only running container
docker ps -a also shows you the stopped ones
So probably, if you expect you container to be long running, (like it would for a web server), then indeed your container command could have an issue and is not keeping your container alive.
Also mind that, if you run your container with the options -ti, like you did, you get an interactive tty attached to it.
--tty , -t Allocate a pseudo-TTY
--interactive , -i Keep STDIN open even if not attached
That basically means that, as soon as you exit that interactive context, your container will shut down.
Running it in a detached mode, with the options -d, is maybe what you are looking for
docker run -d --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3
Related documentation:
--detach , -d Run container in background and print container ID
https://docs.docker.com/engine/reference/commandline/run/

docker run in detached mode - basic question

I run it in detached mode with,
docker run -d busybox:1.24
But it does not show up in docker ps
what is the reason? Should not it be working?
Should i have to pass "running command" like sleep 1000?
EDIT: Seems like the container stops when there is nothing to run.
When you run docker ps, you will only see a list of containers that are running.
To see all containers, including ones that are stopped, created, exited, restarting etc, you should use docker ps -a.
The busybox container isn’t running anything. So it will just exit as soon as it starts.
If you do docker run -d busybox:1.24 sleep 10, then run docker ps, you will see the running container - until the sleep process exits (after 10 seconds). At which point, you’ll need to use docker ps -a again.
Depends on what the CMD directives says in your Dockerfile. If you dont run a script or program that is running continuous the the container will simple end right away.
To see the status of the container: -a shows even exited containers
docker ps -a
To see what happend when it ran
docker logs <container-id>
To run a cmd prompt
docker run -it <container-id> /bin/bash
To run in detached mode you need a script or command that will wait eg:
sleep infinite

Resources