简体   繁体   中英

Is that possible adding the missing file to mount volume when docker container start?

I have docker image and there is a directory ' A ' which is mount in host directory ' hostA '. In directory **hostA** , there is a file file0 . When I build the docker image, in directy A , there is a file file1 . To be more clear, I show them as below:

In host: 
hostA:
   - file0
In docker container: 
A:
   - file1

When I start the docker container with the docker image. Since the dir A is mounted in host dir **hostA** and hostA existed when container starting, the file1 in A will not appear although it is build into the docker image. Instead file0 will be available in dir A since it is in hostA

My question is whether there is an approach (either during building the docker image or starting the docker container), make the file1 be also available in dir A (and in host dir hostA ). It means after container starts, it will be

In host: 
hostA:
   - file0
   - file1
In docker container: 
A:
   - file0
   - file1

Thanks in advance.

No, not as far as I know. As Docker volume mounts are working the same as regular unix mounts, you won't see the file in your Docker container. Normally, you separate those directories which will change during the lifetime of your container (like your source code during local development) and those files which don't (eg node_modules when working with JavaScript.

I can outline you two ideas which get close to your desired solution and might work for you.

Mounting to a different directory

You could split your container directory A into AA and AB . AA contains the file which were added during build time while AB contains files which you're mounting in your container. Then, you can reference the mounted file from the file in your container using relative paths like ../AB/some_file .

# /A/AA/file1.js
require '../AB/file0';
In host: 
hostA:
   - file0
In docker container: 
A:
  AA:
    - file1
  AB: # mounted
    - file0

Mounting a single file

As another alternative, you can mount a single file. That's a valid solution if you only need to mount a couple of files (like configurations) into your container.

Docker:

-v "PWD"/hostA/file0:/A/file0

docker-compose:

somes_service:
  volumes:
    - hostA/file0:/A/file0

Still, the file from your container file1 won't be available on your host machine.

In that case, you could set the mounted file to be read only, as well.

hostA/file0:/A/file0:ro

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM