看了"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/