How do I ensure that mounted volumes always have read/write permissions by the current user on the host?

Viewed 21

For local development of applications where isolation of folder/file permissions are not mission critical, I'd like to mount local directories into transient containers but retain the ability to delete/modify those directories from the host account without sudo.

I have read that I can create specific volumes and then mount those precreated volumes. The thing is I would like to use self-removing transient containers arbitrarily (e.g. docker run --rm -v "$(pwd):/app" golang go build src/main.go).

The objective is to make it easy to work with various technologies (like databases or languages) without needing to install them on the host.

Running

docker run \
    --rm \
    -v "${HOME}/foo:/app" \
    alpine touch /app/bar.txt

Running this will create a folder/file with root permissions. Trying to use the --user flag still mounts the directory as root gives me the following error:

touch: /app/bar.txt: Permission denied
docker run \
    --rm \
    --user "$(id -u):$(id -g)" \
    -v "${HOME}/foo:/app" \
    alpine touch /app/bar.txt

This failure obviously extends to trying to spin up a database instance

docker run \
    --rm \
    --net=host \
    --user $(id -u):$(id -g) \
    -v "${HOME}/mongo/foo:/data/db" \
    mongo:latest
0 Answers
Related