Home Explore Blog CI



docker

3rd chunk of `content/manuals/engine/containers/start-containers-automatically.md`
58c38ade2b0be66cef39fa12c8b8467def034f0ba8e330fe0000000100000d46
- A restart policy only takes effect after a container starts successfully. In
  this case, starting successfully means that the container is up for at least
  10 seconds and Docker has started monitoring it. This prevents a container
  which doesn't start at all from going into a restart loop.

- If you manually stop a container, the restart policy is ignored until the
  Docker daemon restarts or the container is manually restarted. This prevents
  a restart loop.

- Restart policies only apply to containers. To configure restart policies for
  Swarm services, see
  [flags related to service restart](/reference/cli/docker/service/create.md).

### Restarting foreground containers

When you run a container in the foreground, stopping a container causes the
attached CLI to exit as well, regardless of the restart policy of the
container. This behavior is illustrated in the following example.

1. Create a Dockerfile that prints the numbers 1 to 5 and then exits.

   ```dockerfile
   FROM busybox:latest
   COPY --chmod=755 <<"EOF" /start.sh
   echo "Starting..."
   for i in $(seq 1 5); do
     echo "$i"
     sleep 1
   done
   echo "Exiting..."
   exit 1
   EOF
   ENTRYPOINT /start.sh
   ```

2. Build an image from the Dockerfile.

   ```console
   $ docker build -t startstop .
   ```

3. Run a container from the image, specifying `always` for its restart policy.

   The container prints the numbers 1..5 to stdout, and then exits. This causes
   the attached CLI to exit as well.

   ```console
   $ docker run --restart always startstop
   Starting...
   1
   2
   3
   4
   5
   Exiting...
   $
   ```

4. Running `docker ps` shows that is still running or restarting, thanks to the
   restart policy. The CLI session has already exited, however. It doesn't
   survive the initial container exit.

   ```console
   $ docker ps
   CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS     NAMES
   081991b35afe   startstop   "/bin/sh -c /start.sh"   9 seconds ago   Up 4 seconds             gallant_easley
   ```

5. You can re-attach your terminal to the container between restarts, using the
   `docker container attach` command. It's detached again the next time the
   container exits.

   ```console
   $ docker container attach 081991b35afe
   4
   5
   Exiting...
   $
   ```

## Use a process manager

If restart policies don't suit your needs, such as when processes outside
Docker depend on Docker containers, you can use a process manager such as
[systemd](https://systemd.io/) or
[supervisor](http://supervisord.org/) instead.

> [!WARNING]
>
> Don't combine Docker restart policies with host-level process managers,
> as this creates conflicts.

To use a process manager, configure it to start your container or service using
the same `docker start` or `docker service` command you would normally use to
start the container manually. Consult the documentation for the specific
process manager for more details.

### Using a process manager inside containers

Process managers can also run within the container to check whether a process is
running and starts/restart it if not.

> [!WARNING]
>
> These aren't Docker-aware, and only monitor operating system processes within
> the container. Docker doesn't recommend this approach, because it's
> platform-dependent and may differ between versions of a given Linux
> distribution.

Title: Restarting Foreground Containers, Process Managers, and Docker Restart Policy Warnings
Summary
This section details how foreground containers behave with restart policies, illustrating with an example using a Dockerfile that prints numbers and exits. It explains that while the container restarts according to its policy, the attached CLI exits upon the container's initial exit. It also suggests using process managers like systemd or supervisor for managing containers when restart policies are insufficient, warning against combining Docker restart policies with host-level process managers to avoid conflicts. Additionally, it mentions the option of running process managers within containers, but advises against it due to platform dependency and lack of Docker awareness.