Lesson content
Read, practise, then check your understanding
Docker Containers: Run, Stop, and Exec
docker run combines image selection with container creation and startup. Runtime flags configure name, environment, mounts, networks, ports, resources, restart policy, user, and security options. Most configuration cannot be changed after creation; replace the container instead.
Core ideas
- Detached mode returns the terminal while the container continues; attached mode connects standard streams.
docker stopsends the configured stop signal, waits a grace period, then forces termination.docker execlaunches an extra process in a running container; it is useful for diagnosis, not permanent configuration.- The container exit code comes from its primary process and is essential for automation.
How it works
Start with an explicit name and limits, inspect logs and health, then stop gracefully. Remove disposable containers with --rm; use orchestration or restart policy for managed services. When debugging, prefer logs, inspect, stats, and a purpose-built debug container before changing a production instance.
Command or configuration example
docker run -d --name api \
--read-only --tmpfs /tmp \
--memory 512m --cpus 1 \
-e PORT=3000 -p 3000:3000 my-api:1.4.0
docker logs --tail 100 api
docker stats --no-stream api
docker exec api node --version
docker stop --time 20 api
docker rm api
Run examples first in a disposable environment. Replace image names, versions, credentials, ports, paths, and resource values with reviewed settings appropriate to your system.
Production guidance
- Ensure the primary process handles SIGTERM and exits within the grace period.
- Do not bake secrets into the image or pass sensitive values where process inspection exposes them.
- Use immutable replacement rather than hand-editing files inside a running container.
The chapter quiz follows this lesson and checks the concepts as well as the operational tradeoffs.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.