Skip to content

Managing containers

Detach from running container#

When you start a container with docker run it is given an unique id that you can use for interacting with the container.

First let make a container that make some random jobs:

  • For that purpose let's create a bash script that displays the current time every 5 seconds:
cat <<EOF > script.sh
#!/bin/bash

while true; do
  echo "Current time: \$(date +\"%T\")"
  echo "Current time: \$(date +\"%T\")" >> appended_file.txt
  sleep 5
done
EOF
# make the script executable
chmod +x script.sh
  • Then let's create a minimal container that will execute this script
cat <<EOF > Dockerfile_infinite
FROM ubuntu:24.04
COPY script.sh .
CMD ["./script.sh"]
EOF
# Build the Dockerfile_infinite Dockerfile
docker build -t my_docker_infinite -f Dockerfile_infinite .
# Run the my_docker_infinite container
docker run my_docker_infinite

If we run docker run without any flags, your local terminal is attached to the container. This enables you to see the output, but also disables you from doing anything else in the meantime. We can start a container in detached mode with the -d flag.

  • quit the running container attached to your screen
  • run again this container with -d flag:
docker run -d my_docker_infinite

Then list all running containers using :

docker container ls
or
docker ps

This should show information about the running container similar to:

CONTAINER ID   IMAGE                    COMMAND       CREATED          STATUS          PORTS      NAMES
16b0861e2f31   my_docker_infinite   "./script.sh"   2 minutes ago   Up 2 minutes             elastic_keldysh
aa1d819ab847   my_docker_infinite   "./script.sh"   5 minutes ago   Up 5 minutes             agitated_taussig

Clean stopped container#

By default, Docker keeps containers after they have exited. This can be convenient for debugging or if you want to look at logs.

To display container information including those that have exited run:

docker container ls --all

Keeping containers after they have exited consumes huge amounts of disk space. It's therefore a good idea to always run with --rm, which will remove the container once it has exited.

  • Try running a container with --rm option:
docker run --rm ubuntu
  • Check if you still have traces of this container
docker container ls --all

Enter a running container#

If we want to enter a running container, there are two related commands we can use, docker attach and docker exec.

  • docker attach will attach local standard input, output, and error streams to a running container. This can be useful if your terminal closed down for some reason or if you started a terminal in detached mode and changed your mind.

Try to attach one of the two my_docker_infinite running containers:

docker attach <CONTAINER ID>
  • docker exec can be used to execute any command in a running container. It's typically used to peak in at what is happening by opening up a new shell. Here we start the container in detached mode and then start a new interactive shell so that we can see what happens. If you use ls inside the container you can see how the script generates data in the `appended_file.txt file.

Try it out:

docker exec -it <CONTAINER ID> /bin/bash

Note

Data created in the container will be thrown out when the container exits, execpted if the data was in a volume/bind mounted area.

Warning

As we created infinite working containers my_docker_infinite we should stop all of them manually.
To see tehm run docker container ls
Pay attention that we have several containers of the same image ^^.
Now remove them with docker rm -f <CONTAINER ID>

Quick recap

In this section we've learned:

  • How to use docker run for starting a container and how the flags -d and --rm work.
  • How to use docker container ls for displaying information about the containers.
  • How to use docker attach and docker exec to interact with running containers.