Why does 'docker stop CONTAINERID' also removes my stopped container? - docker

If I run docker stop CONTAINERID, docker also deletes my stopped container, so I cannot restart it afterwards. Is there a way to avoid that?
As a note, I ran the container doing docker run --rm -dit --name somename someimages:v1.2.3 and Docker version is 20.10.

From the docker documentation for run you can read:
By default a container’s file system persists even after the container exits. This makes debugging a lot easier (since you can inspect the final state) and you retain all your data by default. But if you are running short-term foreground processes, these container file systems can really pile up. If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the --rm flag:
So run the container without --rm

Remove --rm from your docker run command because of --rm argument docker is removing your container when you stop your container.
Correct Docker run command -
docker run -dit --name somename someimages:v1.2.3

Related

What is the difference between "docker run -it" versus docker run without --detach?

I heard that in case of no --detach in docker run option my terminal is attach to the container, is it this the same as attaching terminal with docker run -it options? What is the difference?
You can start a docker container in detached mode with a -d option. So the container starts up and run in background. That means, you start up the container and could use the console after startup for other commands.
This example runs a container named test using the debian:latest image. The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container.
docker run --name test -it debian

docker container name sometimes already in use after stop

I have a Jenkins job that replaces a docker container with the latest image overnight. Usually this works but occasionally this fails with the error:
docker: Error response from daemon: Conflict. The container name "/demo-api" is already in use by container
The Jenkins job uses the following:
docker stop demo-api
./api_container.sh
api_container.sh does a docker pull and docker run --name demo-api -t -d --rm.
However when I ssh on in the morning after a failure and run docker ps the container is no longer running so looks like it does stop eventually, just not in time for the docker run command that tries to start it with the new image.
Questions
Does the docker stop command not block until it returns?
Should I handle this differently in my Jenkins job script?
I've seen there's also a docker wait command. Should I be using that too in my script?
Pretty sure you have a race condition here. Stop will return before the --rm takes effect. So it's a race between the --rm handled by the engine and the api_container.sh script.
I'd use an explicit docker rm to avoid the race. Note the docker rm may fail depending on where --rm is in its processing, and I'd handle that with a short sleep just to be sure it's done.
docker stop demo-api
docker rm demo-api || sleep 5
./api_container.sh
Or you can switch to a docker rm -f which will kill and delete the container in one step. Probably what you really want, and less error prone, but can leave volumes in a bad state if the app dies ungracefully.
docker rm -f demo-api
./api_container.sh
docker stop will stop your container.
All the stopped container can be found in this command.
docker ps --filter "status=exited
The error you got says The container name "/demo-api" is already in use by container
It means there is already a container using demo-api name, which is true since stopping a docker container will not remove that container and that container name will exists.
All you have to do is
either
Run docker run command without specifying --name option which gives name to your container demo-api. So that each time your script pulls and run the container it will get a new random container name.
OR
If you want to keep your container name same demo-api then rather than stopping the container using docker stop Just remove the container all together docker rm -f demo-api
UPDATE
I just saw you updated your question.
Stopping a container which was ran using --rm option should remove that
container all together.
The error you got seems to only exists when the name is already in-use by another container.
So far you can try to run your script in a while loop and check if that occurs during this run.
Here is the script I used (but didn't got any issue), try this on your particular machine where this issue occurs.
#!/bin/bash
i=20
while [ $i -gt 0 ]
do
docker stop demo-api
docker pull alpine
docker run --name demo-api -t -d --rm alpine sh
i=$((i - 1))
done

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/

How do I stop a docker container so it will rerun with the same command?

I start my docker container with a name. like this:
docker run --name regsvc my-registrationservice
If I call docker stop regsvc, the next time I run the above command it fails. I have to run these commands first.
docker kill regsvc
docker system prune
That seems excessive. Is there a better way to stop the container and restart it?
Thnx
Matt
When you stop a container, you can still see it with:
docker ps -a
Now the container is not alive but it is still there. So, you only need to restart it if you want it to work again:
docker restart regsvc
The command docker run will create a container from your image. So if you want to use docker run again, you need firstly remove your container (after stop it):
docker rm regsvc
docker run --name regsvc my-registrationservice
I believe you want to run a new container every time you issue docker run and it would be better for you to use --rm flag:
docker run --rm --name regsvc my-registrationservice
This will remove the container when your container exits. This is better if you don't want to save data of container.
As suggested by #trong-lam-phan you could restart your existing container using
docker restart regsvc

Docker container not running after creating with mounted volume

I am trying to use an image that I pulled from the docker database. However I need data from the host to use some programs loaded into the image. I created a container with this
sudo docker run --name="mdrap" -v "/home/ubuntu/profile/reads/SE:/usr/local/src/volume" sigenae/drap
it appears that everything works and then I start the container
sudo docker start mdrap
but when I check the running containers it is not listed there and if I try to load the container into /bin/bash it tells me the container is not running. I am a beginner with docker and am only trying to use an image to run programs with all the required dependencies, what am I doing wrong?
docker start is only to start a stopped container. It's not necessary after a docker run. (but more after a docker **create**, like in the documentation)
A container is started as long as it's main process is running.
As soon as the main process stops, the container stops.
The main process of a container can be either:
the ENTRYPOINT if defined
the CMD if no ENTRYPOINT and no command line argument
the command line argument
In your case, as you don't have any command line argument (after the image name on the docker run command) and the image only defines a CMD (=/bin/bash), your container is trying to start a /bin/bash.
But, as you don't launch the container with the --interactive/-i nor --tty/-t (again like in the documentation), your process as nothing to interact with and stops (idem for each start of this container).
So your solution is simply to follow the documentation:
docker create --name drap --privileged -v /home/ubuntu/profile/reads/SE:/usr/local/src/volume -i -t sigenae/drap /bin/bash
docker start drap
docker exec -i -t drap /bin/bash
Or even simpler:
docker run --name drap --privileged -v /home/ubuntu/profile/reads/SE:/usr/local/src/volume -i -t sigenae/drap /bin/bash

Resources