Surfing around the net, this issue has been mentioned on this forum here before as well as a couple of Stack Exchange questions. I may be beating a dead horse.
My compose file at this point looks like this:
services:
db:
image: postgres:${POSTGRES_VERSION}
volumes:
- ./db/data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build:
context: .
dockerfile: docker/Dockerfile
# These come from .env
args:
- COMPOSE_PROJECT_NAME
- DOCKER_REPOSITORY
- POSTGRES_VERSION
- PROJECT_VERSION
- RUBY_IMAGE
- RUBY_VERSION
hostname: ${COMPOSE_PROJECT_NAME}
image: ${DOCKER_REPOSITORY}/${COMPOSE_PROJECT_NAME}:${PROJECT_VERSION}
volumes:
- .:/${COMPOSE_PROJECT_NAME}
ports:
- "3000:3000"
depends_on:
- db
What I’ve discovered that no one else seems to have looked at is that some files inside the container (from the container’s perspective) are owned by root.
docker compose --project-directory . --file docker/compose.yml run --no-deps -it db bash
root@3d1eff959317:/# find /var/lib/postgresql/data -user 0 -exec ls -l {} +
-rw------- 1 root root 8 Sep 9 12:58 /var/lib/postgresql/data/pg_logical/replorigin_checkpoint
-rw------- 1 root root 2225 Sep 9 13:09 /var/lib/postgresql/data/pg_stat_tmp/db_0.stat
-rw------- 1 root root 6665 Sep 9 13:09 /var/lib/postgresql/data/pg_stat_tmp/db_13757.stat
-rw------- 1 root root 7035 Sep 9 13:08 /var/lib/postgresql/data/pg_stat_tmp/db_16384.stat
-rw------- 1 root root 94 Sep 9 12:53 /var/lib/postgresql/data/postmaster.pid
My suspicion is that this is what is causing the errors.
I asked on the Postgres mailing list and I got this reply which I’m too inexperienced to fully understand:
| The data directory is outside the container so it is persistent. The pg_stat_tmp is inside the data directory.
Ah, that’s the reason. Docker daemon runs as root so if you do binding mount, files will be owned by root. You may want to use normal Docker volume and not an external directory.
To clarify a bit, Postgres has a data directory which is /var/lib/postgresql/data inside the container which I have mounted to ./db/data on my laptop. The pg_stat_tmp directory is inside the data directory. Everything within the data directory should only be written by a Postgres daemon which has UID:GID of 999:999. Let, somehow, a few files are becoming owned by root.
The reply above makes me think that this is somehow due to the Docker daemon inside the container but I don’t understand really why the daemon would be creating files within the container and if this is a problem, why is it occurring in only very few cases?
There are various solutions suggested but they seem more like work arounds to me instead of addressing the real root issue – but I could easily be mistaken.
