1. 开启docker 远程访问,
2. 开启docker swarm
上述内容不在详细说明
<!-- Docker依赖 -->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<!-- use latest version https://github.com/docker-java/docker-java/releases -->
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-okhttp</artifactId>
<version>3.2.5</version>
</dependency>
FROM 192.168.220.128:5000/test:0.0.1
MAINTAINER uds
COPY test.jar test.jar
EXPOSE 11000
CMD ["java","-jar","test.jar"]
@Configuration
@Slf4j
public class DockerUtils {
@Value("${docker.url}")
public String dockerUrl;
private static DockerClient dockerClient;
* 下面这个是最完善的
@PostConstruct
public void getDockerClient() {
// 初始docker客户端基础配置
DefaultDockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost(dockerUrl) // docker 连接路径
// .withDockerTlsVerify(true) // 启用/禁用TLS验证(在http和https协议之间切换)
// .withDockerCertPath("**") // 如果使用https协议需要配置证书,这个地方就是证书的位置
.withRegistryUrl("192.168.220.128:5000") // 私服地址
// .withRegistryPassword("***") // 私服的密码
// .withRegistryUsername("***") // 私服账号
.build();
// 实例化docker http客户端
OkDockerHttpClient okDockerHttpClient = new OkDockerHttpClient.Builder()
.dockerHost(dockerClientConfig.getDockerHost()) // docker 连接路径
.connectTimeout(10000) // 连接超时时间
.readTimeout(10000) // 读取超时时间
.sslConfig(dockerClientConfig.getSSLConfig()) // ssl配置
.build();
this.dockerClient = DockerClientImpl.getInstance(dockerClientConfig, okDockerHttpClient);
// /**
// * 简易
// * @return
// */
// @PostConstruct
// public void getDockerClient() {
// DockerClient dockerClient = DockerClientBuilder.getInstance(dockerUrl).build();
// this.dockerClient = dockerClient;
* @param dockerFile
* @param name 这里name组成 例 test:0.0.1 名字加版本号
* 如果镜像需要push到私服,这个地方需要加上私服 格式
* 私服Ip:端口/name 192.168.220.128:5000/test:0.0.1
* @return
public static String buildImages(File dockerFile, String name) {
Set<String> tags = new HashSet<>();
tags.add(name);
// 回调
BuildImageResultCallback callback = new BuildImageResultCallback() {
@Override
public void onNext(BuildResponseItem item) {
log.info("item---------", item);
super.onNext(item);
return dockerClient.buildImageCmd(dockerFile).withTags(tags).exec(callback).awaitImageId();
public static void pushImages(String name) {
ResultCallback.Adapter<PushResponseItem> callBack = new ResultCallback.Adapter<PushResponseItem>() {
@Override
public void onComplete() {
super.onComplete();
dockerClient.pushImageCmd(name).exec(callBack);
public static void createService(String name) {
List<String> envList = new ArrayList<>();
envList.add("server.port=" + 11000); // 环境变量
Mount mount = new Mount()
.withTarget("/data/user") // 容器内部路径
.withSource("/data/data") // 容器外部路径
.withType(MountType.BIND);
List<Mount> mounts = new ArrayList<>();
mounts.add(mount);
TaskSpec taskSpec = new TaskSpec()
.withContainerSpec(new ContainerSpec()
.withImage(name) // 镜像名称
.withEnv(envList)
.withMounts(mounts) // docker -v 操作 服务事 --mount 操作
.withResources(
new ResourceRequirements().withLimits(
new ResourceSpecs().withMemoryBytes(2 * 1024 * 1024 * 1024L))); // 内存大小
PortConfig portConfig = new PortConfig()
.withTargetPort(11000)
.withProtocol(PortConfigProtocol.TCP)
.withPublishMode(PortConfig.PublishMode.ingress);
List<PortConfig> ports = new ArrayList<>();
ports.add(portConfig);
ServiceSpec serviceSpec = new ServiceSpec()
.withName("test") // 服务名称
.withTaskTemplate(taskSpec) // 服务配置
.withMode(new ServiceModeConfig()
.withReplicated(new ServiceReplicatedModeOptions().withReplicas(2)))
.withEndpointSpec(new EndpointSpec().withPorts(ports).withMode(EndpointResolutionMode.VIP));
String id = dockerClient.createServiceCmd(serviceSpec).exec().getId();
log.info("容器ID:" + id);
}
@Value("${file.path}")
private String filePath;
@Override
public void buildImages(MultipartFile multipartFile) throws IOException {
// 1. 将目标jar 与 Dockerfile 存放在同一个 目录地下方便 打镜像
String basePath = filePath + "/" + UUID.randomUUID();
File file = new File(basePath+"/"+multipartFile.getOriginalFilename());
if (!file.exists()) {
file.mkdirs();
multipartFile.transferTo(file);
// 复制Dockerfile 到filePath 下
InputStream io = this.getClass().getResourceAsStream("/Dockerfile");
File dockerFile = new File(basePath+"/"+"Dockerfile");
FileCopyUtils.copy(io, Files.newOutputStream(dockerFile.toPath()));
String id = DockerUtils.buildImages(dockerFile, "192.168.220.128:5000/tt:0.0.1");
log.info("返回的镜像Id:"+id);
DockerUtils.pushImages("192.168.220.128:5000/tt:0.0.1");
DockerUtils.createService("192.168.220.128:5000/tt:0.0.1");
知识点1:# 创建一个“handsome”的类
class handsome:
def __init__(self): # 构造方法(self参数必须是第一个参数,用于访问类中的属性和方法)
print("我是帅哥!")
Handsome = handsome() # 创建handsome类似实例# 运行结果
我是帅哥!知识点2: 继承和super().init()的用法1. 子类