Docker Community Forums

Share and learn in the Docker community.

Docker compose - how to use nfs volumes?

Hi,

I am not sure how to use nfs volumes in docker-compose file ?

For example consider the below example and please guide me

version: ‘2’

services:

PHP-FPM

php-fpm:
    build:
        context: ./docker/php-fpm/${PHP_VERSION}
    volumes_from:
        - volumes_source
    expose:
        - "9000"
    env_file: .env

Web source

volumes_source:
    image: tianon/true
    volumes:
        - ${HOST_APP_DIRECTORY}:/var/www/

Data source

volumes_data:
    image: tianon/true
    volumes:
        - ./data/mysql:/var/lib/mysql

The docker volume create reference implies that you can docker volume create --driver local and create a Docker volume that is a specific mounted filesystem. There’s even a specific example for NFS there.

I haven’t tested this specifically, but I think it should look roughly like the following:

version: '2'
volumes:
  nfs:
    driver: local
    driver_opts:
      type: nfs
      o: addr=192.168.1.1,rw
      device: ":/path/to/dir"
services:
  php-fpm:
    volumes:
      - nfs:/path/in/container
    ...

I’m pretty sure starting a container just to provide a volume isn’t considered a best practice any more (I do see it mentioned as an example in the documentation, though).

Given that your previous step build: a container from source, I’d definitely include your application in the container and not try to “inject” it into the container like this.

Are you looking for the standard mysql image?

don’t forget that in order to use NFS the Docker host would need nfs-client and nfs-common modules installed. This is not the case for all Docker hosts running on Moby Linux - docker for mac , aws and windows

I had a similar problem and here is how I solved it
https://www.vip-consult.solutions/post/persistent-storage-docker-swarm-nfs#content