DEV Community

Celso Nery
Celso Nery

Posted on

Part 2: Essential CLI Commands

🇧🇷 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Listing containers

To see the containers that are currently running:

$ docker ps
Enter fullscreen mode Exit fullscreen mode

To see all containers, including the ones that have already stopped, use the -a flag:

$ docker ps -a
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If the image is Alpine-based (as many lightweight production images are), it usually doesn't have bash installed — use ash instead:

$ docker exec -it <container name or ID> ash
  • Now you can run any command inside container to check cat /etc/os-release ou env.

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
Enter fullscreen mode Exit fullscreen mode
  • 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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)