Docker: Run a Container Indefinitely

Sometimes, using Docker is a convenient way to build or test our applications that must be run on a Linux environment from Windows. These are many ways to run a container forever, including the tail, sleep, ping, for... and while... loops. In this post, I will mainly focus on the second example, with a trick to quickly shut down and delete the container.

Example 1

# in Windows host, start Ubuntu 20.04 and run it forever
docker run --rm --name ub_demo ubuntu:20.04 tail -f /dev/null
# in container
exit
# in host (Windows)
docker stop ub_demo

Example 2

# in Windows host, start Ubuntu 20.04 and run it forever
docker run --rm --name ub_demo ubuntu:20.04 /bin/bash -c "touch /alive.txt && while [ -f /alive.txt ]; do sleep 1; done"

Thank you, Google Bard, for helping me correct grammar and typos!

Tags:
#docker