5 min read

Docker Day to Day - Essential Commands and First Real Containers

1. Recap: From Images to Running Containers

In the first part of this series we saw what Docker is, the problem it solves and the three core concepts — images, containers and registries. Now that the theory is in place, this article is practical: the commands that actually become daily habits — run, exec, logs, ps, build — applied to real containers, not just hello-world.

2. docker run Beyond the Basics

The previous article already used docker run to start an Nginx. It’s worth knowing the flags that show up all the time:

# Interactive mode, useful for exploring an image by hand
docker run -it ubuntu bash

# Environment variables
docker run -e POSTGRES_PASSWORD=secret -d postgres

# Mount a host directory inside the container (bind mount)
docker run -v $(pwd)/data:/data -d my-image

# Remove the container automatically when it stops
docker run --rm -it python:3.12 python3

# Limit resources
docker run --memory=512m --cpus=1 my-image
  • -it combines -i (interactive, keeps STDIN open) with -t (allocates a pseudo-terminal) — it’s the pair of flags for “getting into” a container and using a shell as if it were a normal machine.
  • --rm avoids piling up stopped containers on disk after quick, throwaway tests — without it, every docker run leaves a stopped container behind until it’s removed by hand.
  • -e sets environment variables; official images like Postgres usually document which variables they expect (user, password, initial database name).

3. Inspecting What’s Running

The most used command to get an overview of what Docker is managing on the machine:

docker ps              # running containers
docker ps -a           # all of them, including stopped ones
docker ps -q           # only the ids (useful in scripts)

To dig deeper into a specific container:

docker inspect my-container      # all metadata as JSON: network, volumes, config
docker top my-container          # processes running inside the container
docker stats                     # real-time CPU/memory usage of every container

docker stats is particularly useful for spotting a container using far more memory or CPU than expected, without having to get inside it.

4. Logs: the First Debugging Tool

Containers usually don’t have a traditional log file accessible from outside — Docker’s convention is for the application to write to standard output (stdout/stderr), and Docker captures it automatically:

docker logs my-container             # the whole accumulated log
docker logs -f my-container          # follow the log in real time (like tail -f)
docker logs --tail 100 my-container  # only the last 100 lines
docker logs --since 10m my-container # only the last 10 minutes

In practice, docker logs -f container-name is usually the first command you run when investigating a container that isn’t behaving as expected — even before getting inside it.

5. docker exec: Getting Into a Running Container

Unlike docker run (which creates a new container), docker exec runs a command inside a container that’s already running:

# Open a shell inside a running container
docker exec -it my-container bash

# Run a one-off command without opening an interactive shell
docker exec my-container ls /app

# Handy for inspecting the database of a Postgres container
docker exec -it my-postgres psql -U postgres

This is the everyday command for debugging a container in production (or staging) without restarting it: checking whether a configuration file arrived correctly, looking at the contents of a directory, testing network connectivity with curl from inside the container, and so on.

6. docker build: From Application to Image

Every example so far has used ready-made images from Docker Hub. To package your own application, the starting point is a Dockerfile (Article 3 of this series is entirely about writing them well) — for now, a minimal example for a Python application:

# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python3", "app.py"]

Build the image from that file:

docker build -t my-app:1.0 .
  • -t my-app:1.0 — gives the resulting image a name (my-app) and a tag (1.0). Without an explicit tag, Docker uses latest by default — something to avoid in real environments, because latest says nothing about which version of the code is actually packaged in it.
  • . — the build context: the directory whose contents are available to the Dockerfile’s COPY/ADD instructions. Everything in it is sent to the Docker daemon before the build starts, so large, irrelevant directories (like .git, node_modules) should be excluded with a .dockerignore file.

Once built, the image can be run like any other:

docker run -d --name my-app-container my-app:1.0

7. Cleaning Up

Stopped containers, unused images and orphaned volumes pile up quickly during development. Useful cleanup commands:

docker container prune    # remove all stopped containers
docker image prune        # remove "dangling" images (untagged, build leftovers)
docker image prune -a     # also remove images not used by any container
docker system prune       # general cleanup: stopped containers, unused networks and images

It’s worth running docker system df first, to get an idea of how much disk space each category is taking up (images, containers, volumes, build cache) before deciding what to clean.

8. Conclusion and Next Steps

With run, ps, logs, exec and build, you can already cover the full everyday cycle: start containers, inspect what’s running, debug problems and package your own application as an image. In the next article, the focus goes deep into the Dockerfile: how layers work, how to take advantage of the build cache to speed up repeated builds, and best practices for writing a lean, efficient Dockerfile.


References:

  1. Docker CLI Reference
  2. Docker Documentation — Dockerfile Reference