galakt
galakt

Reputation: 1439

Mount a windows host directory in compose file version 3

I trying to upgrade docker-compose.yml from version 1 to version 3.

Main question about

volumes_from: To share a volume between services, 
define it using the top-level volumes option and 
reference it from each service that shares it using the 
service-level volumes option.

Simplest example:

version "1"

data:
  image: postgres:latest
  volumes:
    - ./pg_hba.conf/:/var/lib/postgresql/data/pg_hba.conf

postgres:
  restart: always
  image:  postgres:latest
  volumes_from:
    - data
  ports:
    - "5432:5432"

If I have understood correctly, should be converted to

version: "3"

services:
  db:
    image: postgres:latest
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - appn

  networks:
    appn:

  volumes:
    db-data:?

Question: How now in top-level volumes option i can set relative path to folder "example_folder" from windows host to "db-data" ?

Upvotes: 1

Views: 1228

Answers (1)

VonC
VonC

Reputation: 1323055

In this instance, you might consider not using volumes_from.

As mentioned in this docker 1.13 issue by Sebastiaan van Stijn (thaJeztah):

The volumes_from is basically a "lazy" way to copy volume definitions from one container to another, so;

docker run -d --name one -v myvolume:/foo image-one

docker run -d --volumes-from=one image-two

Is the same as running;

docker run -d --name one -v myvolume:/foo image-one
docker run -d --name two -v myvolume:/foo image-two

If you are deploying to AWS you should not use bind-mounts, but use named volumes instead (as in my example above), for example;

version: "3.0"

services:
  db:
    image: nginx
    volumes:
      - uploads-data:/usr/share/nginx/html/uploads/

volumes:
  uploads-data:

Which you can run with docker-compose;

docker-compose up -d
Creating network "foo_default" with the default driver
Creating volume "foo_uploads-data" with default driver
Creating foo_db_1

Basically, it is not available in docker compose version 3:

There's a couple of reasons volumes_from is not ported to the compose-file "3";

  • In a swarm, there is no guarantee that the "from" container is running on the same node. Using volumes_from would not lead to the expected result.
    This is especially the case with bind-mounts, which, in a swarm, have to exist on the host (are not automatically created)
  • There is still a "race" condition (as described earlier)
  • The "data" container has to use exactly the right paths for volumes as the "app" container that uses the volumes (i.e. if the "app" uses the volume in /some/path/in/container, then the data container also has to have the volume at /some/path/in/container). There are many cases where the volume may be shared by multiple services, and those may be consuming the volume in different paths.

But also, as mentioned in issue 19990:

The "regular" volume you're describing is a bind-mount, not a volume; you specify a path from the host, and it's mounted in the container. No data is copied from the container to that path, because the files from the host are used.

For a volume, you're asking docker to create a volume (persistent storage) to store data, and copy the data from the container to that volume.

Volumes are managed by docker (or through a plugin) and the storage path (or mechanism) is an implementation detail, as all you're asking is a storage, that's managed.

For your question, you would need to define a docker volume container and copy your host content in it:

services:
  data:
    image: "nginx:alpine"
    volumes:
      - ./pg_hba.conf/:/var/lib/postgresql/data/pg_hba.conf    

Upvotes: 1

Related Questions