How to mount a file in a container that is inaccessible before the first launch?

I am trying to create a Dockerfile for a webapp that uses a file database. I would like to be able to mount the file from the host * The file is at the root of the complete software installation, so it is not ideal for mounting this full directory.

Another problem is that before the first use, the database file has not yet been created. The first time user will not have a database, but another user can do this. I cannot "mount" anything during assembly. I suppose.

Perhaps it could work as follows:

  • Start of the first / new database:
    • Run the container (without mounting).
    • Webapp creates a database.
    • Stop container
  • subsequent launches:
    • Run the container with -v to mount the file

It would be better if an extra start / stop is not needed for the user. Even so, I’m still looking for a way to make it user-friendly, perhaps using the “methods” to launch it (maybe I can determine the thing with the first load as part of docker-compose, as well as the “regular” method? )

How can I do this in simpel mode so that it is cleared for all users for the first time?

* The reason is that you can copy the Dockerfile and the database file as a backup and only be run with these two items.

** How to mount host volumes in docker containers in Dockerfile during build

+5
source share
1 answer

One approach that might work:

  • Run the database in the assembly file so that it has time to create the default file before exiting.
  • Declare VOLUME in the Docker file for the file after the above statement. This will cause the file to be copied when the container is running, provided that you have not explicitly specified the host path.
  • Use data containers, not volumes. Thus, normal use will be:

      docker run --name data_con my_db echo "my_db data container" docker run -d --volumes-from data_con my_db ... 

The first container should exit immediately, but install the one that is used in the second container.

+1
source

All Articles