Skip to content

Chapter 5 of 10

Docker Containers

Control container lifecycle, configuration, logs, signals, exit status, and debugging.

40 minutes 10 quick checksBy Subha Prasad
Lesson 5 of 10Course navigation

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 stop sends the configured stop signal, waits a grace period, then forces termination.
  • docker exec launches 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.

Which statement correctly describes docker run?
Which term matches this explanation: Creating and starting a new container from an image.
Which statement correctly describes docker stop?
Which term matches this explanation: Requesting a graceful container shutdown before forced termination.
Which statement correctly describes docker rm?
Which term matches this explanation: Removing a stopped container's metadata and writable layer.
Which statement correctly describes docker exec?
Which term matches this explanation: Starting an additional command inside a running container.
Which statement correctly describes docker logs?
Which term matches this explanation: Reading the stdout and stderr captured for a container.

0 of 10 checks passed

Your progress is saved on this device.