Docker: Make Files Created Inside Containers Editable Outside

If you develop your projects using Docker, you might have noticed that files created inside the containers are often owned by the root user, and therefore can’t easily be edited outside that container, unless you manually change their ownership.

That’s quite a headache, if your language or the framework of choice uses a CLI tool to generate files and manage the project a la Laravel Artisan. All the generated files end up being owned by the root and you have to run chown to be able to edit them in your IDE.

There’s a solution. I am using a docker-compose.yml file for this, but the same method should be valid with plain docker run too.

In your docker-compose.yml file, under the service(s) you need this fix to work, add a user key with the value ${UID}:${GID}.

Then, in your shell’s “rc file” (Eg: .bashrc, .zshrc.), add export UID and export GID in two separate lines.

Restart your shell, run docker-compose up, go into the container with docker-compose exec <service-name> sh, make a file touch test-file, and try editing that file outside the container. You should be able to edit that file without any permission issues.


Leave a Reply

Your email address will not be published. Required fields are marked *