Skip to content

Commit

Permalink
Merge pull request #226 from testcontainers/docker-compose-mutex
Browse files Browse the repository at this point in the history
Serialize all Docker Compose operations
  • Loading branch information
rnorth committed Sep 25, 2016
2 parents 30183eb + 3d3f104 commit dd6209c
Showing 1 changed file with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> e
private Map<String, Integer> scalingPreferences = new HashMap<>();
private DockerClient dockerClient;

private static final Object MUTEX = new Object();

/**
* Properties that should be passed through to all Compose and ambassador containers (not
* necessarily to containers that are spawned by Compose itself)
Expand Down Expand Up @@ -79,12 +81,13 @@ public void starting(Description description) {
profiler.setLogger(logger());
profiler.start("Docker compose container startup");

pullImages();
applyScaling(); // scale before up, so that all scaled instances are available first for linking
createServices();
registerContainersForShutdown();
startAmbassadorContainers(profiler);

synchronized (MUTEX) {
pullImages();
applyScaling(); // scale before up, so that all scaled instances are available first for linking
createServices();
registerContainersForShutdown();
startAmbassadorContainers(profiler);
}
}

private void pullImages() {
Expand Down Expand Up @@ -184,19 +187,22 @@ private Logger logger() {
@Override @VisibleForTesting
public void finished(Description description) {

// shut down all the ambassador containers
ambassadorContainers.forEach((String address, AmbassadorContainer container) -> container.stop());

// Kill the services using docker-compose
getDockerCompose("down -v")
.start();
synchronized (MUTEX) {
// shut down all the ambassador containers
ambassadorContainers.forEach((String address, AmbassadorContainer container) -> container.stop());

// remove the networks before removing the containers
ResourceReaper.instance().removeNetworks(identifier);
// Kill the services using docker-compose
getDockerCompose("down -v")
.start();

// remove the networks before removing the containers
ResourceReaper.instance().removeNetworks(identifier);

// kill the spawned service containers
spawnedContainerIds.forEach(id -> ResourceReaper.instance().stopAndRemoveContainer(id));
spawnedContainerIds.clear();
// kill the spawned service containers
spawnedContainerIds.forEach(id -> ResourceReaper.instance().stopAndRemoveContainer(id));
spawnedContainerIds.clear();
}
}

public SELF withExposedService(String serviceName, int servicePort) {
Expand Down

0 comments on commit dd6209c

Please sign in to comment.