|
|
@@ -16,10 +16,7 @@ import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.time.Duration;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Docker 客户端
|
|
|
@@ -179,19 +176,32 @@ public class DockerImpl implements Docker {
|
|
|
public InspectContainerResponse createAndRun(String containerName, Config containerConfig) throws Exception {
|
|
|
stopAndDelete(containerName);
|
|
|
|
|
|
- HostConfig hostConfig = HostConfig.newHostConfig()
|
|
|
- .withNetworkMode("host")
|
|
|
- .withRestartPolicy(RestartPolicy.unlessStoppedRestart());
|
|
|
String image = containerConfig.getImage();
|
|
|
CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(image)
|
|
|
- .withName(containerName)
|
|
|
- .withHostConfig(hostConfig);
|
|
|
+ .withName(containerName);
|
|
|
|
|
|
List<String> env = containerConfig.getEnv();
|
|
|
if (env != null) {
|
|
|
createContainerCmd.withEnv(env);
|
|
|
}
|
|
|
|
|
|
+ HostConfig hostConfig = HostConfig.newHostConfig()
|
|
|
+ .withNetworkMode("host")
|
|
|
+ .withRestartPolicy(RestartPolicy.unlessStoppedRestart());
|
|
|
+ if (containerConfig.getVolumes() != null) {
|
|
|
+ Map<String, String> map = containerConfig.getVolumes().getMap();
|
|
|
+ List<Bind> list = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+ Volume volume2 = new Volume(value);
|
|
|
+ Bind bind = new Bind(key, volume2);
|
|
|
+ list.add(bind);
|
|
|
+ }
|
|
|
+ hostConfig.withBinds(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ createContainerCmd.withHostConfig(hostConfig);
|
|
|
CreateContainerResponse response = createContainerCmd.exec();
|
|
|
String containerId = response.getId();
|
|
|
dockerClient.startContainerCmd(containerId).exec();
|