Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 2Naam in de cursus: 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 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met 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

Op het werk

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

Onthoud

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.

Alle commando's in dit hoofdstuk