Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 2Name in the course: Running containers

Run and manage containers

The daily container commands: run a container (in the foreground or background), look at it, read its logs, get a shell inside, limit its resources, stop and remove it. Docker and Podman use the same commands — swap docker for podman.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

2.1

Run a container

docker run = pull the image if needed + create + start.

docker run --rm hello-world
docker run --rm -it ubuntu:24.04 bash
docker run -d --name web -p 8080:80 nginx:1.27
docker run -d --name app -e APP_ENV=prod -e TZ=Asia/Riyadh --restart unless-stopped myorg/app:1.4
curl -I http://localhost:8080
OptionMeaning
-dDetached: run in the background
-itInteractive terminal
--rmRemove the container when it stops
--nameA fixed name
-p HOST:CONTAINERPublish a port (127.0.0.1:8080:80 = only local)
-e VAR=value, --env-file FILEEnvironment variables
--restartno, on-failure, always, unless-stopped

2.2

Look at running containers

List, inspect and watch containers.

docker ps
docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
docker inspect web --format '{{.State.Status}} {{.NetworkSettings.IPAddress}}'
docker top web
docker stats --no-stream
docker port web

2.3

Read logs and get a shell

Containers log to stdout/stderr; docker logs shows it. docker exec runs a command inside a running container.

docker logs web
docker logs -f --tail 50 --since 10m web
docker exec -it web sh
docker exec web nginx -t
docker cp web:/etc/nginx/nginx.conf ./nginx.conf

At work

Many small images have no bash — use sh. Very small ones (distroless) have no shell at all: use docker cp and docker logs, or a debug container.

2.4

Limit resources

Without limits, one container can use all CPU and memory of the host.

docker run -d --name worker --memory 512m --memory-swap 512m --cpus 1.5 --pids-limit 200 myorg/worker:2.0
docker update --memory 1g --memory-swap 1g worker
docker stats worker --no-stream
OptionLimits
--memory, --memory-swapRAM (and RAM + swap) — over it, the process is killed (OOM)
--cpusCPU time (1.5 = one and a half cores)
--pids-limitNumber of processes (stops fork bombs)
--ulimit nofile=1024:2048Open files

2.5

Stop, start and remove

Stop sends SIGTERM (then SIGKILL after 10 s). Remove deletes the container and its writable layer.

docker stop web
docker start web
docker restart web
docker kill -s HUP web
docker rm web
docker rm -f $(docker ps -aq --filter status=exited)
docker container prune -f

Remember

A container stops when its main process (PID 1) ends. A container that exits at once usually has a start command that finishes or fails — read docker logs.

All commands in this chapter