Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to set user of Docker container

📅 2017-Jul-21 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ docker, gid, uid, user ⬩ 📚 Archive

Problem

When you run a Docker container using docker run, everything inside the container is executed by the root user and root group. Its UID is 0 and GID is 0. This can sometimes be a problem.

For example, I had mounted a directory from the host filesystem into the Docker container using the --volume option. When root creates a new file or directory in this mounted directory, it appears as owned by the user nobody and group nogroup. This was a problem since I wanted these new files and directories to be created with my username or at least my group.

Solution

You can set what username or group you want to run as inside a container by using the --user option.

Tried with: Ubuntu 16.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧