brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker run with your own user ID stops root-owned files in your folders

Docker
~/tips/tip-docker-run-user-id

docker run --rm --user "$(id -u):$(id -g)" \

Run a one-off container as yourself so the files it writes are yours.

Run a build tool or a converter in a container with a bind mount, and the files it writes are owned by root on your host. Then you need sudo to delete your own output. Running the container as your own user avoids that.

docker run --rm --user "$(id -u):$(id -g)" \
  -v "$PWD:/work" -w /work \
  alpine sh -c 'echo hi > out.txt'

id -u and id -g print your numeric user and group, so the process in the container runs with the same numbers and out.txt belongs to you. The user does not need to exist inside the image; Docker accepts any numeric ID.

This is handy for one-off tools: a static site build, an image converter, a linter. For long running apps, many images have their own PUID and PGID variables that do the same thing at startup, and I would use those when they exist.

A user that is not in the image’s /etc/passwd has no home folder and no name, so tools that want to write to $HOME or look up their user may fail. Setting -e HOME=/tmp often helps. And an image built to run as root may need to write to paths your ID cannot touch, which shows up as permission errors inside the container.

Further reading: docker container run: user, Running containers: USER

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link