com.elastisys.scale.commons.util.docker.ContainerConfig Maven / Gradle / Ivy
package com.elastisys.scale.commons.util.docker;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Arrays.asList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.base.MoreObjects;
/**
* Describes a {@link Docker} container to be launched.
*
* @see Docker
*/
public class ContainerConfig {
/** Docker image of form: {@code IMAGE[:TAG|@DIGEST]} */
private final String image;
/** Port mappings of form {@code [:]}. */
private final List portMappings;
/** Environment variables. */
private final Map env;
private ContainerConfig(String image, List portMappings,
Map env) {
this.image = image;
this.portMappings = portMappings;
this.env = env;
}
/**
* Docker image of form: {@code IMAGE[:TAG|@DIGEST]}
*
* @return
*/
public String getImage() {
return this.image;
}
/**
* Port mappings of form {@code [:]}.
*
* @return
*/
public List getPortMappings() {
return this.portMappings;
}
/**
* Environment variables.
*
* @return
*/
public Map getEnv() {
return this.env;
}
/**
* Converts this {@link ContainerConfig} to its corresponding
* {@code docker run} command-line arguments.
*
* @return
*/
public List toRunCommandArgs() {
List cmdArgs = new ArrayList<>();
cmdArgs.addAll(asList("docker", "run", "-d"));
for (String portMapping : this.portMappings) {
cmdArgs.addAll(asList("-p", portMapping));
}
for (String envKey : this.env.keySet()) {
cmdArgs.addAll(asList("-e",
String.format("%s=%s", envKey, this.env.get(envKey))));
}
cmdArgs.add(this.image);
return cmdArgs;
}
/**
* Creates a new {@link ContainerConfigBuilder}.
*
* @return
*/
public static ContainerConfigBuilder builder() {
return new ContainerConfigBuilder();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("image", this.image)
.add("portMappings", this.portMappings).add("env", this.env)
.toString();
}
/**
* Builder of {@link ContainerConfig}.
*/
public static class ContainerConfigBuilder {
/** Docker image of form: {@code IMAGE[:TAG|@DIGEST]} */
private String image;
/** Port mappings of form {@code [:]}. */
private List portMappings = new ArrayList<>();
/** Environment variables. */
private Map env = new HashMap<>();
public ContainerConfig build() {
checkArgument(this.image != null, "no docker image given");
return new ContainerConfig(this.image, this.portMappings, this.env);
}
/**
* Sets a Docker image of form: {@code IMAGE[:TAG|@DIGEST]}
*
* @param image
* @return
*/
public ContainerConfigBuilder image(String image) {
this.image = image;
return this;
}
/**
* Adds a port mapping of form {@code [:]}.
*
* @param portMapping
* @return
*/
public ContainerConfigBuilder portMapping(String portMapping) {
this.portMappings.add(portMapping);
return this;
}
/**
* Adds an environment variable.
*
* @param key
* @param value
* @return
*/
public ContainerConfigBuilder env(String key, String value) {
this.env.put(key, value);
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy