Running Containers in Practice

Site Admin · 11 Sep 2026 · 8 views

Running Containers in Practice

Lifecycle of a Container

A container's life is short and precise. It starts, runs its main process, and exits when that process stops. A web server runs forever; a batch script finishes and the container exits with a status code. This single-process model keeps containers predictable and easy to supervise.

docker run -d --name web --restart unless-stopped -p 80:80 nginx

Flags That Matter

The most useful run flags are few and powerful:

  • -d runs a container in the background (detached).
  • --name gives the container a memorable name.
  • -p maps host ports to container ports.
  • -e passes environment variables, the standard channel for configuration.
  • --restart decides the restart policy when the container stops.
  • --rm removes the container automatically when it exits, perfect for one-off tasks.
docker run -it python:3.12-slim bash

The -it flags give an interactive terminal, which drops you into a shell inside the container for debugging.

Volumes: Data That Outlives Containers

Containers are ephemeral: files written inside them vanish with the container. To persist data such as databases or uploads, mount a volume or a host directory with -v.

docker run -d -v mydata:/var/lib/mysql mysql:8

The named volume mydata survives container restarts and even survives the container being removed, so start it again later and the data is still there.

Networking Containers

Containers can talk to each other over a Docker network by name, without exposing ports to the host. This keeps a database container private while the web container communicates with it.

docker network create appnet

Containers joined to the same network resolve each other's container names as addresses, which later becomes the foundation of Docker Compose and Kubernetes-style deployments.

Key Points

  • Containers run one main process and exit when it finishes.
  • -d, -p, -e, --restart, and -it cover most daily needs.
  • Volumes persist data beyond a container's lifetime.
  • Docker networks let containers talk by name without host port exposure.
  • Ephemeral containers plus persistent volumes is the standard pattern.
Share this post:

Comments (0)

Please login or register to comment.