简体   繁体   中英

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. Supposedly this is quite a simple thing to do.

This is what my docker-compose file looks like:

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):

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

But I keep getting the error that web.sh cannot be found. Putting various ls commands and the like in my Dockerfile show that no such directory called "contracts" is being created on the container. If I try with an existing directly like mnt , I get the same result - the directory remains empty.

But a docker inspect shows that it is apparently mounted!

"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.

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. While you're running the build, the volume mount won't exist, so you won't see it during your RUN commands.

At build time, instead of mounting a volume, you would COPY or ADD this directory into the image. You would include something like this in your Dockerfile before the RUN command:

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.

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. The folder needs to exist on the docker host (VM) itself to be mounted into the container. 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. And be careful with case-sensitivity that applies to linux filenames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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