Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
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| Option | Meaning |
|---|---|
-d | Detached: run in the background |
-it | Interactive terminal |
--rm | Remove the container when it stops |
--name | A fixed name |
-p HOST:CONTAINER | Publish a port (127.0.0.1:8080:80 = only local) |
-e VAR=value, --env-file FILE | Environment variables |
--restart | no, on-failure, always, unless-stopped |
Look at running containers
List, inspect and watch containers.
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.confOp 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.
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| Option | Limits |
|---|---|
--memory, --memory-swap | RAM (and RAM + swap) — over it, the process is killed (OOM) |
--cpus | CPU time (1.5 = one and a half cores) |
--pids-limit | Number of processes (stops fork bombs) |
--ulimit nofile=1024:2048 | Open files |
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 -fOnthoud
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.