Can I create a docker volume where files and folders cannot be overriden or deleted?

Viewed 82

Is it possible to be able to write files in my volume, only if they have not already exited and to never delete my previous files ?

I have a Dockerfile which creates an image called bot. In the image, I have a folder /models which can be empty. After the image is built, it is pushed to a Nexus/Dockerhub repository.

To build my application on my VM, I have a docker-compose.yml which ,among other things, called the bot image by also specify that the folder /models should be in a volume.

My issue is that, I already have files (file_1, file_2, ...) on my VM in /models, if I do docker-compose down and then docker-compose up -d I keep these files because of my volume. But if I have a new version of my image bot, when I do docker-compose up -d I will call the new version of my image (which is almost what I want) but I will erase the folder /models in my volume with the new /models folder from my image.

Thus, I would want to find a way to write file in my volume, only if they have not already exited and to never delete my previous files.

version: "3.2"

services:
  bot:
    restart: always
    tty: true
    volumes:
      - type: volume
        source: ModelsVolume
        target: /models
        volume:
          nocopy: true

volumes:
  ModelsVolume:

1 Answers

Along the lines of @masseyb's comment, I'd perform any file checks at image build time and write a small file (maybe even the shell script to execute) inside the image that tells me if the model files are writeable or not. Then the script you call in your ENTRYPOINT can use that file to execute the appropriate copy logic.

It's also possible for your compose shell script to perform host-level checks by running the container and looking for the file your build step wrote within it.

Hope this helps!

Related