I try to start container with following command
sudo docker run ubuntu
after that I checked with
sudo docker ps -a
found the container exited already
why does it exit?
How could I keep it running in backgroud without specifying -it and attach to it on demanding?
Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.
If there's no terminal attached, then your shell process will exit, and so the container will exit. You can stop this by adding --interactive --tty (or just -it ) to your docker run ... command, which will let you type commands into the shell.
The easiest way to keep the container running is to change its entry point to a command that will continue running forever. Let's use tail -f /dev/null . Rechecking the running container via docker ps -a we can see that our container has been up and running for several seconds and hasn't stopped yet.
Use this command inside you Dockerfile to keep the container running in your K8s cluster: CMD tail -f /dev/null.
Solved by myself, a elegant way to keep the container running and waiting for further "attach" or "exec" is the following (to keep the STDIN open by -i option)
docker run -i -d ubuntu
You need to start an application with the docker run command that won't exit.
Example:
docker run -d --entrypoint '/bin/bash cat' ubuntu
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With