3
头图

Remember the real-time update of the volume mount in the docker container

1. View the container configuration

docker inspect [container id or name]
 "Mounts": [
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub",
                "Destination": "/app",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub\\deploy\\php.ini",
                "Destination": "/opt/bitnami/php/conf/php.ini",
                "Mode": "",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],

The value of Propagation is rprivate, the reason why the folder is not synchronized in real time is because of this, the official document has a form, I post it here

Propagation settingDescription
sharedSub-mounts of the original mount are exposed to replica mounts, and sub-mounts of replica mounts are also propagated to the original mount.
slavesimilar to a shared mount, but only in one direction. If the original mount exposes a sub-mount, the replica mount can see it. However, if the replica mount exposes a sub-mount, the original mount cannot see it.
privateThe mount is private. Sub-mounts within it are not exposed to replica mounts, and sub-mounts of replica mounts are not exposed to the original mount.
rsharedThe same as shared, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rslaveThe same as slave, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rprivateThe default. The same as private, meaning that no mount points anywhere within the original or replica mount points propagate in either direction.

Let me translate roughly:

Setting typeDescription
sharedAny container mounted with this directory will be updated in real time with the host in both directions, and the scope of the current directory, excluding subdirectories
slaveAny container that mounts this directory will receive real-time updates, and the update is one-way; from the host to the container, the scope of the current directory, excluding subdirectories
privateAny containers with this directory mounted will not receive real-time updates
rsharedSame as shared, the scope also includes subdirectories
rslaveSame as slave, the scope also includes subdirectories
rprivateSame as private, the scope also includes subdirectories

2. Solution

When we develop locally, we want the files to be updated in real time, so we need to set it to rslave.

  1. When docker starts, you can directly configure it as rslave
 docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  --mount type=bind,source="$(pwd)"/target,target=/app2,readonly,bind-propagation=rslave \
  nginx:latest
  1. You can modify the configuration file docker-compose.yml when using docker-compose up
volumes:
      - type: bind
        source: ./
        target: /app
        read_only: false
        bind:
          propagation: rslave
      - type: bind
        source: ./deploy/php.ini
        target: /opt/bitnami/php/conf/php.ini
        read_only: true
        bind:
          propagation: rslave

Volumes do not use abbreviations, these configurations are fine, after the configuration is complete, look at the container properties

"Mounts": [
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub",
                "Destination": "/app",
                "Mode": "",
                "RW": true,
                "Propagation": "rslave"
            },
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub\\deploy\\php.ini",
                "Destination": "/opt/bitnami/php/conf/php.ini",
                "Mode": "",
                "RW": false,
                "Propagation": "rslave"
            }
        ],

In this case, the file is updated in real time, but it is one-way, from the host to the container, what you want to change can be based on your needs.

Make a record of the configuration of this real-time update of the docker folder, hoping to help everyone.


akazwz
100 声望4 粉丝

Hello World!