I want to use a debian Docker container to test something, and by this I mean execute some commands in the debian bash console. I tried downloading the image using docker pull debian
and then running it using docker run debian
, but I get no output. What am I doing wrong? Shouldn't the docker container stay open until I close it?
Dockerfile Command to Keep the Container Running 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.
According to this answer, adding the -t flag will prevent the container from exiting when running in the background. You can then use docker exec -i -t <image> /bin/bash to get into a shell prompt.
Press Ctrl-P, followed by Ctrl-Q, to detach from your connection. You'll be dropped back into your shell but the previously attached process will remain alive, keeping your container running.
Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. docker run -d -t ubuntu Method 2: You can run the container directly passing the tail command via arguments as shown below. docker run -d ubuntu tail -f /dev/null
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
(Obviously this is for dev purposes only, you shouldn't need to keep a container alive unless it's running a process eg. nginx...) Show activity on this post. I just had the same problem and I found out that if you are running your container with the -t and -d flag, it keeps running. Here is what the flags do (according to docker run --help ):
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.
You need to explicitly run bash:
docker run -it debian /bin/bash
The -i
means "run interactively", and -t
means "allocate a pseudo-tty".
A good place to read a bit more is the section Running an interactive shell in the Quickstart documentation.
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