Skip to content
Home ยป Prevent Docker from exiting Ubuntu container immediately

Prevent Docker from exiting Ubuntu container immediately

Tags:

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.

  • The -i argument will keep the STDIN open, and the container will not exit automatically. The default STDIN is the keyboard.
  • The -t argument will create a TTY or pseudo-terminal and connect it to the STDIN of the docker container, allowing you to run commands inside the container, from within your local computer’s terminal.

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!

1 thought on “Prevent Docker from exiting Ubuntu container immediately”

Leave a Reply

Your email address will not be published. Required fields are marked *