Objective
Watch OverlayFS copy a file from the image’s lower layer to the container’s upper layer on first write. This is the “copy-up” behaviour that affects write performance and disk usage.
Requirements
- A Linux host.
- Docker installed.
- An image with a known file (e.g.
nginx:1.27).
Tasks
Task 1: Start a container
docker run --rm -d --name overlaytest nginx:1.27 sleep 3600
PID=$(docker inspect overlaytest --format '{{.State.Pid}}')
echo "Container PID: $PID"
Task 2: Find the OverlayFS directories
CGROUP=$(cat /proc/$PID/cgroup | awk -F: '{print $3}')
BASE=/sys/fs/cgroup$CGROUP
LOWER=$(cat $BASE/lower)
UPPER=$(cat $BASE/upper)
WORK=$(cat $BASE/work)
echo "Lower: $LOWER"
echo "Upper: $UPPER"
echo "Work: $WORK"
Task 3: Verify the file is in the lower layer
ls -la /var/lib/docker/overlay2/$LOWER/diff/usr/share/nginx/html/index.html
The file exists in the lower layer (image).
Task 4: Modify the file inside the container
docker exec overlaytest sh -c "echo modified > /usr/share/nginx/html/index.html"
Task 5: Check for the whiteout and upper file
ls -la /var/lib/docker/overlay2/$UPPER/diff/usr/share/nginx/html/
md5sum /var/lib/docker/overlay2/$LOWER/diff/usr/share/nginx/html/index.html
The lower layer’s file is unchanged. The upper layer has the modification.