Docker Uptime Command Showing Inaccurate System Duration: Causes and Fixes


3 views

Many developers encounter surprising results when running uptime inside Docker containers. You might see output like:

root@7efb3e947f73:/# uptime
23:41:57 up 16 min,  0 users,  load average: 0.06, 0.02, 0.00

When in reality, your container was just launched seconds ago. This discrepancy stems from how Docker handles system time and process isolation.

The uptime command reads from /proc/uptime, which reflects the host machine's uptime rather than the container's. This happens because:

  • Docker containers share the host's kernel
  • /proc is typically mounted from the host by default
  • Containerization doesn't create a separate kernel space

You can confirm this by comparing host and container uptime:

# On host machine
cat /proc/uptime

# Inside container
docker run -it ubuntu cat /proc/uptime

Both commands will show identical values, proving they're reading the same source.

1. Using Container-Specific Metrics

For accurate container uptime, use Docker's native commands:

docker inspect --format='{{.State.StartedAt}}' container_name_or_id

2. Alternative Uptime Calculation

Create a custom solution using container start time:

#!/bin/bash
start_time=$(docker inspect --format='{{.State.StartedAt}}' $CONTAINER_ID)
now=$(date +%s)
start_seconds=$(date --date="$start_time" +%s)
echo "Uptime: $(( (now - start_seconds) / 60 )) minutes"

3. Namespace Isolation (Advanced)

For complete process isolation, consider:

docker run --privileged --pid=host -it ubuntu

Note this has security implications and isn't recommended for production.

  • Use Docker stats API for resource monitoring
  • Implement health checks in your Dockerfile
  • Consider container orchestration tools that provide native uptime metrics

When working with Docker containers, you might notice unexpected behavior when running the uptime command. The reported uptime often appears significantly longer than the actual container runtime duration. This phenomenon occurs because:

# Example showing the discrepancy
$ docker run -it ubuntu
root@a1b2c3d4:/# uptime
 12:34:56 up 1 day,  3:45,  0 users,  load average: 0.00, 0.01, 0.05

The uptime command in Linux reads from /proc/uptime, which reflects the host system's uptime rather than the container's. Since containers share the host's kernel, they inherit this information:

# Inside container:
$ cat /proc/uptime
123456.78 987654.32  # Host's uptime in seconds

To get accurate container runtime information, consider these approaches:

Using Docker Inspect

$ docker inspect --format='{{.State.StartedAt}}' container_name_or_id

Container-native Alternative

Create a custom uptime script that checks the container's actual start time:

#!/bin/bash
start_time=$(date -d "$(cat /proc/1/stat | cut -d' ' -f22 | \
           awk '{print $1/100}')" +%s)
now=$(date +%s)
echo "Container uptime: $((now - start_time)) seconds"

For temporary debugging, you can use this one-liner:

$ echo "Uptime: $(($(date +%s) - $(date +%s -d "$(stat -c '%x' /proc/1/cmdline)"))) seconds"

While often just an annoyance, this becomes important when:

  • Writing container health checks
  • Monitoring container lifecycle
  • Debugging startup issues

The kernel maintains /proc/uptime as a global counter since boot. Containers, being just processes with isolated namespaces, don't have their own separate kernel boot time. This is why all containers on a host will show the same uptime value matching the host system.

# Compare host vs container
$ cat /proc/uptime          # On host
$ docker run --rm alpine cat /proc/uptime  # In container
# Both show identical values