🇧🇷 Artigo em português aqui
Using Docker — Part 2: Essential CLI Commands
In Part 1 of this series, we installed Docker on Debian and ran our first container. Now let's cover the commands you'll use day to day to manage containers: running, listing, entering, stopping, removing, tailing logs, and monitoring resources.
Running a container
The most basic command is docker run, which creates and starts a container from a builded image:
$ docker run --rm -p 8080:80 httpd
Where:
-
--rm: automatically removes the container as soon as it stops (useful for tests, avoiding a pile-up of stopped containers); -
-p 8080:80: maps port 8080 of the host to port 80 of the container.
Running this command keeps the terminal "stuck," showing the container's logs in the foreground. To free up the terminal and run it in the background, use the -d (daemon) flag:
$ docker run --rm -d -p 8080:80 httpd
Listing containers
To see the containers that are currently running:
$ docker ps
To see all containers, including the ones that have already stopped, use the -a flag:
$ docker ps -a
This is useful for spotting stopped containers that might not have been removed (because they weren't run with the --rm flag).
Connecting a container
To interactively enter a running container (opening a shell):
$ docker exec -it <container name or ID> bash
If the image is Alpine-based (as many lightweight production images are), it usually doesn't have
bashinstalled — useashinstead:$ docker exec -it <container name or ID> ash
- Now you can run any command inside container to check
cat /etc/os-releaseouenv.
You can also run a specific command without opening an interactive session — for example, to check an environment variable:
$ docker exec -it <container name or ID> env
- This way, you won't need to enter the container.
Stopping and removing containers
To stop a running container:
$ docker stop <container name or ID>
Stopping doesn't remove the container — it continues to exist (stopped), until you explicitly remove it or it was created with --rm.
To remove it manually:
$ docker rm <container name or ID>
Tailing logs
Containers typically write their logs to stdout/stderr, and Docker captures that automatically. To view them (and follow in real time, like tail -f):
$ docker logs -f <container name or ID>
Without the -f flag, the command just prints the existing logs up to that point, without continuing to follow.
Monitoring resources
To see, in real time, the CPU, memory, disk I/O, and network usage of all running containers:
$ docker stats
This command is useful for quickly identifying containers consuming excessive resources, without needing external monitoring tools.
Next steps
With the essential CLI commands in hand, you can already operate containers day to day with confidence. But so far, data inside containers is ephemeral — if the container is removed, the data goes with it. In Part 3 of this series, we'll solve that with volumes, ensuring data persistence between runs.
Continued in Part 3.
Top comments (0)