Last active
March 2, 2017 07:19
-
-
Save watsh-rajneesh/47228aec2068b6dbb53fe103b305777e to your computer and use it in GitHub Desktop.
Docker image execution in Java
This file contains 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
package edu.sjsu.amigo.cp.docker; | |
import com.spotify.docker.client.DefaultDockerClient; | |
import com.spotify.docker.client.DockerClient; | |
import com.spotify.docker.client.LogStream; | |
import com.spotify.docker.client.exceptions.DockerException; | |
import com.spotify.docker.client.messages.ContainerConfig; | |
import com.spotify.docker.client.messages.ContainerCreation; | |
import com.spotify.docker.client.messages.RegistryAuth; | |
import edu.sjsu.amigo.cp.api.CommandExecutionException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Logger; | |
import static com.spotify.docker.client.DockerClient.LogsParam.stderr; | |
import static com.spotify.docker.client.DockerClient.LogsParam.stdout; | |
/** | |
* Docker command executor task. | |
* | |
* @author rwatsh on 2/14/17. | |
*/ | |
public class DockerTask { | |
private static final Logger logger = Logger.getLogger(DockerTask.class.getName()); | |
/** | |
* Executes a containerized command on AWS. | |
*/ | |
public String execute(String dockerImage, List<String> envList, List<String> commandList, String entryPoint) throws CommandExecutionException { | |
try( DockerClient dockerClient = new DefaultDockerClient("unix:///var/run/docker.sock")) { | |
String response = null; | |
pullImage(dockerClient, dockerImage); | |
final ContainerConfig containerConfig = ContainerConfig.builder() | |
.image(dockerImage) | |
.env(envList) | |
.entrypoint(entryPoint) | |
.cmd(commandList) | |
.build(); | |
final ContainerCreation container = dockerClient.createContainer(containerConfig); | |
final String containerId = container.id(); | |
dockerClient.startContainer(containerId); | |
// Wait for the container to exit. | |
// If we don't wait, docker.logs() might return an epmty string because the container | |
// cmd hasn't run yet. | |
dockerClient.waitContainer(containerId); | |
final String log; | |
try (LogStream logs = dockerClient.logs(containerId, stdout(), stderr())) { | |
log = logs.readFully(); | |
} | |
logger.info(log); | |
response = log; | |
dockerClient.removeContainer(containerId); | |
return response; | |
} catch (DockerException | InterruptedException e) { | |
throw new CommandExecutionException(e); | |
} | |
} | |
private void pullImage(DockerClient docker, String imageRepoName) throws DockerException, InterruptedException { | |
final RegistryAuth registryAuth = RegistryAuth.builder() | |
// TODO change reading from env var to value read from user profile in DB | |
.email(System.getenv("AUTH_EMAIL")) | |
.username(System.getenv("AUTH_USERNAME")) | |
.password(System.getenv("AUTH_PASSWORD")) | |
.build(); | |
final int statusCode = docker.auth(registryAuth); | |
logger.info("Docker registry auth status: " + statusCode); | |
docker.pull(imageRepoName, registryAuth); | |
} | |
public static void main(String[] args) throws CommandExecutionException { | |
DockerTask t = new DockerTask(); | |
List<String> envList = new ArrayList<>(); | |
envList.add("AWS_DEFAULT_REGION="+ System.getenv("AWS_DEFAULT_REGION")); | |
envList.add("AWS_ACCESS_KEY_ID="+ System.getenv("AWS_ACCESS_KEY_ID")); | |
envList.add("AWS_SECRET_ACCESS_KEY="+ System.getenv("AWS_SECRET_ACCESS_KEY")); | |
List<String> cmdList = new ArrayList<>(); | |
cmdList.add("s3"); | |
cmdList.add("ls"); | |
/** | |
* Using sjsucohort6/docker_awscli docker image to run a container which has | |
* awscli installed to execute awscli commands without having to install awscli. | |
* A similar thing can be done with sjsucohort6/oracle-java:8 image to run any java | |
* application without installing java. | |
*/ | |
t.execute("sjsucohort6/docker_awscli:latest", envList, cmdList, "aws"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment