How to create a volume of a single file on Docker?

Asked

Viewed 393 times

0

I want to synchronize a file that is inside my container with my host. I thought of simply creating a volume for the file, getting that way:

version: '3.7'
services:
    deathrun:
        image: "ceifa/lory-gmod-deathrun"
        volumes:
            - ./stateful/deathrun/sv.db:/server/garrysmod/sv.db
        restart: always
        tty: true

However, as the file does not exist on the host the first time it runs, it is being automatically created a folder in place of the file and giving the following error:

ERROR: for deathrun Cannot start service deathrun: "..." caused "not a directory": Unknown: Are you trying to mount a directory onto a file (or vice versa)? Check if the specified host path exists and is the expected type

How to make it grab the container file instead of trying to create it on the host?

  • Can you verify the answer? Don’t forget to accept it if you are satisfied with it.

2 answers

1

To synchronize a single file you must use the absolute path in the volume setting. For example:

volumes:
  - /home/user/stateful/deathrun/sv.db:/server/garrysmod/sv.db

or else

volumes:
  - $pwd/stateful/deathrun/sv.db:/server/garrysmod/sv.db

1

There are some differences in how volumes work on Windows hosts and Linux/Mac.

If you are using Windows (host), NAY it is possible to map files, only directories.

And anyway, whether in Windows or not, the ideal is to create the directory or the file before to use mapping, Docker will assume that it is a directory and can create it (https://github.com/moby/moby/issues/13121).

  • The sad thing is having to keep creating files before running the container. I would like it to be totally plug-n-play. Is there no other way?

  • You can map a directory and let the application create the file.

  • @Francisco Vc can map a directory and let the application create the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.