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

docker

Build, ship and run containers — the most used container tool in CI pipelines and on developer laptops.

Niveau
DevSecOps-service
Groep
Containers en Kubernetes
Root nodig
Voor wijzigingen
Pakket
docker-ce

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

Wat het doet

Docker packs an app with everything it needs into an image and runs it as an isolated container. The docker CLI talks to the dockerd daemon (service docker.service). Images come from registries (Docker Hub, GitHub ghcr.io, Azure ACR). Docker Compose (docker compose) runs several containers from one YAML file.

On RHEL the built-in tool is podman (same commands, no daemon, rootless by default). Docker CE is installed from Docker's own repository. Users in the docker group are root on the host — keep that group small.

Zo schrijf je het

docker COMMAND [OPTIONS] [ARGS]
docker compose [-f compose.yaml] up|down|ps|logs

Belangrijkste delen

Main commands

run
Create and start a container from an image.
ps [-a]
List running (all) containers.
build -t NAME:TAG .
Build an image from a Dockerfile.
images / image ls
List local images.
pull / push
Download / upload an image.
exec -it C sh
Run a command inside a running container.
logs [-f] C
Show container output.
stop / start / restart / rm C
Container life cycle.
inspect OBJ
Show low-level JSON details.
volume / network
Manage volumes and networks.
compose
Multi-container apps from compose.yaml.
system df / system prune
Disk use / clean up.
login / logout REGISTRY
Log in to a registry.
buildx
Advanced builds (multi-platform, cache).
scout
Check images for known CVEs (Docker Scout).

Opties 15

Meest gebruikt

docker run options

-d
Run in the background.
-it
Interactive with a terminal.
--name NAME
Name the container.
-p HOST:CONT
Publish a port.
-v SRC:DST[:ro]
Mount a volume or a host folder.
-e KEY=VALUE / --env-file FILE
Set environment variables.
--rm
Remove the container when it exits.
--restart unless-stopped
Restart policy.
--network NET
Join a network.
-u UID:GID
Run as this user.
--read-only
Read-only root file system.
--cap-drop ALL / --cap-add CAP
Remove / add Linux capabilities.
--security-opt no-new-privileges
Block privilege escalation.
--memory 512m / --cpus 1
Resource limits.
--health-cmd CMD
Health check command.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1Install Docker CE on RHEL and run a test

    dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
    dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    systemctl enable --now docker
    docker run --rm hello-world
  2. 2Run a web server in the background

    docker run -d --name web -p 8080:80 --restart unless-stopped nginx:1.27
    docker ps
    curl -I http://localhost:8080
  3. 3Look inside and read the logs

    docker logs -f --tail 50 web
    docker exec -it web sh
    docker inspect -f '{{.NetworkSettings.IPAddress}}' web
  4. 4Build and tag an image

    cat Dockerfile
    FROM python:3.12-slim
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    COPY . .
    USER 1001
    CMD ["python", "app.py"]
    docker build -t ghcr.io/s0sta/shop:1.4.0 .
  5. 5Push to a registry

    echo "$GHCR_TOKEN" | docker login ghcr.io -u s0sta --password-stdin
    docker push ghcr.io/s0sta/shop:1.4.0
  6. 6Run a hardened container

    docker run -d --name api --read-only --tmpfs /tmp -u 1001:1001 --cap-drop ALL --security-opt no-new-privileges --memory 256m --cpus 0.5 -p 8080:8080 ghcr.io/s0sta/shop:1.4.0
  7. 7Keep data in a named volume

    docker volume create pgdata
    docker run -d --name db -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:16
  8. 8Run a full stack with Compose

    cat compose.yaml
    services:
      web:
        image: ghcr.io/s0sta/shop:1.4.0
        ports: ["8080:8080"]
        depends_on: [db]
      db:
        image: postgres:16
        volumes: [pgdata:/var/lib/postgresql/data]
    volumes:
      pgdata: {}
    docker compose up -d
    docker compose ps
    docker compose logs -f web
    docker compose down
  9. 9Build for two CPU types

    docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/s0sta/shop:1.4.0 --push .
  10. 10Check an image for CVEs

    docker scout cves ghcr.io/s0sta/shop:1.4.0
    trivy image ghcr.io/s0sta/shop:1.4.0
  11. 11Free disk space

    docker system df
    docker system prune -a --volumes

Let op

  • The docker group = root on the host. Prefer rootless Docker or podman.
  • Never put secrets in a Dockerfile (ENV, COPY .env): they stay in the image layers.
  • -p 8080:80 opens the port on all addresses and passes around firewalld rules; use -p 127.0.0.1:8080:80 for local-only.
  • Pin image tags (better: digests), never use latest in production.

Bestanden

/etc/docker/daemon.json
Daemon settings (log driver, registries, userns-remap).
/var/lib/docker/
Images, containers, volumes.
~/.docker/config.json
Registry logins.
/var/run/docker.sock
The daemon API socket (root access!).

Tips om te onthouden

  • Add a .dockerignore (with .git, .env, node_modules) to keep images small and secret-free.
  • Use multi-stage builds and slim or distroless base images.

Exitcodes

0Success.
125Docker daemon error.
126The command in the container cannot run.
127The command in the container was not found.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man docker