Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
JUnit @Rule for starting a docker container from within an Integration test
package rules;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.ContainerCreation;
import com.spotify.docker.client.messages.HostConfig;
import com.spotify.docker.client.messages.PortBinding;
import org.eclipse.jdt.launching.SocketUtil;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
* Created with IntelliJ IDEA.
* User: moshe
* Date: 9/23/14
* Time: 11:57 AM
public class DockerContainerRule extends ExternalResource {
public static final String DOCKER_SERVICE_URL = "http://localhost:2375";
private final DockerClient dockerClient;
private final HostConfig hostConfig;
private final Map<String, String> externalServicePorts = Maps.newHashMap();
private ContainerCreation container;
public DockerContainerRule(String imageName, Map<String, Map.Entry<String, String>> portMapping, String[] cmd) {
this(imageName, portMapping, DOCKER_SERVICE_URL, cmd);
public DockerContainerRule(String imageName, Map<String, Map.Entry<String, String>> portMapping, String dockerServiceUrl, String[] cmd) {
List<String> exposedPorts = new ArrayList<>();
for (Map.Entry<String, Map.Entry<String, String>> entry : portMapping.entrySet()) {
exposedPorts.add(entry.getValue().getKey() + "/tcp");
ContainerConfig containerConfig = ContainerConfig.builder()
.exposedPorts(exposedPorts.toArray(new String[exposedPorts.size()]))
dockerClient = new DefaultDockerClient(dockerServiceUrl);
Map<String, List<PortBinding>> portBindings = Maps.newHashMap();
// you can leave the host IP empty for the PortBinding.of first parameter
for (Map.Entry<String, Map.Entry<String, String>> entry : portMapping.entrySet()) {
String externalPort = isSet(entry.getValue().getValue()) ? entry.getValue().getValue() : Integer.toString(SocketUtil.findFreePort());
externalServicePorts.put(entry.getKey(), externalPort);
portBindings.put(entry.getValue().getKey() + "/tcp", Lists.newArrayList(PortBinding.of("", externalPort)));
hostConfig = HostConfig.builder()
try {
container = dockerClient.createContainer(containerConfig);
} catch (DockerException | InterruptedException e) {
private boolean isSet(String value) {
return value != null && !value.equals("");
public Map<String, String> getExternalServicePorts() {
return externalServicePorts;
public Statement apply(Statement base, Description description) {
return super.apply(base, description);
protected void before() throws Throwable {
dockerClient.startContainer(, hostConfig);
protected void after() {
try {
dockerClient.removeContainer(, true);
} catch (DockerException | InterruptedException e) {

This comment has been minimized.

Show comment Hide comment

huksley Nov 8, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment