Difficulty: Beginner
Estimated Time: 10 minutes

In this lab, we will learn about attaching and detaching to a container's STDOUT

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Attach and Detach

Attach/Detach

  • Run Centos image in detached mode:
    docker container run -d centos ping 8.8.8.8 -c 50

  • Attach to the newly created container and observe the output:
    docker container attach $(docker container ls -q)
    exit using CTRL+C.

  • Check your running containers and notice that your container is no longer running:
    docker container ls
    The CTRL+C combination killed the container process.

  • Run another container, but this time with -it passed:
    docker container run -d -it centos ping 8.8.8.8 -c 50

  • Attach and detach from container:
    docker container attach $(docker container ls -q)
    ^P^Q NOTE: This will not work in katacoda, CTRL-Q maps to quit.

  1. Show your container still running in the background:
    docker container ls