Can't access a volume during building a docker

2019-08-30 04:48发布

问题:

I am trying to create a docker container with a volume called 'example', which has some files inside it, but I can't access it during build. Here are the files I am using:

# docker-compose.yml
version: "3"
services:
  example:
    build: .
    volumes:
      - "./example:/var/example"
    stdin_open: true
    tty: true

And:

# Dockerfile
FROM ubuntu
RUN ls /var/example
CMD ["/bin/bash"]

When I run:

sudo docker-compose up

It gives me an error:

ls: cannot access /var/example: No such file or directory

But when I delete the RUN command from the Dockerfile and run sudo docker-compose up again, and then run:

docker exec -it c949eef14fcd /bin/bash # c949eef14fcd is the id of the created container
ls /var/example

... in another terminal window, there is no error, and I can see all the files of the example directory. Why?

回答1:

Sorry, I have just found out that volumes are not accessible during build. They are accessible during run of the container, which is said here in the point 9. But when I changed my Dockerfile to this:

# Dockerfile
FROM ubuntu:14.04
CMD ["ls", "/var/example"]

... it worked perfectly well and printed out all the files inside the example folder.