cd ..
DOCKER

Managing Containers Day-to-Day with Docker

Managing Containers Day-to-Day with Docker

After understanding what a container is and how Docker organizes images, containers, and configurations, the next natural step is to learn how to keep these containers alive: create, exit without terminating, pause, monitor, and, when no longer needed, remove.

These notes came precisely from that study moment, testing command by command on Ubuntu containers.

Creating and Managing Your First Containers

When running an interactive container, three flags appear constantly:

FlagFunction
-iinteractivity — keeps STDIN open
-tallocates a terminal (TTY)
-itboth combined — a truly interactive terminal

In practice, the most common command to get started is:

docker container run -it ubuntu

Exiting the Container Without Terminating It

Here lies a common pitfall: there are two very different ways to “exit” a container.

To return to a container that is still running (exited with Ctrl+P+Q), simply reattach the terminal:

docker container attach <id_do_container>

Naming Containers

Working with randomly generated IDs quickly becomes tiresome. You can name the container during creation:

docker container run --name tostao -it ubuntu

And use that name instead of the ID to reconnect:

docker container attach tostao

If the container was terminated with Ctrl+D, attach alone won’t work — you first need to restart it and only then attach the terminal:

docker container start tostao
docker container attach tostao

Pausing and Unpausing

Pausing freezes the container’s processes without terminating them:

docker container pause tostao
docker container unpause tostao

Stopping and Removing

When the container is no longer needed:

docker container stop tostao
docker container rm tostao

And to clean up all stopped containers at once, without having to remove them one by one:

docker container prune

Viewing Metrics and Resource Usage

stats provides an overview of resource consumption for running containers:

docker container stats

Some useful variations:

docker container stats -a

Lists all containers (including stopped ones) and shows statistics for each.

docker container stats --no-stream

Prints the statistics once, without continuously updating on the screen.

top shows all processes running inside a specific container — not resource consumption statistics, but what is actually running inside:

docker container top tostao

Summarizing the difference: stats shows how much resource is being used; top shows what is being executed.

Consulting Logs

docker container logs is the first place to investigate what a container is doing:

docker container logs tostao

Some frequently appearing flags:

FlagFunction
--detailsshow extra details provided to the logs
-f, --followfollow log output in real time
-n, --tailnumber of lines to display from the end (default: all)
-t, --timestampdisplay the timestamp for each line
--sinceshow logs from a timestamp (e.g., 2013-01-02T13:23:37Z) or relatively (e.g., 42m for 42 minutes ago)
--untilshow logs until a timestamp, absolute or relative, in the same format as --since

Image and Container: The Save Game and the Character

A way that helped solidify the difference between an image and a container: think of a game.

docker run --name tostao -it ubuntu

And, following the same Docker command pattern, removing an image uses the same logic as container rm:

docker image rm <nome_da_imagem>

Conclusion

None of these commands are complicated in isolation, but together they form the complete lifecycle of a container: create, enter and exit without termination, pause when necessary, investigate through logs, measure consumption, and finally, clean up what is no longer needed. This is the cycle that repeats constantly in day-to-day Docker operations.

References

What did you think?