How does -t flag keep a container running? [duplicate] - docker

This question already has answers here:
Confused about Docker -t option to Allocate a pseudo-TTY
(9 answers)
Closed 1 year ago.
When I run a busybox container with '-t' flag, it remains in "Running" state but without the "-t" flag, the container goes to "exit" state.
How does -t flag effect container state?
$ docker run -d --name mybzy busybox -> container exits
$ docker run -dt --name mybzy1 busybox -> container keeps running

The default CMD of busybox is running a shell. Running a docker container with -t means connecting it to the terminal. A shell prompts the user for input if its connected to a shell (and only if).
When running without -t, the container is not connected to your terminal, and the shell program just exits.

Related

What is the difference between "docker run my_image" and "docker run -it my_image" [duplicate]

This question already has answers here:
what is docker run -it flag?
(3 answers)
Closed 12 days ago.
When I containerize my image with "docker run my_image" and "docker run -it my_image"., both works same, but what is the difference ?
The docker run command is used to start a new container from an image. The difference between docker run my_image and docker run -it my_image is in the way they interact with the container.
docker run my_image runs the container in the background, detached from the terminal. The container will continue to run in the background until it is stopped or exits.
docker run -it my_image runs the container in the foreground, attached to the terminal. The -it option allows you to interact with the container using the terminal. This is useful for running commands within the container and observing the output in real-time.
In summary, docker run my_image is used to run a container in the background without interaction, while docker run -it my_image is used to run a container in the foreground and interact with it using the terminal.

docker:when i use only --tty or -t (without -i) for the sake of colorful output along with --rm, the docker does not stop on ctrl+c

Currently i am using docker for development of a django project.
I want to see the output of the commands in color.
I found in this forum https://forums.docker.com/t/docker-run-with-colorful-output/24542/3 that i have to use --tty or -t
So the following is my command
hostfolder="/home/web_dev/Docker_django_testing"
dockerfolder="/home/simha/app"
imagename="django_testing"
docker run -t -p 8000:8000 --rm -v ${hostfolder}:${dockerfolder} ${imagename} pipenv run python django_project_test/manage.py runserver 0.0.0.0:8000
I am using --rm to stop and remove the container on exit. But because of -t the container does not get stopped even after exiting using Ctrl + c
Here is the image showing the output of the above command
And after exiting i checked the output of the docker ps -a it shows the container is still running.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
212c039d35ae django_testing "pipenv run python d…" 4 minutes ago Up 4 minutes 0.0.0.0:8000->8000/tcp nervous_euclid
Where as the same command if i run without -t eventhough it does not show up color output, the container gets stopped and removed on exit by using Ctrl + c
The below image shows the output
If you use -t without -i your stdin is not connected to the program's stdin (in this case pipenv run ...). This means that when you press Control C, this is passed to the docker run command and therefore docker run exits, but it's not passed to the container's main process and therefore the container keeps running. In short, you need the -i option too:
docker run -it -p 8000:8000 --rm -v ${hostfolder}:${dockerfolder} ${imagename} pipenv run python django_project_test/manage.py runserver 0.0.0.0:8000

Inspect docker container at container startup [duplicate]

This question already has answers here:
Explore Docker's image files if container exits immediately?
(3 answers)
Closed 3 years ago.
I downloaded an image which immediately stops. How can I inspect it (or any container spawned from it)?
I cannot use anything like docker exec -it CONTAINER_ID bash since I don't have time to get the CONTAINER_ID.
(docker run -it 5413e661e579 bash does not help, it starts the container and stops immediatly.)
I don't know how the image was built, I don't have the Dockerfile ; the only thing I know is the entrypoint: ["python" "app.py"] but it does not output anything useful.
Answer from duplicate question:
docker run -it --entrypoint "/bin/bash" image_name
You can get the container id by passing in the --all flag to docker container ls
docker container ls --all
This will list all containers, including those that have been stopped or exited. Then, once you have the container id, inspect the logs with the docker logs command
docker logs <container>

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

Resources