Docker Container Terminates After a Certain Period

Context

A Docker container launches successfully but unexpectedly stops after operating for a duration without other observable errors.

Symptom

When a Docker container image is executed, the CLI remains inactive before automatically closing after prolonged idleness. This behavior persists even for containers launched in background via docker run -d.

image

Root Cause

The container image includes a predefined 300-second timeout (TMOUT) setting in /etc/profile. When users access the container via SSH and maintain terminal inactivity beyond this threshold, the system forcibly terminates the session.

image

Resolution

  1. Deploy the container with existing TMOUT configuration.

    shell
    docker run -itd XXX bash
  2. Adjust container settings through interactive execution.

    shell
    docker exec -it $container_id bash
    1. Disable the timeout by setting TMOUT=0 in /etc/profile:

      image

    2. Ensure profile loading by adding source /etc/profile to /root/.bashrc:

      image

  3. Reinitialize the container to apply permanent changes:

    shell
    docker restart $container_id

    image