Site icon Roel Peters

Prevent Docker from exiting Ubuntu container immediately

In this article, you’ll learn about keeping a Docker container alive, so that it doesn’t exit. We’ll use Ubuntu as an example.

When you use docker run, a process is ran in an isolated container. This means that, when you run an a container with a Ubuntu image, the operating systems boots and finishes when completed.

In other words, to ensure that the container doesn’t exit, you need to have some kind of process running in the foreground inside the container. For Ubuntu, most of the time, that means that you want to have access to the terminal.

The easiest way to do that, is by specifying a combination of arguments with the docker run command.

To run an Ubuntu image in a container with STDIN connected to a pseudo-terminal.

docker run --name my_container --it ubuntu

Now, you can send your Linux commands to the container. Good to know is that you can detach from the container by using the following hotkey (Windows): CTRL+P followed CTRL+Q.

You can reattach to the container — and the STDIN — by running the following command:

docker attach my_container

Great success!

Exit mobile version