Mounting specific file from a volume into the container using docker-compose

I would like to mount specific file from a volume into the container.

For example: I would want to mount .ash_history from volume alpine_ash_history at /root/.ash_history

How would I do that?

Here’s my experimental docker-compose.yml if it helps:

services:
    eph:
        image: alpine
        command: ["/bin/sh", "-c", "sleep", "3d"]
        volumes:
            - alpine_ash_history:.ash_history:/root/.ash_history"

volumes:
    alpine_ash_history:
        external: true

Would be great if this would be possible. Unfortunately it is not.

1 Like

Any potential workarounds that come to mind?

The only workaround I can see is if you map the volume to a temporary location inside the container and use a symlink or mount --bind (which doesn’t make sense in this scenario) inside the container, There is no workaround on docker level itself.

You can either use (named) volumes the way they are, or use binds. Binds allows mapping an arbitrary host path or file into an arbitrary container path or file.

Depending on the use case, using a bind might be an alternative.

1 Like

Could I request you to show me the syntax for the YAML file in the OP, please?

Instead of declaring a volume and using its handle in the service, you simply use the host path to the file you want to bind into the container.