dockerdocker-image

Can I mount a Docker image as a volume in Docker?


I would like to distribute some larger static files/assets as a Docker image so that it is easy for user to pull those optional files down the same way they would be pulling the app itself. But I cannot really find a good way to expose files from one Docker image to the other? Is there a way to mount a Docker image itself (or a directory in it) as a volume to other Docker container?

I know that there are volume plugins I could use, but I could not find any where I could to this or something similar?


Solution

  • Is possible create any directory of an image to a docker volume, but not full image. At least not in a pretty or simple way.

    If you want to create a directory from your image as a docker volume you can create a named volume:

    docker volume create your_volume
    docker run -d \
      -it \
      --name=yourcontainer \
      -v your_volume:/dir_with_data_you_need \
      your_docker_image
    

    From this point, you'll have accessible your_volume with data from image your_docker_image

    Reason why you cannot mount the whole image in a volume is because docker doesn't let specify / as source of named volume. You'll get Cannot create container for service your-srv: invalid volume spec "/": invalid volume specification: '/' even if you try with docker-compose.