Remember that multiple containers can mount the same volume, and it can be mounted read-write for some of them and read-only for others, at the same time.
Cloning From An Existing Container But, if you do need to add a volume to a running container, you can use docker commit to make a new image based on that container, and then clone it with the new volume. Then, you can run the new image, replacing the old image with the cloned one.
You can mount host volumes by using the -v flag and specifying the name of the host directory. Everything within the host directory is then available in the container. What's more, all the data generated inside the container and placed in the data volume is safely stored on the host directory.
You can specify that a volume should be read-only by appending :ro
to the -v
switch:
docker run -v volume-name:/path/in/container:ro my/image
Note that the folder is then read-only in the container and read-write on the host.
According to the Use volumes documentation, there is now another way to mount volumes by using the --mount
switch. Here is how to utilize that with read-only:
$ docker run --mount source=volume-name,destination=/path/in/container,readonly my/image
Here is an example on how to specify read-only containers in docker-compose
:
version: "3"
services:
redis:
image: redis:alpine
read_only: true
Here is a proper way to specify read-only volume in docker-compose
:
version: "3.2" # Use version 3.2 or above
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With