docker run --detach IMAGE
Detached mode, shown by the option
-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.
This tutorial is aimed at beginners. I will show you how to run containers in detached mode. Then reconnect them to your terminal input and output if needed.
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
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.
Once you have started your containers in detached mode, you can view them running using the CLI command
By default, it lists running containers. Some useful options include:
-all. -afor all containers (default shows just running), and
--quiet, -qto list their ids (useful for when you want to get all the containers).
To see even more information about a container you can view its logs.
Once you have certain details about containers, such as their name or ID, you can then view their output using the command
docker logs [OPTIONS] CONTAINER
Two of the most useful options for this command are:
--tailWhile the default is all lines, you can specify a specific number of lines to show from the end of the logs.
--follow , -fFollow log output, which means it will print the running container's logs to standard out as it is being used.
I cover more ways to use the
docker logs command in my tutorial on Medium: Docker logs: how to use options, tail and grep to debug effectively.
As well as viewing container output this way, you can also run commands to reattach your terminal input and output to the container.
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
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.
Also, check out my other tutorials for more on using Docker →
Originally published at medium.freecodecamp.org.