Can I mount a volume from a Dockerfile?

Is it possible to mount a volume from a Dockerfile? if not what is the best way to get this done?

I know you can do this using a normal Docker commands in the command prompt, but is there a better way to get this done?

  • Determine if Docker image is signed or unsigned
  • open() file failed (13: Permission denied) in nginx & docker & minikube
  • Linking containers together on production deploys
  • Docker running Windows applications
  • Docker containers keep losing internet
  • PHP - how precisely should I define version
  • How can I create ubuntu based docker host by using docker-machine with VirtualBox?
  • what triggers Elastic Beanstalk to pull in an updated Docker image
  • How to install gdbserver package on Alpine Docker image?
  • Cadvisor support ELK stack
  • Unable to delete untagged images:Error response from daemon: Conflict, 511136 wasn't deleted FATA[0000] Error: failed to remove one or more images
  • Is supervisord needed for docker+gunicorn+nginx?
  • 2 Solutions collect form web for “Can I mount a volume from a Dockerfile?”

    Unfortunately it is not possible to do so during an image build.

    I think the reason could be portability? I’m not quite sure here – will be nice if someone can explain this further.

    But my guess is that if Docker would to allow users to mount volumes on build then Docker will have to anticipate and handle different types of file systems, e.g Windows vs Unix. Also what if the directory path which it is supposed to be used for mounting does not exists on another host?

    Anyway, I’m not quite sure what your use case is but you should be able to use the ADD or COPY commands to move files from your host’s file system into the image during build and use the moved files for other purpose like installing etc.

    See:
    https://docs.docker.com/engine/reference/builder/#/add

    Q: So what is the best approach?
    A: I think there is no standard approach for mounting volume. I usually define my mounting in the docker-compose file.

    See:
    https://docs.docker.com/compose/compose-file/#/volumes-volume-driver

    Within the context of a Dockerfile, you have limited access to host resources, to the extent of what’s needed to actually build the container’s image. To illustrate, should you be able to mount host volumes, start containers or access their resources, that would probably equate to an image that can be built for you, in your current environment only.

    If you really need host resources mounted as a volume for the duration of the build only, you might want to look into Packr. However, if you want the volume to be available when the container starts, then usually it’s fine to somehow use docker run --name=Foo $ARGS.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.