一、开启docker的远程连接
(1)修改服务器的docker配置
vim /lib/systemd/system/docker.service
修改前:ExecStart=/usr/bin/dockerd-current
修改后:ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
修改后的文件全文如下:
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock\
--add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
--default-runtime=docker-runc \
--exec-opt native.cgroupdriver=systemd \
--userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
--init-path=/usr/libexec/docker/docker-init-current \
--seccomp-profile=/etc/docker/seccomp.json \
$OPTIONS \
$DOCKER_STORAGE_OPTIONS \
$DOCKER_NETWORK_OPTIONS \
$ADD_REGISTRY \
$BLOCK_REGISTRY \
$INSECURE_REGISTRY \
$REGISTRIES
(2)修改成功后,重新加载配置并重启docker
systemctl daemon-reload && systemctl restart docker
(3)测试是否开启docker的远程连接:打开浏览器,访问http://docker所在服务器IP:2375/version,返回如下数据则配置成功
{
"Version": "1.13.1",
"ApiVersion": "1.26",
"MinAPIVersion": "1.12",
"GitCommit": "cccb291/1.13.1",
"GoVersion": "go1.10.3",
"Os": "linux",
"Arch": "amd64",
"KernelVersion": "3.10.0-1062.9.1.el7.x86_64",
"BuildTime": "2020-03-03T17:21:24.304031284+00:00",
"PkgVersion": "docker-1.13.1”
}
二、安装docker插件,并配置idea
三、编写Dockerfile文件
在resources目录下创建Dockerfile文件:
编写Dockerfile:
FROM openjdk:8u242-jre-buster
MAINTAINER test<test@foxmail.com>
WORKDIR /opt
ADD *-exec.jar helloworld.jar
COPY application*.yml config/
VOLUME ["/opt/config/","/tmp"]
ENTRYPOINT ["java","-jar","helloworld.jar"]
四、配置docker-maven-plugin插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.1.RELEASE</version>
<configuration>
<classifier>exec</classifier>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.2</version>
<!--绑定docker命令到maven phases-->
<executions>
<execution>
<id>build-image</id>
<!--将插件绑定在package这个phase上。也就是说,
用户只需执行mvn package ,就会自动执行mvn docker:build-->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--指定远程docker的地址-->
<dockerHost>http://111.231.242.174:2375</dockerHost>
<!--指定生成的镜像名,一般用项目名-->
<imageName>${project.artifactId}</imageName>
<!--指定标镜像的版本,默认版本是latest-->
<imageTags>
<imageTag>${project.version}</imageTag>
<!--<imageTag>latest</imageTag>-->
<!-- 强制docker在每次新的构建上覆盖镜像tags-->
<forceTags>true</forceTags>
</imageTags>
<!-- 指定项目对应的Dockerfile文件的路径-->
<dockerDirectory>${project.basedir}/src/main/resources</dockerDirectory>
<!--指定复制docker容器中可执行jar包的位置 -->
<resources>
<resource>
<!--指定build资源到哪个目录,默认是target/classes-->
<targetPath>/</targetPath>
<!--jar包所在的路径,此处配置的即对应项目中target目录-->
<directory>${project.build.directory}</directory>
<!-- 需要添加到dockerfile文件中的可执行jar包的名称 -->
<include>${project.build.finalName}-exec.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
五:构建镜像
六、创建容器
在idea下方点击Services,进入Services窗口