Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between volumes-from and volumes?

Tags:

docker

I saw the docker-compose patterns but I'm confused. What is the best way to make composed containers. When should I use link, or volumes_from. When should I use volumes_from, volumes

#1 app-db-data app: image: someimage link: - db // data volume container name db: image: mysql volumes_from: - data // data volume name data: image: someimage volumes: - {host data}:{guest data}

#2 app-db+data app: image: someimage link: - db // data volume container name db: image: mysql volumes: - data // data file name

app #1 app-service-data

app:     image: someimage     volumes_from:         - service // service container name  service:     image: mysql     volumes_from:         - data // image container name data:     image: someimage     volumes:         - {host data}:{guest data}  

#2 app-service+data

app:     image: someimage     volumes_from:         - service // service container name  service:     image: mysql     volumes:         - data // mounted file 

Thanks

like image 484
Tomoaki Sato Avatar asked Mar 27 '15 17:03

Tomoaki Sato


People also ask

What are volumes used for?

Quite simply, volumes are directories (or files) that are outside of the default Union File System and exist as normal directories and files on the host filesystem. This will make the directory /data inside the container live outside the Union File System and directly accessible on the host.

How many types of volumes are there in docker?

There are two types of volumes to consider: Named volumes have a specific source from outside the container, for example awesome:/bar . Anonymous volumes have no specific source so when the container is deleted, instruct the Docker Engine daemon to remove them.

What are docker volumes used for?

Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container. The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.

What is named volume in docker?

Declaring and referencing a named volume in a docker-compose file will create an empty volume which may then be accessed and shared by the services saying so in their volumes section. If you want to share a named volume, you have to declare this volume in the top-level volume section of your docker-compose file.


2 Answers

Link and volumes_from are different concepts. Links are used when you need to connect (by network) two containers. In this case if you want to connect an App to the Database, the way to do this is by using a link, since applications use a port and host to connect to a database (not a directory on the filesystem).

Volumes and volumes_from differ in that the first one only declares volumes that docker will make persistent or host:guest mounts, but volumes_from tells docker to use a volumes that is already declared on another host (making it available to this host).

Of those 4 cases that you present, I think that the first and second are good choices. In the first you are creating a data only container, and make the mysql container use it. In the second case the data and the mysql container are the same.

Links and volumes are perfectly explained in the docker documentation.

Hope it helps.

like image 187
Jorge Marey Avatar answered Sep 18 '22 19:09

Jorge Marey


In short:

  1. volumes_from mounts from other containers.
  2. volumes mounts defined inline.
  3. links connects containers.

A little bit more explained:

  1. volumes_from mounts volumes from other containers. For example if you have data only containers and you want to mount these data only containers in the container that has your application code.

  2. volumes is a the inline way to define and mount volumes. If you read #17798 you can see that named volumes can replace data only containers in most cases.

    The simplest is then to use volumes. Since you can reuse them by naming them.

  3. links is different. Because it does not mount. Instead it connects containers. So if you do:

    app:   container_name: app_container   links:     - db 

    That means that if you connect to app_container with docker exec -it app_container bash and try ping db you will see that container is able to resolve ip for db.

    This is because docker creates a network between containers.

like image 30
sites Avatar answered Sep 21 '22 19:09

sites