This repository has been archived by the owner on Jun 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from ContainerSolutions/bug/47-inner-docker-proxy
Setup inner docker proxy on Mac
- Loading branch information
Showing
5 changed files
with
179 additions
and
17 deletions.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
src/main/java/org/apache/mesos/mini/mesos/InnerDockerProxy.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.apache.mesos.mini.mesos; | ||
|
||
import com.github.dockerjava.api.DockerClient; | ||
import com.github.dockerjava.api.command.CreateContainerCmd; | ||
import com.github.dockerjava.api.model.ExposedPort; | ||
import com.github.dockerjava.api.model.Link; | ||
import com.github.dockerjava.api.model.PortBinding; | ||
import org.apache.mesos.mini.container.AbstractContainer; | ||
|
||
public class InnerDockerProxy extends AbstractContainer { | ||
|
||
private static final String DOCKER_IMAGE = "mwldk/go-tcp-proxy"; | ||
|
||
private static final int PROXY_PORT = 2377; | ||
|
||
private MesosContainer mesosContainer; | ||
|
||
public InnerDockerProxy(DockerClient dockerClient, MesosContainer mesosContainer) { | ||
super(dockerClient); | ||
this.mesosContainer = mesosContainer; | ||
} | ||
|
||
@Override | ||
protected void pullImage() { | ||
pullImage(DOCKER_IMAGE, "latest"); | ||
} | ||
|
||
@Override | ||
protected CreateContainerCmd dockerCommand() { | ||
return dockerClient | ||
.createContainerCmd(DOCKER_IMAGE) | ||
.withLinks(Link.parse(mesosContainer.getContainerId() + ":docker")) | ||
.withExposedPorts(ExposedPort.tcp(mesosContainer.getDockerPort())) | ||
.withPortBindings(PortBinding.parse("0.0.0.0:" + getProxyPort() + ":" + mesosContainer.getDockerPort())) | ||
.withCmd("-l=:" + mesosContainer.getDockerPort(), "-r=docker:" + mesosContainer.getDockerPort()); | ||
} | ||
|
||
public int getProxyPort() { | ||
return PROXY_PORT; | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.apache.mesos.mini; | ||
|
||
import com.github.dockerjava.api.DockerClient; | ||
import com.github.dockerjava.api.command.CreateContainerCmd; | ||
import com.github.dockerjava.api.model.Container; | ||
import org.apache.mesos.mini.container.AbstractContainer; | ||
import org.apache.mesos.mini.docker.PrivateDockerRegistry; | ||
import org.apache.mesos.mini.mesos.MesosClusterConfig; | ||
import org.apache.mesos.mini.mesos.MesosContainer; | ||
import org.junit.Test; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Test inner docker client. | ||
* Should instantiate container INSIDE the daemon of the MesosContainer (i.e. not directly visible) | ||
* The only way to do this on a mac is via a proxy. | ||
*/ | ||
public class InnerDockerTest { | ||
@Test | ||
public void shouldStart() throws InterruptedException { | ||
MesosClusterConfig config = MesosClusterConfig.builder().defaultDockerClient() | ||
.slaveResources(new String[]{"ports(*):[9200-9200,9300-9300]", "ports(*):[9201-9201,9301-9301]", "ports(*):[9202-9202,9302-9302]"}) | ||
.build(); | ||
PrivateDockerRegistry registry = new PrivateDockerRegistry(config.dockerClient, config); | ||
registry.start(); | ||
MesosContainer mesosContainer = new MesosContainer(config.dockerClient, config, registry.getContainerId()); | ||
mesosContainer.start(); | ||
|
||
HelloWorldNoRemoveContainer helloWorldContainer = new HelloWorldNoRemoveContainer(mesosContainer.getInnerDockerClient()); | ||
helloWorldContainer.start(); | ||
|
||
List<Container> containers = mesosContainer.getInnerDockerClient().listContainersCmd().exec(); | ||
assertEquals(1, containers.size()); | ||
} | ||
|
||
class HelloWorldNoRemoveContainer extends AbstractContainer { | ||
|
||
public static final String HELLO_WORLD_IMAGE = "tutum/hello-world"; | ||
|
||
protected HelloWorldNoRemoveContainer(DockerClient dockerClient) { | ||
super(dockerClient); | ||
} | ||
|
||
// Override remove method to do nothing. We are force removing in the mesosContainer. | ||
@Override | ||
public void remove() { | ||
} | ||
|
||
@Override | ||
protected void pullImage() { | ||
pullImage(HELLO_WORLD_IMAGE, "latest"); | ||
} | ||
|
||
@Override | ||
protected CreateContainerCmd dockerCommand() { | ||
return dockerClient.createContainerCmd(HELLO_WORLD_IMAGE).withName("hello-world_" + new SecureRandom().nextInt()); | ||
} | ||
|
||
} | ||
} |