Add bind mount to Dockerfile just like volume

我的未来我决定 提交于 2019-12-04 08:24:46

问题


I want to add the bind mount to docker file just like I initialise a volume inside Dockefile. Is there any way for it?


回答1:


A Dockerfile defines how an image is built, not how it's used - so you can't specify the bind mount in a Dockerfile. Try using docker-compose instead. A simple docker-compose.yml that mounts a directory for you would look like this:

version: '3.1'

services:
  mycontainer:
    image: myimage
    build: .
    volumes:
      - './path/on/docker/host:/path/inside/container'  

The build: . is optional if you're building the image by some other means, but sometimes it's handy to do it all in one.

Run this with docker-compose up -d




回答2:


In addition to what the other answers say:

Because bind mounts provide access to the host filesystem, allowing them to be embedded into an image would be a huge security risk. Consider an image that purports to be, say, a web server, but in fact bind mounts your /etc/passwd and /etc/shadow and then sends them off to a remote server.

Or one that bind mounts /lib/ld-linux.so and then overwrites it, thus breaking your entire system.

For these reasons, you cannot embed a a bind mount in your Dockerfile. Similarly, you cannot specify host port mappings, host device access, or any other similar attributes in the Dockerfile.




回答3:


Simple answer is no.

A basic design principle for docker images is portablility. Bind mounts are hosts specific since the mounted folder is defined on the host machine. Thus this contradicts with the portablility requirement for Docker images.



来源:https://stackoverflow.com/questions/47942016/add-bind-mount-to-dockerfile-just-like-volume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!