Restarting and attaching to containers
- Background and foreground
- Detaching from a container (Linux/macOS)
- Detaching cont. (Win PowerShell and cmd.exe)
- Specifying a custom detach sequence
- Attaching to a container
- Detaching from non-interactive containers
- Checking container output
- Restarting a container
- Attaching to a REPL
We have started containers in the foreground, and in the background.
In this chapter, we will see how to:
- Put a container in the background.
- Attach to a background container to bring it to the foreground.
- Restart a stopped container.
Background and foreground
The distinction between foreground and background containers is arbitrary.
From Docker's point of view, all containers are the same.
All containers run the same way, whether there is a client attached to them or not.
It is always possible to detach from a container, and to reattach to a container.
Analogy: attaching to a container is like plugging a keyboard and screen to a physical server.
Detaching from a container (Linux/macOS)
If you have started an interactive container (with option
-it), you can detach from it.
The "detach" sequence is
Otherwise you can detach by killing the Docker client.
(But not by hitting
^C, as this would deliver
SIGINTto the container.)
-it stand for?
-tmeans "allocate a terminal."
-imeans "connect stdin to the terminal."
Detaching cont. (Win PowerShell and cmd.exe)
Docker for Windows has a different detach experience due to shell features.
^P^Qdoes not work.
^Cwill detach, rather than stop the container.
Using Bash, Subsystem for Linux, etc. on Windows behaves like Linux/macOS shells.
Both PowerShell and Bash work well in Win 10; just be aware of differences.
Specifying a custom detach sequence
- You don't like
^P^Q? No problem!
- You can change the sequence with
docker run --detach-keys.
- This can also be passed as a global option to the engine.
Start a container with a custom detach command:
$ docker run -ti --detach-keys ctrl-x,x jpetazzo/clock
Detach by hitting
^X x. (This is ctrl-x then x, not ctrl-x twice!)
Check that our container is still running:
$ docker ps -l
Attaching to a container
You can attach to a container:
$ docker attach <containerID>
- The container must be running.
- There can be multiple clients attached to the same container.
- If you don't specify
--detach-keyswhen attaching, it defaults back to
Try it on our previous container:
$ docker attach $(docker ps -lq)
^X x doesn't work, but
^P ^Q does.
Detaching from non-interactive containers
Warning: if the container was started without
- You won't be able to detach with
- If you hit
^C, the signal will be proxied to the container.
- You won't be able to detach with
Remember: you can always detach by killing the Docker client.
Checking container output
docker attachif you intend to send input to the container.
If you just want to see the output of a container, use
$ docker logs --tail 1 --follow <containerID>
Restarting a container
When a container has exited, it is in stopped state.
It can then be restarted with the
$ docker start <yourContainerID>
The container will be restarted using the same options you launched it with.
You can re-attach to it if you want to interact with it:
$ docker attach <yourContainerID>
docker ps -a to identify the container ID of a previous
and try those commands.
Attaching to a REPL
REPL = Read Eval Print Loop
Shells, interpreters, TUI ...
docker attach, and see nothing
The REPL doesn't know that you just attached, and doesn't print anything
docker attach, the Docker Engine sends SIGWINCH signals to the container.
SIGWINCH = WINdow CHange; indicates a change in window size.
This will cause some CLI and TUI programs to redraw the screen.
But not all of them.