4

After a long search, I cant find the answer or solution to the question if I can mount or use volume in docker-compose to bind a directory from a remote host?

The examples are always using local directories or volumes.

To clarify: I try to establish a blue green deployment with one Database on a different VM then the 2 app servers. The app and its components will be deployed using docker, and also need access to the same directory where things will be saved, so both blue and green can access this saved files. My thought was to create this directory on the database server as this will be used kind of in the same way. Now I am trying to mount or use the volume syntax in docker-compose so that the directory on the db server can be accessed by the backend containers. The containers that needs access to the directory are based on ubuntu images if that matters.

Is this the right way? and then how do I assign that in my docker-compose.yml or Dockerfile? Or is there a better way to accomplish this?

Thanks in advance

Foxdemon
  • 45
  • 1
  • 7

1 Answers1

2

Docker allows you to mount a filesystem into the container using the local volume driver to anything you can access with the mount syscall. The syscall is almost identical to the mount command in Linux. However, when you do something like an NFS mount, you'll see a bit of how the mount command preprocesses that command into the syscall.

Here are a few examples of different ways to perform an NFS volume mount in docker:

  # create a reusable volume
  $ docker volume create --driver local \
      --opt type=nfs \
      --opt o=nfsvers=4,addr=nfs.example.com,rw \
      --opt device=:/path/to/dir \
      foo

  # or from the docker run command
  $ docker run -it --rm \
    --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=nfs,\"volume-opt=o=nfsvers=4,addr=nfs.example.com\",volume-opt=device=:/host/path \
    foo

  # or to create a service
  $ docker service create \
    --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=nfs,\"volume-opt=o=nfsvers=4,addr=nfs.example.com\",volume-opt=device=:/host/path \
    foo

  # inside a docker-compose file
  ...
  volumes:
    nfs-data:
      driver: local
      driver_opts:
        type: nfs
        o: nfsvers=4,addr=nfs.example.com,rw
        device: ":/path/to/dir"
  ...

For more details on all the options and potential pitfalls, see this answer on a similar question.

BMitch
  • 231,797
  • 42
  • 475
  • 450