keepcalculator

06 Numbers and Powers

Why Docker Containers Stay Off Until Needed

Card No. 06·83766 · Updated · Standard formula, shown below

Docker containers stay off until you need them because they are designed as on-demand processes, not persistent services. By default, a container runs only while its main process is active; when that process exits, the container stops. This behavior is intentional, saving CPU, memory, and battery, and it is controlled by restart policies, orchestration tools, or manual commands.

How Docker Container Lifecycle Works

Every Docker container has a lifecycle: created, running, paused, stopped, and removed. When you run docker run, Docker creates a container from an image and starts its primary process (the ENTRYPOINT or CMD). The container stays alive only as long as that process runs. If the process finishes—for example, a script that completes—the container exits and enters the stopped state. It does not automatically restart unless you configure a restart policy or use an orchestrator like Docker Compose or Kubernetes.

Stopped containers still exist on disk, holding their filesystem and configuration, but they consume no CPU or memory. They are like paused virtual machines, but lighter. You can start them again with docker start, which resumes the same container with its previous filesystem state, or you can create a new one from the same image.

Why Not Keep All Containers Running?

Running containers consume resources even when idle. Each running container has its own process tree, network namespace, and memory allocation. On a laptop, dozens of idle containers would drain battery and slow the system. On a server, they would waste memory that other applications need. Docker’s philosophy is to run containers only when work is being done, then stop them to free resources.

This is especially important for batch jobs, cron-like tasks, or CI/CD pipelines. A container that runs a data backup, sends an email, or processes a queue can start, do its job, and exit. Keeping it running would require polling or waiting, which wastes resources. The “stop when done” pattern is a core Docker best practice.

Restart Policies: The Key to Automatic Starts

Docker provides restart policies that control whether a container starts automatically. These are set with the --restart flag or in a Compose file. The options are:

  • no (default): never restart automatically, even after a reboot.
  • on-failure: restart if the container exits with a non-zero exit code, up to a maximum number of times.
  • always: always restart the container if it stops, including after a daemon restart or reboot.
  • unless-stopped: restart unless the user manually stopped the container.

If you see containers that stay off, check their restart policy. A container with no will not come back after it exits. One with unless-stopped will start on boot but not if you explicitly stopped it. This is a common source of confusion: users expect a container to run forever, but the default policy is no.

Orchestration: Containers That Start on Demand

In production, containers rarely run directly with docker run. Instead, orchestrators like Docker Compose, Kubernetes, or Docker Swarm manage them. These tools can scale containers up or down based on demand, health checks, or scheduled times. For example, a web service might run three replicas during the day and scale to zero at night. A batch processor might start a container every hour, run for five minutes, then stop.

Orchestrators also handle dependencies. If a database container is down, an app container may not start until the database is ready. This is why you might see containers in a “created” or “restarting” state—they are waiting for conditions to be met. The depends_on option in Compose, or liveness probes in Kubernetes, control this sequencing.

Common Reasons Your Containers Stay Off

If you expected a container to be running but it is off, the cause is usually one of these:

  • Main process exited: the command inside the container finished, so the container stopped.
  • Restart policy is no: the container won’t restart after a crash or reboot.
  • Manual stop: someone ran docker stop or docker-compose down.
  • Resource limits: the container hit a memory or CPU limit and was killed.
  • Health check failed: an orchestrator stopped the container because it was unhealthy.
  • Dependency not ready: the container is waiting for another service to be available.

To diagnose, run docker ps -a to see all containers and their status. Then use docker logs <container> to see why it exited. The exit code is also helpful: 0 means a clean exit, non-zero indicates an error.

How to Make Containers Start When You Need Them

There are several ways to ensure a container runs when required, depending on your use case:

  1. Set a restart policy: use --restart unless-stopped for services that should always be up.
  2. Use a process manager: tools like systemd can start a container on boot or on a schedule.
  3. Schedule with cron: run docker run in a cron job for batch tasks.
  4. Use an orchestrator: Kubernetes or Swarm can start containers based on replicas or events.
  5. Trigger manually: call docker start when you need the container, such as from a script or webhook.

For a web server that must always be available, use --restart always. For a one-off script, don’t use any restart policy—let it exit when done. The choice depends on whether the container is a long-running service or a short-lived job.

Frequently Asked Questions

Why is my container not starting after reboot?

If the restart policy is no (the default), Docker will not start the container after a reboot. Change it to unless-stopped or always with docker update --restart unless-stopped <container>. Also check if the Docker daemon itself is set to start on boot.

What does “Exited (0)” mean in docker ps?

Exit code 0 means the main process inside the container finished successfully. This is normal for batch jobs. The container is stopped but not deleted. You can restart it with docker start or remove it with docker rm.

Should I keep a database container running all the time?

Yes, databases are stateful services that need to be available for other containers. Use --restart unless-stopped so it starts on boot and stays up. But ensure you have persistent volumes so data survives container restarts.

Can I make a container start only when I access it?

There is no built-in Docker feature that starts a container on demand based on network traffic. You would need a proxy or orchestration tool that detects requests and starts the container, which is complex. Simpler is to keep it running or use a serverless platform.

Why does my container restart in a loop?

If the main process crashes immediately, Docker will keep restarting it if the policy is always or on-failure. Check the logs for errors. The restart count is capped for on-failure, but always will retry indefinitely, which can hide a persistent bug.

Does a stopped container lose its data?

No, stopped containers keep their filesystem and configuration. Data is lost only if you remove the container with docker rm. However, for important data, use volumes or bind mounts so data persists even after container removal.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →