Home > Software engineering >  Docker container bind host directory
Docker container bind host directory

Time:08-24

I wish I could update the settings of the application by changing the local profile. I use "volume" to bind a local directory, for example:

docker run -v D:\test:/app

But when the container is running, all files in /app are emptied, because D:\test does not have any files. Is there any way I can achieve my goal

CodePudding user response:

Your question is a bit unclear. I guess you're problem is the following: You want to bind mount your app directory, but it is initially empty and will stay empty since the bind mount overwrites everything put into /app during build

I usually use two different ways:

  1. Put your profile into host directory D:\test (if applicable). This is also a viable strategy for e.g. the source code of nodejs apps
  2. During build, put your profile into /app_temp. Then create an entry point which moves /app_temp into /app. If you want to persist the profile through multiple build/run phases, it has to be inside the build context (which is likely not D:\test) on your host.

CodePudding user response:

You need to change a bit the way your application is organized. put all the settings in their own directory and have the application read them from there. Then you can map only the settings folder you the host one.

Another option is to map the host folder to a temporary folder inside the container and have the ENTRYPOINT script update your files (by copying them over) and then run your application.

Docker was not meant to be used for the workflow you are trying to setup and for this reason you need to do some extra work.

CodePudding user response:

because D:\test does not have any files.

That the way it works. Volume type you use is bind mount, i.e. you mount file system, using mount point mapped to a host directory. According to documentation:

With bind mounts, we control the exact mountpoint on the host. We can use this to persist data, but it’s often used to provide additional data into containers.

You have two options here (both imply host data should exist in advance):

  1. Bind to a folder, containing configuration data. As you showed.

  2. It is possible to bind only file:

    docker run -v D:\test\config.json:/app/config.json

While binding to a file, if it does not exist beforehand, docker-daemon would think it is a directory and will create directory, both in container and on the host.

you mount file system, using mount point mapped to a host directory

Hence, if host directory is empty mounted file system would also be empty.

  • Related