Created
December 30, 2016 03:52
-
-
Save rkuzsma/57ca2478786c0b05025e5ecd78290ee1 to your computer and use it in GitHub Desktop.
Utility for launching local containers via docker-compose.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DockerCompose { | |
private final File dockerSrc; | |
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |
public DockerCompose(String dockerSrcDir) { | |
dockerSrc = new File(dockerSrcDir); | |
if (!dockerSrc.exists()) { | |
throw new RuntimeException("Docker source path does not exist: " + dockerSrcDir); | |
} | |
logger.info("Docker source path: " + dockerSrc.getAbsolutePath()); | |
} | |
public void invoke(String cmd) { | |
try { | |
String command = "docker-compose " + cmd; | |
logger.info("$ " + command); | |
String line; | |
ProcessBuilder builder = new ProcessBuilder() | |
.command(command.split(" ")) | |
.directory(dockerSrc).redirectErrorStream(true); | |
Process p = builder.start(); | |
try { | |
int errCode = p.waitFor(); | |
if (errCode != 0) { | |
throw new RuntimeException("docker-compose returned exit code " + errCode); | |
} | |
} | |
catch (InterruptedException e) { | |
throw new RuntimeException("docker-compose interrupted"); | |
} | |
BufferedReader input = | |
new BufferedReader | |
(new InputStreamReader(p.getInputStream())); | |
while ((line = input.readLine()) != null) { | |
logger.info("> " + line); | |
} | |
input.close(); | |
} | |
catch (IOException e) { | |
throw new RuntimeException("Docker-Compose error", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment