idea中使用docker部署spring boot項目
開啓docker遠程訪問
修改docker配置文件:
vi /lib/systemd/system/docker.service
找到ExecStart
,將修改如下:
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
[root@bluesky dc-redis]# vi /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 fd:// -H tcp://0.0.0.0:2375 ## 改了這裏
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
...
重啓docker服務
[root@bluesky dc-redis]# systemctl daemon-reload
[root@bluesky dc-redis]# service docker restart
Redirecting to /bin/systemctl restart docker.service
校驗
curl http://localhost:2375/version
[root@bluesky dc-redis]# curl http://localhost:2375/version
{"Platform":{"Name":"Docker Engine - Community"}....
idea中佈署boot
現有spring boot項目,項目結構:
boot-docker
-> src
-> main
-> test
-> Dockerfile
-> pom.xml
注意Dockerfile的位置!!,和 src 同級
Dockerfile
內容:
FROM jdk8:latest
VOLUME /tmp
COPY target/boot-docker.jar app.jar # target/boot-docker.jar 的路徑是相對於
# Dockerfile的位置,所以Dockerfile位置很重要!!
RUN sh -c 'touch /app.jar'
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
直接執行Dockerfile方式
連接Docker server
idea底部可以看到Docker的服務面板:
如果不能看到Docker服務面板,可以通過keymap中, 設置快捷鍵顯示:
構建鏡像,點擊 Edit configuration:
直接運行,如果運行成功,可以在docker面板看到:
根據鏡像創建容器:
使用docker-image-plugin插件方式
在 pom.xml
中加入 plugin :
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<!--將插件綁定在某個phase執行-->
<executions>
<execution>
<id>build-image</id>
<!--將插件綁定在package這個phase上。也就是說,
用戶只需執行mvn package ,就會自動執行mvn docker:build-->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--指定生成的鏡像名,這裏是我們的項目名-->
<imageName>${project.artifactId}</imageName>
<!--指定標籤 這裏指定的是鏡像的版本,我們默認版本是latest-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<!-- 指定我們項目中Dockerfile文件的路徑-->
<dockerDirectory>${project.basedir}/</dockerDirectory>
<!--指定遠程docker 地址-->
<dockerHost>http://192.168.x.xx8:2375</dockerHost>
<!-- 這裏是複製 jar 包到 docker 容器指定目錄配置 -->
<resources>
<resource>
<targetPath>/</targetPath>
<!--jar包所在的路徑 此處配置的即對應項目中target目錄-->
<directory>${project.build.directory}</directory>
<!-- 需要包含的 jar包 ,這裏對應的是 Dockerfile中添加的文件名 -->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
然後,在右側點擊package,就可以構建鏡像了,其他的和上一種方式一樣