Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volumes and docker-compose

I'm trying to create a docker-compose.yml file that contains a --volumes-from instruction. Does anyone know the syntax?

I have been looking online for some time now, and it appears that the --volumes-from command is only available as a docker command. I hope I'm wrong.

like image 626
Vingtoft Avatar asked Feb 29 '16 13:02

Vingtoft


People also ask

What does volumes in Docker-compose do?

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

Do Docker-compose volumes persist?

Whichever you choose, once you have set up a volume to the folder where the data is stored in the container, if you do a docker-compose down , and then a docker-compose up , your data will not be erased and it will become persistent.

Does Docker-compose down destroy volumes?

Stops containers and removes containers, networks, volumes, and images created by up .

Does Docker-compose create volume directories?

Running docker-compose up results in creation of directories specified via volumes directive on the host system.


1 Answers

February 2016:

The docs/compose-file.md mentions:

Mount all of the volumes from another service or container, optionally specifying read-only access(ro) or read-write(rw).

(If no access level is specified, then read-write will be used.)

volumes_from:
 - service_name
 - service_name:ro
 - container:container_name
 - container:container_name:rw

For instance (from this issue or this one)

version: "2"

services:
...
  db:
    image: mongo:3.0.8
    volumes_from:
      - dbdata
    networks:
      - back
    links:
      - dbdata

 dbdata:
    image: busybox
    volumes:
      - /data/db

Note August 2017: with docker-compose version 3, regarding volumes:

The top-level volumes key defines a named volume and references it from each service’s volumes list.
This replaces volumes_from in earlier versions of the Compose file format. See Use volumes and Volume Plugins for general information on volumes.

Example:

version: "3.2"
services:
  web:
    image: nginx:alpine
    volumes:
      - type: volume
        source: mydata
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: ./static
        target: /opt/app/static

  db:
    image: postgres:latest
    volumes:
      - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
      - "dbdata:/var/lib/postgresql/data"

volumes:
  mydata:
  dbdata:

This example shows a named volume (mydata) being used by the web service, and a bind mount defined for a single service (first path under db service volumes).

The db service also uses a named volume called dbdata (second path under db service volumes), but defines it using the old string format for mounting a named volume.

Named volumes must be listed under the top-level volumes key, as shown.

like image 168
VonC Avatar answered Sep 29 '22 07:09

VonC