Docker Community Forums

Share and learn in the Docker community.

Set nfs voume with ip not hard-coded in docker-compose.yml file


(Guysoft) #1

Hey all,
I have a docker-compose.yml that is committed in to a git version control.
It has an nfs volume set in it, and it looks as follows.

The issue is that the nfs ip address is hard-coded, which means if the IP if the nfs server change I need to commit a bunch of files.
Is there a way to get this value externally?
I understand that env_file can’t be set inside a volumes scope, so I am not sure how to do this with an env file for several docker-compoe files.

Current docker-compose.yml :

version: '3.6'

services:
hello:
    build:
    context: .
    restart: always
    volumes:
    - type: volume
        source: data
        target: /srv/volume
        volume:
        nocopy: true

    environment:
    tty: true

volumes:
data:
    driver_opts:
    type: "nfs"
    o: "addr=192.168.1.1,nolock,soft,rw"
    device: ":/srv/volume"

(Gerryken) #2

https://docs.docker.com/compose/environment-variables/#the-env-file Maybe the IP address could be a variable set in environment file?
I tried this locally in my own environment and it seems to be fine with variables substituted by actual values configured in .env file.


(Yehuda Deutsch) #3

env_file will not help for variables outside the relevant service, but you can still use environment variables from the shell docker-compose is called, from the docs: “Compose uses the variable values from the shell environment in which docker-compose is run.”

So either:

export NFS_IP=10.100.100.100
docker-compose up -d

Or

NFS_IP=10.100.100.100 docker-compose up -d

And in docker-compose.yml:

volumes:
  data:
    driver_opts:
      type: nfs
      o: "addr=${NFS_IP},nolock,soft,rw"
      device: ":/srv/volume"

(Guysoft) #4

Ok, so they can be set outside the docker-compose.yml. But then they have to still be set in .env which has to be in the same folder in the docker-compose.yml.

So I can’t really have a single point to update when the IP changed.
Its better though because .env is not committed.

Any ways to make those .env files take settings from a shared location?


(Guysoft) #5

env_file: can’t be set in the volumes section, which means two docker-compose.yml files cant share the same .env