Skip to content

📦 Container Management

Running Containers

bash
# Simplest way to run
docker run nginx

# Run in background + name the container
docker run -d --name my-nginx nginx
a1b2c3d4e5f6...

# Port mapping: host 8080 -> container 80
docker run -d --name web -p 8080:80 nginx

# Environment variables
docker run -d --name db \
  -e MYSQL_ROOT_PASSWORD=mypassword \
  -e MYSQL_DATABASE=myapp \
  mysql:8.0

# Resource limits: max 1 CPU and 512MB memory
docker run -d --name limited \
  --cpus="1.0" \
  --memory="512m" \
  nginx

# Restart policy: always auto-restart
docker run -d --restart=unless-stopped --name always-up nginx

📝 Note: 📝 Quick flag reference: -d background | -p port mapping | -e env var | --name container name | -v volume mount | -it interactive mode

Container Lifecycle

bash
# List running containers
docker ps
CONTAINER ID   IMAGE     COMMAND                  STATUS        PORTS                  NAMES
a1b2c3d4e5f6   nginx     "/docker-entrypoint.…"   Up 5 minutes  0.0.0.0:8080->80/tcp   my-nginx

# List all containers (including stopped ones)
docker ps -a

# Stop a container
docker stop my-nginx

# Start a stopped container
docker start my-nginx

# Restart a container
docker restart my-nginx

# Remove a stopped container
docker rm my-nginx

# Force remove a running container
docker rm -f my-nginx

# Clean up all stopped containers
docker container prune

Entering Containers and Viewing Logs

bash
# Enter a running container (open an interactive terminal)
docker exec -it my-nginx /bin/bash
root@a1b2c3d4e5f6:/# ls
bin  boot  dev  docker-entrypoint.d  etc  home  lib
root@a1b2c3d4e5f6:/# cat /etc/nginx/nginx.conf
...
root@a1b2c3d4e5f6:/# exit

# Run a single command inside the container (without entering it)
docker exec my-nginx nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# View container logs
docker logs my-nginx
172.17.0.1 - - [20/Jun/2026:10:30:00 +0000] "GET / HTTP/1.1" 200 615
172.17.0.1 - - [20/Jun/2026:10:30:05 +0000] "GET /favicon.ico HTTP/1.1" 404 555

# Follow logs in real time (like tail -f)
docker logs -f --tail 100 my-nginx

# View container resource usage
docker stats
CONTAINER ID   NAME       CPU %   MEM USAGE / LIMIT   MEM %   NET I/O       BLOCK I/O
a1b2c3d4e5f6   my-nginx   0.05%   2.5MiB / 512MiB     0.49%   1.2kB / 0B    0B / 0B

Data Persistence: Volumes

Containers are like temporary sheds — once demolished, everything inside is gone. But your data (database files, uploaded images) can't be lost! A Volume is like installing a safe inside the shipping container — even if the container is removed, the safe remains.

bash
# Create a named volume
docker volume create mysql-data

# Use a named volume
docker run -d --name db \
  -v mysql-data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=mypassword \
  mysql:8.0

# Use a bind mount (map a host directory into the container)
docker run -d --name web \
  -v /home/user/website:/usr/share/nginx/html \
  -p 8080:80 \
  nginx

# Read-only mount (container can only read, not write)
docker run -d -v /config:/etc/app/config:ro myapp

# List all volumes
docker volume ls
DRIVER    VOLUME NAME
local     mysql-data

# Inspect volume details
docker volume inspect mysql-data
[
    {
        "CreatedAt": "2026-06-20T10:00:00Z",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/mysql-data/_data",
        "Name": "mysql-data"
    }
]