unable to automatically stop container running jupyter notebook - docker

If I start a container with --rm like so:
docker run --rm -it image-name /bin/bash
Then from the repl I can close it exit and it automatically removes the container, so I don't have to.
It's very nice.
But now I'm starting a container and telling it to automatically run jupyter notebook like so:
docker run --rm -it image-name /bin/bash -c "jupyter notebook ..."
In this case, when I press control+c I exit the container, but it doesn't shut down the container, because jupyter notebook is still running. Then I have to go in and docker stop container-id and then docker rm container-id which is annoying.
Is there any way to tell the container to automatically close when I exit it?

So the reason the container removes itself in the first scenario is because the "bash" script is what your execution was tied to. When you exit, BASH exits, so the container is removed.
In the second scenario, you're not actually doing that. You're executing a command interactively via bash's command function, and you exiting the terminal isn't tied to the termination of that command.
The way the Docker --rm flag works is that, once the command requested by the execution statement ends, remove the container". If you execute bash to get an interactive shell, and then run the command, you'll get a different experience.

Related

Command docker start -a <Container ID> do nothing, "Ubuntu 20.04.02 LTS"

i'm trying to run the start command in docker after creating a container.
for example :
$ docker create busybox echo hi there
it gives me the id of that container like 4e59d0fe8584bb4dcaf44dbce100253b6767bf51546edc27f29f39f52ed57957
and when i try to start that container without any flags like: -a flag
it works, but it only gives me that id back again.
but when i try to show the output using the attach -a flag, actually nothing happened, even it didn't give me an error, simply the command still running without anything happened.
i also couldn't kill the command and stop the execution by Ctrl+c, so the only option i had to close the terminal
i tried to make the problem clear as possible as i can
You can run the image via this command:
docker run -it busybox
It goes you to shell environment and you have -i (interactive) -t (tty) terminal, which means you see the terminal.
The default CMD(PID1) for busybox image is sh, see this.
For docker create busybox echo hi there, the COMMAND becomes echo hi there. This means after container starts, it will first execute echo hi there, then as PID1 exit, the container exit too. If you use docker ps, you won't find your container, you could just find your exited container with docker ps -a.
So,
If you intend to run a one time task, then as the container finish its task, it's normal you could not enter into container anymore.
If you intend to run a daemon task, leave the container service there, you should choose a command which won't finish after run, then your container will still there.
For your case, to quick let you understand it, you could use next to have a quick understanding, use tail -f /dev/null to let the container not exit:
# docker create busybox sh -c "echo hi there; tail -f /dev/null"
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root#shlava:~# docker start 840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root#shlava:~# docker logs 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26
hi there
root#shlava:~# docker exec -it 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26 /bin/sh
/ #

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 can I keep docker container running?

I want to run multiple containers automatically and create something,
but some images, such as swarm, will automatically stop after run or start.
I already try like that
docker run -d swarm
docker run -d swarm /bin/bash tail -f /dev/null
docker run -itd swarm bash -c "while true; do sleep 1; done"
but 'docker ps' show nothing, and I tried to build Dockerfile by typing:
FROM swarm
ENTRYPOINT ["echo"]
and The image does not run with error message :
docker: Error response from daemon: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"exec: \\\"echo\\\": executable file not found in $PATH\"\n".
I can't understand this error... How can I keep swarm container running..?
(Sorry,my English is not good))
using -d is recommended because you can run your container with just one command and you don’t need to detach terminal of container by hitting Ctrl + P + Q.
However, there is a problem with -d option. Your container immediately stops unless the commands are not running on foreground.
Docker requires your command to keep running in the foreground. Otherwise, it thinks that your applications stops and shutdown the container.
The problem is that some application does not run in the foreground.
In this situation, you can add tail -f /dev/null to your command.
By doing this, even if your main command runs in the background, your container doesn’t stop because tail is keep running in the foreground.
docker run -d swarm tail -f /dev/null
docker ps shows container
Now you can attach your container by using docker exec container_name command
or
docker run -d swarm command tail -f /dev/null
First of all you don't want to mix the -i and -d switches. Either you would like to run the container in interactive or detached mode. In your case in detached mode:
docker run -d swarm /bin/bash tail -f /dev/null
There are also no need to allocate a tty using the -t flag, since this only needs to be done in interactive mode.
You should have a look at the Docker run reference
Docker container does two type of task. One is to perform and exit & other is to run it in background.
To run docker container in background, there are few options.
Run using shell. docker run -it <image> /bin/bash
For continuously running container. docker run -d -p 8080:8080 <image>. Assuming image will expose port 8080 and in listening mode.
It's fine to to a tail on /dev/null, but why not make it do something useful?
The following command will reap orphan processes, so no zombie (defunct) precesses are left floating around. Also some init.d / restart scripts doesn't allow this.
exec sh -c 'while true ;do wait ;done'
You are right docker run -itd swarm ( Without give argument for container( bash -c "while true; do sleep 1; done" ) )works fine .If you pass argument for docker run it will run the command and terminates the container.If you want to run the container permanently first start the container with docker run -itd swarm and check if the container runs or not by docker ps now the container runs , if you want to execute any command in container use docker exec -itd container_name command Remember : Only use the command which not stop the container. bash -c "while true; do sleep 1; done this command will stop the container ( Because this is complete command if we execute in normal terminal it execute and terminate , this type of command also terminates the container ).
I Hope this Helps..
Basically this is the method , but your docker image is swarm so it is different and i don't know about swarm docker image and i am not using that .But after i research about that . First i run the docker swarm image it shows.,
After that i understand we run docker swarm image by using only five commands in picture like create, list manage, join, help . if we run swarm image without command like docker run -itd swarm it takes command as --help . Sorry, But i don't know what is the purpose of swarm image. For more usage check https://hub.docker.com/_/swarm/ .
The answer that i added docker run -itd image tail -f /dev/null is not for swarm image , it is for docker images like ubuntu,fedora, centos.
Just read the usage of swarm image and why it is used .
After if you have any issue post your issue in https://github.com/docker/swarm-library-image/issues
Thank You...
have a container running
docker run --rm -d --name=tmp ubuntu sleep infinity
example of requesting a command from the dorment container
docker exec tmp echo hello from container
notes:
--rm removes the container if it is stopped
-d runs the container in the background
--name=tmp name the container so you control how to denote it
ubuntu pushes a light image to exec your commands
sleep infinity keeps the container dorment

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

getting a ptty for docker start

Docker's run and exec command both allow the -t flag to get a psuedo-terminal. This works nicely for something like: docker run -it --name deb debian bash. Once the user exits the interactive bash shell in this container, the container stops. While the exec command could similarly take the same flag and would work well doing so, the stopped container may not have anything executed within unless it is started.
The start command does not take the -t flag. Using just -ai with say, docker start -ai deb would run bash again but fail to show an interactive prompt, making use kind of... cumbersome. Without -ai the bash process would just exit, making the container stop right after starting.
While this sub-optimal attached, interactive, bash is open, ^z will not background the current docker command. If however you open a new terminal, setup your docker environment variables correctly, and now, finally, issue docker exec -it deb bash, you can get back to the nicer interactive bash prompt when you first started the container.
This seems quite involved. Am I missing something about start or exec that might make it easier to use bash or another interactive command? Perhaps there is some preferred command to run that will never exit, keep the container running (until told to stop) and use very little processor time so as to make exec the preferred method of attaching to an interactive process in the container.

Resources