codechurn
codechurn

Reputation: 3970

docker-compose volumes syntax for local driver to mount a file

I am attempting to mount a file (nginx.conf) in the volumes section of my docker-compose.yml. I can mount a directory as a volume without issue, however, it is not clear to me what the syntax is for a file.

I have the following defined in my volumes section

volumes:
    roundcubeweb:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: /mnt/docker/volumes/roundcube/html
    nginxconf:
      driver: local
      driver_opts:
        type: none    
        o: bind
        device: /mnt/docker/volumes/roundcube/nginx.conf

Later on, I have the following under my services section

nginx:
  image: nginx:latest
  deploy:
    replicas: 1
  restart: always
  depends_on: 
    - roundcube
  ports:
    - "8082:80"
  volumes:
    - roundcubeweb:/var/www/html
    - nginxconf:/etc/nginx/conf.d/default.conf

When I attempt to start this up, I receive the following error:

ERROR: for roundcube_nginx_1 Cannot create container for service nginx: failed to mount local volume: mount /mnt/docker /volumes/roundcube/nginx.conf:/var/lib/docker/volumes/roundcube_nginxconf/_data, flags: 0x1000: not a directory

ERROR: for nginx Cannot create container for service nginx: failed to mount local volume: mount /mnt/docker/volumes/rou ndcube/nginx.conf:/var/lib/docker/volumes/roundcube_nginxconf/_data, flags: 0x1000: not a directory ERROR: Encountered errors while bringing up the project.

I've found that if I inline the file location in the nginx service section's volume declaration then it works just fine. For example:

   volumes:  
    - roundcubeweb:/var/www/html  
    - /mnt/docker/volumes/roundcube/nginx.conf:/etc/nginx/conf.d/default.conf  

Can one not mount a file in the volumes section? Is there a reference for the local driver's parameters?

Upvotes: 5

Views: 9553

Answers (1)

Pierre B.
Pierre B.

Reputation: 12933

I don't think it is possible to have a standalone Docker volume point to a specific file rather than a directory. It may be related to a Docker design on how volumes are managed and mounted (tried to find a doc or related piece of code but could not find any)

local driver's parameter seems to take similar parameter as Linux mount commands. It seems implied in Docker volume doc though not very clearly:

the local driver accepts mount options as a comma-separated list in the o parameter


I've found that if I inline the file location in the nginx service section's volume declaration then it works just fine. For example:

volumes:  
 - roundcubeweb:/var/www/html  
 - /mnt/docker/volumes/roundcube/nginx.conf:/etc/nginx/conf.d/default.conf 

In your example you are doing a Bind Mound rather than using Volumes, which is handled differently by Docker and more suited for your use case of mounting a local file in the container. I think using Bind Mount is the appropriate solution in your case.

Upvotes: 2

Related Questions