idea 使用docker 插件链接docker 容器发布springboot项目

idea 使用docker 插件链接docker 容器发布springboot项目

文章目录

1 docker 配置远程访问

1.1 修改配置文件

vim /lib/systemd/system/docker.service

1.2 修改配置

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock修改为

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

[root@localhost ~]# vim /lib/systemd/system/docker.service


[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
                                                                                                       

1.3 重启docker 服务

#systemctl daemo-reload
#systemctl restart docker

1.4 查看服务器端口监听状态

[root@localhost ~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1019/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1176/master         
tcp6       0      0 :::2375                 :::*                    LISTEN      1093/dockerd        
tcp6       0      0 :::9000                 :::*                    LISTEN      1417/docker-proxy   
tcp6       0      0 :::22                   :::*                    LISTEN      1019/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1176/master    

1.5 关闭防火墙

#systemctl  status firewalld
#systemctl  disable  firewalld
#systemctl  stop  firewalld
#systemctl  status firewalld

1.6 测试docker 远程连通性

[root@localhost ~]# curl http://127.0.0.1:2375/version
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.13","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"2020-09-16T17:02:21.000000000+00:00","Experimental":"false","GitCommit":"4484c46d9d","GoVersion":"go1.13.15","KernelVersion":"3.10.0-1127.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.4.1","Details":{"GitCommit":"c623d1b36f09f8ef6536a057bd658b3aa8632828"}},{"Name":"runc","Version":"1.0.0-rc92","Details":{"GitCommit":"ff819c7e9184c13b7c2607fe6c30ae19403a7aff"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.13","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"4484c46d9d","GoVersion":"go1.13.15","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-1127.el7.x86_64","BuildTime":"2020-09-16T17:02:21.000000000+00:00"}
[root@localhost ~]# 

1.7 远程测试

idea 使用docker 插件链接docker 容器发布springboot项目

1.8 编写Dockerfile

#vim Dockerfile

#使用基础镜像
FROM centos:7
#配置作者和邮箱
MAINTAINER mrt "shi860715@126.com"
#配置工作目录,如果没有则创建
WORKDIR /usr/local/docker
#一个复制命令,把jdk安装文件复制到镜像中,语法 ADD SRC DEST ,ADD命令具有自动解压功能
ADD jdk-8u311-linux-x64.tar.gz  /usr/local/docker
#配置环境变量,此处目录为tar.gz包解压后的名称,需提前解压知晓:
ENV JAVA_HOME=/usr/local/docker/jdk1.8.0_311
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH
#、设置启动命令
CMD ["java","-version"]
~                                                                                                                                
~  

1.9 下载对应的版本的jdk

JDK版本jdk-8u311-linux-x64.tar.gz

1.10 运行编制镜像文件

[root@localhost java8]# docker build -t sx/java8:v1 .
Sending build context to Docker daemon  659.7MB
Step 1/8 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/8 : MAINTAINER mrt "shi860715@126.com"
 ---> Running in 22074722b2a3
Removing intermediate container 22074722b2a3
 ---> c76e0a9d07f2
Step 3/8 : WORKDIR /usr/local/docker
 ---> Running in aabedd6c94a3
Removing intermediate container aabedd6c94a3
 ---> 48f2374051b3
Step 4/8 : ADD jdk-8u311-linux-x64.tar.gz  /usr/local/docker
 ---> 8f0b40f6bd67
Step 5/8 : ENV JAVA_HOME=/usr/local/docker/jdk1.8.0_311
 ---> Running in 87b45d85ec91
Removing intermediate container 87b45d85ec91
 ---> ba097d3f5b37
Step 6/8 : ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in bd6af92de0cb
Removing intermediate container bd6af92de0cb
 ---> 237edc5566fc
Step 7/8 : ENV PATH=$JAVA_HOME/bin:$PATH
 ---> Running in 62cb60b2291a
Removing intermediate container 62cb60b2291a
 ---> f3600d7f32ac
Step 8/8 : CMD ["java","-version"]
 ---> Running in 2dc787eb0844
Removing intermediate container 2dc787eb0844
 ---> 0471c2285a42
Successfully built 0471c2285a42
Successfully tagged sx/java8:v1

1.11 查看编译后的镜像文件

[root@localhost java8]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
sx/java8              v1                  0471c2285a42        33 seconds ago      569MB
centos                7                   eeb6ee3f44bd        2 months ago        204MB
nginx                 latest              bc9a0695f571        12 months ago       133MB
portainer/portainer   latest              62771b0b9b09        16 months ago       79.1MB

1.12 创建运行容器

[root@localhost java8]# docker run -di --name=liubj_jdk8 sx/java8:v1
da2a91b2433b398ef671b20f17f4e60646ddb8659e23debc4988a42967484cca

1.13 验证容器中有java8环境

[root@localhost java8]# docker logs liubj_jdk8
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
[root@localhost java8]# 

1.14 配置docker 插件

 <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
<!--                    docker 远程服务器地址-->
                    <dockerHost>http://192.168.85.180:2375</dockerHost>
<!--                    镜像名称,前缀/项目名-->
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!--                    dockerfile的位置-->
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

1.15 编写dockerfile

FROM sx/java8:v1
VOLUME /tmp
ADD docker-0.0.1-SNAPSHOT.jar /test.jar
CMD ["java","-jar","/test.jar"]

1.16 打包自己的jar包

可能加载时候不怎么友好,建议报红后关闭重新打开,或者直接运行

 <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
<!--                <version>2.1.5.RELEASE</version>-->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>

1.17 编译dockerfile

"C:\Program Files\Java\jdk1.8.0_271\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\idea_work\docker -Dmaven.home=F:\java_win\apache-maven-3.6.3 -Dclassworlds.conf=F:\java_win\apache-maven-3.6.3\bin\m2.conf "-Dmaven.ext.class.path=E:\tools\idea\IntelliJ IDEA 2020.2.2\plugins\maven\lib\maven-event-listener.jar" "-javaagent:E:\tools\idea\IntelliJ IDEA 2020.2.2\lib\idea_rt.jar=51825:E:\tools\idea\IntelliJ IDEA 2020.2.2\bin" -Dfile.encoding=UTF-8 -classpath F:\java_win\apache-maven-3.6.3\boot\plexus-classworlds-2.6.0.jar;F:\java_win\apache-maven-3.6.3\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2020.2.2 -s E:\libs\repo\settings.xml com.spotify:docker-maven-plugin:1.0.0:build
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.liu.sx:docker >--------------------------
[INFO] Building docker 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ docker ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying D:\idea_work\docker\target\docker-0.0.1-SNAPSHOT.jar -> D:\idea_work\docker\target\docker\docker-0.0.1-SNAPSHOT.jar
[INFO] Copying src\main\docker\Dockerfile -> D:\idea_work\docker\target\docker\Dockerfile
[INFO] Building image sx/docker
Step 1/4 : FROM sx/java8:v1

 ---> 0471c2285a42
Step 2/4 : VOLUME /tmp

 ---> Using cache
 ---> 7c01d9f87e5b
Step 3/4 : ADD docker-0.0.1-SNAPSHOT.jar /test.jar

 ---> Using cache
 ---> 068850e2e14e
Step 4/4 : CMD ["java","-jar","/test.jar"]

 ---> Using cache
 ---> 15c88cd2acfc
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 15c88cd2acfc
Successfully tagged sx/docker:latest
[INFO] Built sx/docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.423 s
[INFO] Finished at: 2021-12-03T09:25:15+08:00
[INFO] ------------------------------------------------------------------------

上一篇:「日常记录」Java8新特性 - Stream API


下一篇:java8:Optional.ofNullable的使用