While running a docker container in detached mode d
maybe you have experienced exiting the container immediately. Here I’m going to show you some ways how to keep it running in the background using:
Dockerfile
pseudo-tty
tail
Prerequisites
- Docker
- sudo privileges
Using Dockerfile
If you have a Dockerfile you can set ENTRYPOINT
that will keep the container running without getting terminated.
FROM centos:latest
ENTRYPOINT ["tail", "-f", "/dev/null"]
Using pseudo-tty
Using the recent docker versions have authorized to run a container with pseudo-tty
, so you can avoid the container being terminated.
sudo docker run -t -d centos
Using tail command
Last but not least, this is the way that I prefer and I’m using it a lot, directly passing the tail command within the docker run
command:
sudo docker run -d centos tail -f /dev/null
Conclusion
If you want to add another useful way don’t hesitate to put a comment below. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.