Class VolumeBindingUtil
- java.lang.Object
-
- io.fabric8.maven.docker.util.VolumeBindingUtil
-
public class VolumeBindingUtil extends Object
Utility methods for working with Docker volume bindings.This class provides explicit support for relative binding paths. This means that the plugin configuration or docker compose file can specify a relative path when configuring a volume binding. Methods in this class will examine volume binding strings in a
RunVolumeConfigurationand resolve any relative paths in the host portion of volume bindings. Examples of relative bindings include:- A host path relative to the current working directory
- ./relative/path:/absolute/container/path
- A host path relative to the current working directory
- relative/path/:/absolute/container/path
- A host path relative to the parent of the current working directory
- ../relative/path:/absolute/container/path
- A host path equal to the current user's home directory
- ~:/absolute/container/path
- A host path relative to the current user's home directory
- ~/relative/path:/absolute/container/path
Understand that the following is not considered a relative binding path, and is instead interpreted as a named volume:
relis interpreted as a named volume. Use./relorrel/to have it interpreted as a relative path.- rel:/absolute/container/path
Volume bindings that specify an absolute path for the host portion are preserved and returned unmodified.
-
-
Constructor Summary
Constructors Constructor Description VolumeBindingUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringresolveRelativeVolumeBinding(File baseDir, String bindingString)Resolves relative paths in the suppliedbindingString, and returns a binding string that has relative paths replaced with absolute paths.static voidresolveRelativeVolumeBindings(File baseDir, RunVolumeConfiguration volumeConfiguration)Iterates over eachbindingin thevolumeConfiguration, and resolves any relative paths in the binding strings usingresolveRelativeVolumeBinding(File, String).
-
-
-
Method Detail
-
resolveRelativeVolumeBinding
public static String resolveRelativeVolumeBinding(File baseDir, String bindingString)
Resolves relative paths in the suppliedbindingString, and returns a binding string that has relative paths replaced with absolute paths. If the suppliedbindingStringdoes not contain a relative path, it is returned unmodified.Discussion:
Volumes may be defined inside of
serviceblocks as documented here:volumes: # Just specify a path and let the Engine create a volume - /var/lib/mysql # Specify an absolute path mapping - /opt/data:/var/lib/mysql # Path on the host, relative to the Compose file - ./cache:/tmp/cache # User-relative path - ~/configs:/etc/configs/:ro # Named volume - datavolume:/var/lib/mysql"
This method only operates on volume strings that are relative: beginning with
./,../, or~. Relative paths beginning with./or../are absolutized relative to the suppliedbaseDir, which must be absolute. Paths beginning with~are interpreted relative tonew File(System.getProperty("user.home")), andbaseDiris ignored.Volume strings that do not begin with a
./,../, or~are returned as-is.Examples:
Given
baseDirequal to "/path/to/basedir" and abindingStringstring equal to "./reldir:/some/other/dir", this method returns/path/to/basedir/reldir:/some/other/dirGiven
baseDirequal to "/path/to/basedir" and abindingStringstring equal to "../reldir:/some/other/dir", this method returns/path/to/reldir:/some/other/dirGiven
baseDirequal to "/path/to/basedir" and abindingStringstring equal to "~/reldir:/some/other/dir", this method returns/home/user/reldir:/some/other/dirGiven
baseDirequal to "/path/to/basedir" and abindingStringequal to "src/test/docker:/some/other/dir", this method returns/path/to/basedir/src/test/docker:/some/other/dirGiven a
bindingStringequal to "foo:/some/other/dir", this method returnsfoo:/some/other/dir, becausefoois considered to be a named volume, not a relative path.- Parameters:
baseDir- the base directory used to resolve relative paths (e.g. beginning with./,../,~) present in thebindingString; must be absolutebindingString- the volume string from the docker-compose file- Returns:
- the volume string, with any relative paths resolved as absolute paths
- Throws:
IllegalArgumentException- if the suppliedbaseDiris not absolute
-
resolveRelativeVolumeBindings
public static void resolveRelativeVolumeBindings(File baseDir, RunVolumeConfiguration volumeConfiguration)
Iterates over eachbindingin thevolumeConfiguration, and resolves any relative paths in the binding strings usingresolveRelativeVolumeBinding(File, String). ThevolumeConfigurationis modified in place, with any relative paths replaced with absolute paths.Relative paths are resolved relative to the supplied
baseDir, which must be absolute.- Parameters:
baseDir- the base directory used to resolve relative paths (e.g. beginning with./,../,~) present in the binding string; must be absolutevolumeConfiguration- the volume configuration that may contain volume binding specifications- Throws:
IllegalArgumentException- if the suppliedbaseDiris not absolute
-
-