Skip to content

Commit

Permalink
SDK-285: Cannot create Docker container on Linux (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Jun 11, 2021
1 parent 92344a5 commit f9fbc50
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 41 deletions.
20 changes: 17 additions & 3 deletions docker-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<description>OpenMRS SDK allows for rapid development of OpenMRS modules.</description>
<url>https://wiki.openmrs.org/display/docs/OpenMRS+SDK</url>

<properties>
<docker-java.version>3.2.8</docker-java.version>
</properties>

<dependencies>
<!-- Maven -->
<dependency>
Expand Down Expand Up @@ -47,13 +51,18 @@
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>3.0.6</version>
<version>${docker-java.version}</version>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>${docker-java.version}</version>
</dependency>

<dependency>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<version>2.8.0</version>
<version>2.12.3</version>
</dependency>

<!--mysql-->
Expand Down Expand Up @@ -83,7 +92,12 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.1</version>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import org.apache.commons.lang.SystemUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.components.interactivity.Prompter;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

Expand All @@ -21,7 +24,7 @@ abstract class AbstractDockerMojo extends AbstractMojo {
protected static final String DEFAULT_MYSQL_EXPOSED_PORT = "3308";
protected static final String DEFAULT_MYSQL_PASSWORD = "Admin123";
protected static final String MYSQL_5_6 = "mysql:5.6";
protected static final String DEFAULT_MYSQL_DBURI = "jdbc:mysql://localhost:3307/";
protected static final String DEFAULT_MYSQL_DBURI = "jdbc:mysql://localhost:3308/";
protected static final String API_VERSION = "1.18";
protected static final String DEFAULT_HOST_LINUX = "unix:///var/run/docker.sock";

Expand Down Expand Up @@ -63,14 +66,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {

public abstract void executeTask() throws MojoExecutionException, MojoFailureException;

protected void resolveDocker() {

protected void resolveDocker() throws MojoExecutionException {
DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost(dockerHost)
.withApiVersion(API_VERSION)
.build();

docker = DockerClientBuilder.getInstance(config).build();
try {
docker = DockerClientBuilder.getInstance(config).withDockerHttpClient(
new ApacheDockerHttpClient.Builder().dockerHost(new URI(dockerHost)).build()
).build();
}
catch (URISyntaxException e) {
throw new MojoExecutionException("Docker host \"" + dockerHost + "\" is not a valid URI.", e);
}

try {
docker.infoCmd().exec();
Expand All @@ -90,28 +98,17 @@ protected Container findContainer(String id){
for (Container container : containers) {
if (container.getId().equals(id)) {
return container;
} else {
List<String> containerNames = Arrays.asList(container.getNames());
// on Linux name is prepended with '/'
if (containerNames.contains(id) || containerNames.contains("/" + id)) {
return container;
} else if (container.getLabels().containsKey(id)) {
return container;
}
}
}

for (Container container: containers) {
if (Arrays.asList(container.getNames()).contains(id)) {
return container;
}
}

//on Linux name is prepended with '/'
String idWithSlash = "/" + id;
for (Container container: containers) {
if (Arrays.asList(container.getNames()).contains(idWithSlash)) {
return container;
}
}

for (Container container: containers) {
if (container.getLabels().containsKey(id)) {
return container;
}
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.VolumesFrom;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -49,7 +54,6 @@ public class CreateMySql extends AbstractDockerMojo {

@Override
public void executeTask() throws MojoExecutionException, MojoFailureException {

if (StringUtils.isBlank(port)) port = DEFAULT_MYSQL_EXPOSED_PORT;
//root password may be blank but not null, if user wants to have empty password
if (rootPassword == null) rootPassword = DEFAULT_MYSQL_PASSWORD;
Expand All @@ -61,7 +65,6 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
if (findContainer(DEFAULT_MYSQL_CONTAINER) == null) {
createMysqlContainer(docker);
}

}

private boolean noMySqlImage(DockerClient docker) {
Expand All @@ -72,25 +75,24 @@ private boolean noMySqlImage(DockerClient docker) {
private void createMysqlContainer(DockerClient docker) {
if (container == null) container = DEFAULT_MYSQL_CONTAINER;

docker.createVolumeCmd().withName(container + "-data").exec();

ExposedPort tcp3306 = ExposedPort.tcp(3306);
Ports portBindings = new Ports();
portBindings.bind(tcp3306, new Ports.Binding("localhost", port));
PortBinding portBinding = new PortBinding(new Ports.Binding("localhost", port), ExposedPort.tcp(3306));

Map<String, String> labels = new HashMap<>();
labels.put(container, "true");

Volume volume = new Volume(container + "-data:/var/lib/mysql");
Volume volume = new Volume("/var/lib/mysql");
Bind boundVolume = new Bind(container + "-data", volume);

HostConfig hostConfig = new HostConfig()
.withPortBindings(portBinding)
.withBinds(boundVolume);

docker.createContainerCmd(MYSQL_5_6)
.withExposedPorts(tcp3306)
.withPortBindings(portBindings)
.withHostConfig(hostConfig)
.withName(container)
.withEnv("MYSQL_ROOT_PASSWORD="+rootPassword)
.withAttachStdout(true)
.withLabels(labels)
.withVolumes(volume)
.exec();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void promptForNewServerIfMissing(Server server) {
int indx = 0;
while (new File(Server.getServersPath(), defaultServerId).exists()) {
indx++;
defaultServerId = DEFAULT_SERVER_NAME + String.valueOf(indx);
defaultServerId = DEFAULT_SERVER_NAME + indx;
}
String serverId = promptForValueIfMissingWithDefault("Specify server id (-D%s)", server.getServerId(), "serverId", defaultServerId);
server.setServerId(serverId);
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.3.3</version>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.3.3</version>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand Down

0 comments on commit f9fbc50

Please sign in to comment.