Sei sulla pagina 1di 4

3/13/2019 Docker’s detached mode for beginners – freeCodeCamp.

org

Docker’s detached mode for beginners


How to run containers in the background of
your terminal

ryanwhocodes Follow
Oct 1, 2018 · 4 min read

docker run --detach IMAGE

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.

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.

https://medium.freecodecamp.org/dockers-detached-mode-for-beginners-c53095193ee9 1/7
3/13/2019 Docker’s detached mode for beginners – freeCodeCamp.org

• Detached mode

• View containers

• View container logs

• Execute commands in running containers

• Attach

• Find out more

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).

To see even more information about a container you can view its logs.

https://medium.freecodecamp.org/dockers-detached-mode-for-beginners-c53095193ee9 2/7
3/13/2019 Docker’s detached mode for beginners – freeCodeCamp.org

View container 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 .

docker logs [OPTIONS] CONTAINER

Two of the most useful options for this command are:

• --tail While the default is all lines, you can specify a specific
number of lines to show from the end of the logs.

• --follow , -f Follow log output, which means it will print the


running container's logs to standard out as it is being used.

For more on using docker logs , checkout my tutorial below.

Docker logs: how to use options, tail and grep


to debug e ectively

When developing applications based on Docker,


being able to nd speci c information in the log…
medium.com

As well as viewing container output this way, you can also run commands
to reattach your terminal input and output to the container.

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

https://medium.freecodecamp.org/dockers-detached-mode-for-beginners-c53095193ee9 3/7
3/13/2019 Docker’s detached mode for beginners – freeCodeCamp.org

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

further.

• Run bash or any command in a Docker container

• Docker run vs exec: deep-dive into their differences

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.

Attach
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.

https://medium.freecodecamp.org/dockers-detached-mode-for-beginners-c53095193ee9 4/7

Potrebbero piacerti anche