简体   繁体   English

无法在Docker中挂载卷

[英]Cannot mount volume in docker

I am trying to use docker-compose to mount my local directory a new directory at the root level on my docker container. 我正在尝试使用docker-compose在Docker容器的根级别将本地目录挂载到新目录。 Supposedly this is quite a simple thing to do. 据说这很简单。

This is what my docker-compose file looks like: 这是我的docker-compose文件的样子:

version: '2'
services:
  web:
    build: ./docker/web/
    expose:
      - 80
    volumes:
      - .:/contracts

The Dockerfile in docker/web literally looks like this (I plan to do more later): 从字面上看,docker / web中的Dockerfile看起来像这样(我打算以后再做):

FROM php:5.6-apache
RUN /contracts/provision/web.sh

But I keep getting the error that web.sh cannot be found. 但是我不断收到找不到web.sh的错误。 Putting various ls commands and the like in my Dockerfile show that no such directory called "contracts" is being created on the container. 在我的Dockerfile中放置各种ls命令之类的命令,表明没有在容器上创建名为“ contracts”的目录。 If I try with an existing directly like mnt , I get the same result - the directory remains empty. 如果我尝试直接使用像mnt这样的现有目录,则会得到相同的结果-目录仍然为空。

But a docker inspect shows that it is apparently mounted! 但是docker inspect显示它显然已安装!

"Mounts": [
    {
        "Type": "bind",
        "Source": "/vagrant",
        "Destination": "/contracts",
        "Mode": "rw",
        "RW": true,
        "Propagation": ""
    }
],

I am running docker-compose on a vagrant box running Ubuntu 14.04. 我在运行Ubuntu 14.04的无聊的盒子上运行docker-compose。

Any help would be hugely appreciated, I am at the end of my tether. 任何帮助将不胜感激,我已经束手无策了。 Thanks! 谢谢!

Volumes are attached to containers, containers are created from images, and images are created from the build command. 将卷附加到容器,从映像创建容器,并从build命令创建映像。 While you're running the build, the volume mount won't exist, so you won't see it during your RUN commands. 运行构建时,该卷挂载将不存在,因此在RUN命令期间不会看到它。

At build time, instead of mounting a volume, you would COPY or ADD this directory into the image. 在构建时,您无需COPY卷,而可以将该目录COPYADD到映像中。 You would include something like this in your Dockerfile before the RUN command: 您将在RUN命令之前在Dockerfile中包含以下内容:

COPY . /contracts

Note that if you do that and mount a volume over that same location at runtime, you will hide whatever exists inside the image at that location. 请注意,如果执行此操作并在运行时在同一位置上安装卷,则将隐藏该位置映像中存在的任何内容。 So separate what parts are commands and scripts that need to be part of the image from data that needs to be part of a persistent volume, and only copy the build time scripts into the image. 因此,将需要成为映像一部分的命令和脚本与需要成为持久卷一部分的数据分开,然后仅将构建时间脚本复制到映像中。

With your running container, you can try a docker exec -it $container_id ls /contracts to see if it exists as expected there. 使用正在运行的容器,您可以尝试使用docker exec -it $container_id ls /contracts来查看它是否按预期存在。

If the directory exists but is empty, the most likely cause is if you have docker on a VM or remote host and you are running your commands from a different environment. 如果目录存在但为空,则最可能的原因是如果您在虚拟机或远程主机上安装了docker,并且正在其他环境中运行命令。 The folder needs to exist on the docker host (VM) itself to be mounted into the container. 该文件夹需要存在于Docker主机(VM)本身上才能安装到容器中。 Docker for Win and Docker for Mac have settings to share specific folders or drives into the VM which may need adjusting if you do your builds in a different location than expected. Docker for Win和Docker for Mac具有将特定的文件夹或驱动器共享到VM的设置,如果您在与预期不同的位置进行构建,则可能需要进行调整。 And be careful with case-sensitivity that applies to linux filenames. 并且请注意适用于linux文件名的区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM