ryanwhocodes Oct 1, 2018 · 3 min read

Docker's Detached Mode for Beginners

How to run containers in detached mode then reconnect them to your terminal input and output if needed.

Detached mode, shown by the option --detach or -d, means that a Docker container runs in the background of your terminal. It does not receive input or display output. If you run containers in the background, you find out their details and then reattach your terminal to its input and output.


Detached mode

When starting a Docker container, you must first decide if you want to run the container in the background in a detached mode or in the default foreground mode. You may want to use this if you want a container to run but do not want to view and follow all its output.

You will often see the short version of the detach option used, -d, while the longer version is --detach.

docker run -d IMAGE

If you run containers in detached mode, you may then need to check on their status or run commands on them. Next, I will explain how to view them in your terminal.

View containers

Once you have started your containers in detached mode, you can view them running using the CLI command docker ps.

By default, it lists running containers. Some useful options include:

  • -a / -all for all containers (default shows just running), and
  • --quiet / -q to list their ids (useful for when you want to get all the containers).

Once you have certain details about them, such as their name or ID , you can then call other commands on them, such as docker exec.

Execute commands in running containers

Docker exec is a commonly used CLI command that allows you to run a command within an existing running container.

For example, you might want use docker exec with the -i (interactive) flag to keep stdin open and -t to allocate a terminal.

**docker exec -i -t container\_name /bin/bash**

Here are a few links to my other posts where I explain docker exec further.

You may even want to attach your terminal again to view a container’s output. Next, I will show you how you can do this.


The docker attach command connects your terminal’s standard input, output and error to a running container by passing it the container name or ID.

docker attach [OPTIONS] CONTAINER

This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
— attach | Docker Docs

$ docker run -d --name topdemo ubuntu /usr/bin/top -b

$ docker attach topdemo

Find out more

Learning how to use detached mode along with the ways to reattach your terminal to Docker containers means you can help manage running multiple containers and other tasks on the command line.

Docker’s documentation provides more examples of the differences between detached and attached modes.