看了"idea一鍵部署springboot項目到docker"之類的文章,然後跟着做了.記錄一下遇到的問題:
環境:
centos7,內核5以上.之前學習es和logstash的時候已經裝了docker,這裏不再說明.(Linux運行於局域網下的一臺老筆記本)
各種必要端口已打開,包括後面項目需要的端口
idea是2020.3.3
maven是idea自帶的3.6.1
(docker和maven最好都切國內的源)
docker:這裏換的中科大的源
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
這裏一定要注意json文件的格式!(尤其是複製過來的,請檢查一下)
然後重啓使文件生效
systemctl daemon-reload
systemctl restart docker.service
maven:這裏換阿里雲的源,添加mirror,重啓idea
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央倉庫1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央倉庫2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
準備:
docker中先要有jdk的鏡像.可以先了解一下docker如何拉取鏡像.這個jdk是別人上傳的.他的dockerhub地址在下文有.
docker pull jinghan94/centos_jdk
docker image ls
再就是idea安裝docker插件.我的docker插件是安裝idea時默認勾選上的,已經有了.沒有遇到插件安裝不上的問題...
然後是創建springboot項目.我用的demo項目是另一臺電腦上創建並上傳到碼雲的.拉到本機時各種無法啓動,後來重新設置主文件路徑才得以啓動.這可能影響了後面docker鏡像的運行,先打住.
準備好後:
首先
vim /usr/lib/systemd/system/docker.service
在ExecStart一行後面加上"-H tcp://"等(設置成4個0就可以從別的主機訪問了,爲的是讓idea所在的電腦能訪問docker所在的主機)
[Service]
10 Type=notify
11 # the default is not to use systemd for cgroups because the delegate issues still
12 # exists and systemd currently does not support the cgroup feature set required
13 # for containers run by docker
14 #ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
15 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
16 ExecReload=/bin/kill -s HUP $MAINPID
17 TimeoutSec=0
18 RestartSec=2
19 Restart=always
然後使改動後的配置文件立即生效
systemctl daemon-reload
systemctl start docker
接下來設置idea中的docker
注意查看底部的連接狀態,URL不對或者端口沒有開放的話可能連接不上.
新安裝docker插件的話可能要重啓idea.安裝成功後能在底部看到services的選項卡.如果上面一步配置成功了,這裏就能看到docker中的images和containers
接着開始改造項目,在pom文件的同級目錄下創建"Dockerfile"文件.
#使用jdk8作爲基礎鏡像
FROM jinghan94/centos_jdk
#將複製指定的demo_for_gitee-0.0.1-SNAPSHOT.jar爲容器中的job.jar,相當於拷貝到容器中取了個別名
#ADD target/demo_for_gitee-0.0.1-SNAPSHOT.jar /demo_for_gitee.jar
#使用maven-cpmpile打出的jar包無法啓動,可能是我項目本身的老問題.剛拉下來的時候就各種無法啓動.下面嘗試從artifacts打jar包
ADD /out/artifacts/springboot2_jar/springboot2.jar /demo_for_gitee.jar
#創建一個新的容器並在新的容器中運行命令
RUN bash -c 'touch /demo_for_gitee.jar'
#設置時區
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#相當於在容器中用cmd命令執行jar包 指定外部配置文件
ENTRYPOINT ["java","-jar","/demo_for_gitee.jar"]
#ENTRYPOINT ["java","-jar","/demo_for_gitee.jar","--spring.config.location=/root/work/docker/demo_for_gitee/config/application.yml"]
jdk8是我從dockerhub中拉取的:https://hub.docker.com/r/jinghan94/centos_jdk
ADD一行,之前是從/target目錄下獲取jar包,但是啓動鏡像,運行到"java -jar demo_for_gitee.jar"時報找不到或無法加載主文件.
[root@localhost ~]# docker run demo_for_gitee/demo_for_gitee
錯誤: 找不到或無法加載主類 com.example.demo_for_gitee.DemoForGiteeApplication
然後百度到一個解決辦法:Idea 打包jar文件,可避免出現Error: Could not find or load main class TestDemo.jar_開發工具_xuedingkai的博客-CSDN博客
原因可能是我的項目路徑有問題,但更可能是我在pom文件中關於編譯插件做的改動引起的.在此貼出我的pom文件
<build>
<plugins>
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- 插件也可以繼承到子模塊 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- <mainClass>com.leishu.Application</mainClass> <!– 此處爲主入口–>-->
<!--這個mainClass是最開始爲了解決報錯引入的,後來換成從artifacts打jar包了-->
<mainClass>com.example.demo_for_gitee.DemoForGiteeApplication</mainClass> <!-- 此處爲主入口-->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!--使用docker-maven-plugin插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<!--將插件綁定在某個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>demo_for_gitee/${project.artifactId}</imageName>
<!--指定標籤,也就是版本號,可以自定義-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<!-- 指定 Dockerfile 路徑-->
<dockerDirectory>${project.basedir}</dockerDirectory>
<!--指定遠程 docker api地址 也就是服務器ip+docker的端口號-->
<dockerHost>http://192.168.1.103: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>
</plugins>
</build>
ENTRYPOINT一行中,如果要制定啓動時的--spring.config.location文件,需要先將該文件放到容器中(這一步還沒有創建容器...)
項目中的配置文件部分就此結束.接下來開始運行.
原本參照別人的方式,只要打包就能創建鏡像,但我的項目不能這麼創建鏡像.
接下來的步驟是:
1.從project structure中打jar包
2.run dockerfile(或者下面那個build image for dockerfile.我沒注意這裏的啓動圖標是插件檢測到dockerfile以後就會有還是怎樣).
3.接下來在services裏,docker的images中應該會出現新的image.然後選中並右鍵選擇創建容器
4.在新的頁面中注意綁定主機和docker容器的端口,這樣才能訪問到項目.
5.啓動成功
-----------------------------------------------------------------------
進入docker容器:
docker exec -it 44fc0f0582d9 /bin/bash
(bash報錯的話換成sh試試)
主機和docker容器間的拷貝文件.比如將yml文件放到docker容器中
實例
將主機./RS-MapReduce目錄拷貝到容器30026605dcfe的/home/cloudera目錄下。
docker cp RS-MapReduce 30026605dcfe:/home/cloudera
將容器30026605dcfe的/home/cloudera/RS-MapReduce目錄拷貝到主機的/tmp目錄中。
docker cp 30026605dcfe:/home/cloudera/RS-MapReduce /tmp/