Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add :cached or :delegated into a docker-compose.yml volumes list?

Title says it all. I've got a few volumes set up as readonly (:ro) but want to test :cached and :delegated for helping with file i/o performance, but couldn't figure out how to set this up in a compose file.

Oh, I already tested:

volumes: - external:internal:cached

like image 414
red Avatar asked May 08 '17 09:05

red


People also ask

What is delegated in Docker compose?

Use delegated : when docker container performs changes, host is in read only mode. Use default : When both container and host actively and continuously perform changes on data. Ensure you use an updated docker-compose and docker version on your machine.

Which command displays a list of volumes created by docker?

docker volume ls | Docker Documentation.


2 Answers

Explanation: The purpose of using volumes configuration on docker is to share data between the host and the docker container and ensure data consistency between both (What happens in A(host/container) is represented in B(host/container) and vice versa) . The mounted volume is "part" of the container and relevant. The common usage is to store shared data backup both in container and on file system in machine. If the container is removed, volume still exists and is independent of container state it will be reused and loaded from last persisted state.

TLDR:

  • Use cached: when the host performs changes, the container is in read only mode.
  • Use delegated: when docker container performs changes, host is in read only mode.
  • Use default: When both container and host actively and continuously perform changes on data.
  • Ensure you use an updated docker-compose and docker version on your machine

From Documentation:

Mac uses osxfs to propagate directories and files shared from macOS to the Linux VM. This propagation makes these directories and files available to Docker containers running on Docker Desktop for Mac. **By default, these shares are fully-consistent, meaning that every time a write happens on the macOS host or through a mount in a container, the changes are flushed to disk so that all participants in the share have a fully-consistent view.

Full consistency can severely impact performance in some cases.** Docker 17.05 and higher introduce options to tune the consistency setting on a per-mount, per-container basis. The following options are available:

The consistency option, if present, may be one of consistent, delegated, or cached. This setting only applies to Docker Desktop for Mac, and is ignored on all other platforms.

The Docker volumes flags are:

  1. consistent or default: The default setting with full consistency, as described above.
  2. delegated: The container runtime’s view of the mount is authoritative. There may be delays before updates made in a container are visible on the host. When you use it? For example You use it when the container changes the data continuously and you want to backup this data on the host, this is read only operation on host perspective and therefore the right choice would be delegated.
  3. cached: The macOS host’s view of the mount is authoritative. There may be delays before updates made on the host are visible within a container. When you use it? For example when your host continuously changes data that the container service reads and uses it ( like configuration / source code / rendered data from server etc ...)

Usage:

- <my-first-host-volume>:<first-container-volume-path>:delegated - <my-second-host-volume>:<second-container-volume-path>:cached 

Example:

version: '3.4' services:    jenkins:      image: jenkins/jenkins:lts      environment:        - JENKINS_HOME=/var/jenkins_home      container_name: jenkins        volumes:        - '~/jenkins/:/var/jenkins_home:delegated'        - '~/environment_keys:/var/data:cached'      ports:        - 0.0.0.0:8080:8080      expose:        - 5000        restart: unless-stopped 
like image 192
avivamg Avatar answered Sep 18 '22 19:09

avivamg


In my case, I wanted a readonly (:ro) volume that’s also :cached or :delegated. To do this, you simply use this syntax:

volumes:  - /external/folder:/internal/folder:ro,cached 
like image 34
Florian Wendelborn Avatar answered Sep 20 '22 19:09

Florian Wendelborn