Docker mounts volumes as read only

I work with Docker, and I want to set the dyanmic folder, which changes a lot (so I don’t need to do dockers for each execution, which would be too expensive), but I want this folder to be read-only, However, changing the folder owners for someone else works, however chown requires root access, which I would not want to show in the application.

When I use the -v flag for mounting, it gives what the user name gives me, I created a user without root inside the docker image, however, all the files in the volume with the owner as the user who launched the docker are changed to the user I give from the command line, so I can’t only read files and folders. How can I prevent this?

I also added mustafa ALL=(docker) NOPASSWD: /usr/bin/docker , so I can go to another user through the terminal, but still the files have permissions for my user.

+91
docker readonly
Oct 03 '13 at 12:06 on
source share
2 answers

You can specify that the volume should be -v read-only by adding :ro to the -v switch:

 docker run -v volume-name:/path/in/container:ro my/image 

Please note that the folder is read-only in the container and read and write on the host.

2018 Edit

According to the documentation for using volumes , there is now another way to mount volumes using the --mount switch. Here's how to use this read-only:

 $ docker run --mount source=volume-name,destination=/path/in/container,readonly my/image 

docker-compose

Here is an example of how to specify read docker-compose only containers in docker-compose :

 version: "3" services: redis: image: redis:alpine read_only: true 
+152
Dec 01 '13 at 22:11
source share
β€” -

docker

Here is the correct way to specify a read docker-compose only volume in docker-compose :

 version: "3" services: my_service: image: my:image volumes: - type: volume source: volume-name target: /path/in/container read_only: true volumes: volume-name: 

https://docs.docker.com/compose/compose-file/#long-syntax-3

+5
Apr 19 '19 at 10:43 on
source share



All Articles