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
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.
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.
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.
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.
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.
In short:
volumes_from
mounts from other containers.volumes
mounts defined inline.links
connects containers.A little bit more explained:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With