简体   繁体   中英

Docker / Docker-compose volume fill & share issue

I have a few questions about Docker volumes. I have installed Docker and docker-compose on a fresh host running debian stretch. I managed to get a docker-compose file running for a simple nginx/php-fpm project, both containers mounted on the directory containing the source code. I wanted to try to create a single volume that would be shared across my containers but I have a few issue, and my understanding of the official documentation is not helping.

So this is an idea of what I'm trying to achieve :

在此处输入图片说明

Question 1 : Trying to create a volume from a dockerfile on a directory mounted from host

docker-compose.yml :

version: '3'
services:
    php:
        build:
            context: .
            dockerfile: php.dockerfile
        volumes:
            - ./host-project-directory:/project

php.dockerfile :

FROM php:7-fpm
VOLUME project

from my understanding, when running docker-compose we should have a volume created on host containing all files from /project from container. And /project from container should contain all files from ./host-project-directory from host. If I ls the content of /project on container I can see the files from host, but using docker volume list , there are no volumes created on host, why ?

Question 2 : How to populate and use this volume from another container ?

version: '3'
services:
    php:
        build:
            context: .
            dockerfile: php.dockerfile
        volumes:
            - named-volume:/project
    web:
        image: nginx
        links:
            - php
        volumes:
            - named-volume:/project
volumes:
    named-volume:

This should create a volume called 'named-volume' and bind it to /project directories on both containers php and web . Now, how to populate this volume with content from ./host-project-directory ? I've tried adding a dockerfile like

ADD ./host-project-directory /project

But nothing changed and the volume remained empty.

I'm sorry if this is due to my lack of experience using Docker but I can't figure out how to make this simple thing work.

Thank you for your time !

For the first question, I try a simple docker file like this:

FROM php:7-fpm
COPY ./project /project

And a docker-compose like this:

version: '3'
services:
    php:
        build: .
        volumes:
            - named-volume:/project
    web:
        image: nginx
        links:
            - php
        volumes:
            - named-volume:/project
volumes:
    named-volume:

Since you create the volume on docker-compose you don't need to create that in the Dockerfile. Running docker volume list, I'm able to see the volume created with a local driver. Making ls inside the folder I'm also able to see the file. It's important to note, that the file present in you local directory it's not the same that the file inside the container. So if you edit the files in the host this will not change the files in container. That's because you have your volume created in another path, probably at: /var/lib/docker/volumes/... This happens because you map the volume to the path, but you not specifies where you want the volume. To do that just make your docker-compose like this:

version: '3'
services:
    php:
        build: .
        volumes:
            - ./project:/project
    web:
        image: nginx
        links:
            - php
        volumes:
            - ./project:/project

Making this I'm still able to see the volume with the volume list command but without a name. So I don't know why you are not able to see the volume in the list.

For question 2:

Doing the example above I have the files inside the container that exists in my local "project" folder.

Please check that the path to the local folder is correct.

A bind mount is not the same thing as a volume . You're defining a named volume here, but wanting the functionality of a bind mount.

Try this

version: '3'
services:
    php:
        build:
            context: .
            dockerfile: php.dockerfile
        volumes:
            - ./host-project-directory:/project
    web:
        image: nginx
        links:
            - php
        volumes:
            - ./host-project-directory:/project

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