docker-compose to share host volumes for containers

docker-compose to share host volumes for containers

When I was trying to share a folder on the host to the container using relative path, it was always unsuccessful.

My docker-compose.yml is as below:

    nginx:
      container_name: nginx
      build:
        context: ./nginx
        dockerfile: nginx.dockerfile      
      volumes: 
        - ../data/wwwroot:/data/www:rw
        - ../logs:/var/log/nginx
        - ./nginx/conf/vhost:/etc/nginx/conf.d
        - ./nginx/nginx.conf:/etc/nginx/nginx.conf: ro

Notice that I have a dirctory structure as below:

+---data
    +-----wwwroot
+---logs
+---services
    +-----nginx
          +-----conf
                +-----vhost
                ------nginx.conf
          ------nginx.dockerfile

After I run docker-compose up I got the following error:

hokhykdeMacBook-Pro:services hok$ docker-compose up
 ERROR: The Compose file './docker-compose.yml' is invalid because:
 services.nginx.volumes 'type' is a required property
 hokhykdeMacBook-Pro:services hok$ 

This is because I have a permission indicator rw and ro . I just deleted it for simplicity.

As I want to share the same data\wwroot and logs directories for all these containers, so I’d like to find a better way to do so.

After digging the Internet for half a day or so with effort trying all kinds of methods suggested either in docker official documents or other sources, I finally found these two articles clear and useful. Attached here:

  1. https://github.com/moby/moby/issues/19990#issuecomment-442846650
  2. http://blog.code4hire.com/2018/06/define-named-volume-with-host-mount-in-the-docker-compose-file/

The first article illustrates volumes quite clear. The second one actually solves the problem of relative path usage.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.