1. Overview

While working with a docker container, we often need to run it in an interactive mode. This is where we attach the standard input, output, or error streams of our terminal to the container.

Often we prefer to run our container in the background. However, we may wish to connect to it later to check its output or errors or disconnect the session.

In this short article, we’ll learn some useful commands to achieve these. We’ll also see different ways to detach from a session without stopping the container.

2. Run a Container in Attached/Detached Mode

Let’s see how to run a container in attached or detached mode.

2.1. Default Mode

By default, Docker runs a container in the foreground:

$ docker run --name test_redis -p 6379:6379 redis

This means we can’t return to our shell prompt until the process finishes.

The above command links the standard output (stdout), and the standard error (stderr) streams with our terminal. So, we can see the console output of the container in our terminal.

The –name option gives the container a name. We can later use the same name to refer to this container in other commands. Alternatively, we can refer to it by the container id which we get executing the docker ps command.

We may also use the -a option to choose specific streams from stdin, stdout, and stderr to connect with:

$ docker run --name test_redis -a STDERR -p 6379:6379 redis

The above command means we see only the error messages from the container.

2.2. Interactive Mode

We initiate a container in the interactive mode with -i and -t options together:

$ docker run -it ubuntu /bin/bash

Here, the -i option attaches the standard input stream (stdin) of the bash shell in the container and the -t option allocates a pseudo-terminal to the process. This lets us interact with the container from our terminal.

2.3. Detached Mode

We run a container in detached mode with the -d option:

$ docker run -d --name test_redis -p 6379:6379 redis

This command starts the container, prints its id, and then returns to the shell prompt. Thus, we can continue with other tasks while the container continues to run in the background.

We can connect to this container later using either its name or container id.

3. Interact With a Running Container

3.1. Execute a Command

The execute command lets us execute commands inside a container that is already running:

$ docker exec -it test_redis redis-cli

This command opens a redis-cli session in the Redis container named test_redis which is already running. We can also use the container id instead of the name. The option -it, as explained in section 2.2, enables the interactive mode.

However, we may only want to get the value against a key:

$ docker exec test_redis redis-cli get mykey

This executes the get command in the redis-cli, returns the value for the key mykey, and closes the session.

It is also possible to execute a command in the background:

$ docker exec -d test_redis redis-cli set anotherkey 100

Here, we use -d for this purpose. It sets the value 100 against the key anotherKey, but doesn’t display the output of the command.

3.2. Attaching a Session

The attach command connects our terminal to a running container:

$ docker attach test_redis

By default, the command binds the standard input, output, or error streams with the host shell.

To see only the output and error messages, we may omit stdin using the –no-stdin option:

$ docker attach --no-stdin test_redis

4. Detach From a Container

The way to detach from a docker container depends on its running mode.

4.1. Default Mode

Pressing CTRL-c is the usual way of ending a session. But, if we’ve launched our container without the -d or -it option, the CTRL-c command stops the container instead of disconnecting from it. The session propagates the CTRL-c i.e., SIGINT signal to the container and kills its main process.

Let’s override the behavior passing –sig-proxy=false:

$ docker run --name test_redis --sig-proxy=false -p 6379:6379 redis

Now, we can press CTRL-c to detach only the current session while the container keeps running in the background.

4.2. Interactive Mode

In this mode, CTRL-c acts as a command to the interactive session and so it doesn’t work as a detach key. Here, we should use CTRL-p CTRL-q to end the session.

4.3. Background Mode

In this case, we need to override the –sig-proxy value while attaching the session:

$ docker attach --sig-proxy=false test_redis

We may also define a separate key by –detach-keys option:

$ docker attach --detach-keys="ctrl-x" test_redis

This detaches the container and returns the prompt when we press CTRL-x.

5. Conclusion

In this article, we saw how to launch a docker container in both attached and detached mode.

Then, we looked at some commands to start or end a session with an active container.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.