Docker部署springBoot项目
1.、编写Dockerfile文件 内容如下
#依赖镜像名称和ID
FROM java:8
#指定镜像创建者信息
MAINTAINER Tony-y
# 将本地文件夹挂载到当前容器
VOLUME /tmp
# 拷贝文件到容器,wechat-cloud-web-0.0.1-SNAPSHOT.jar 这里是maven打包后的名字
ADD wechat-cloud-web-0.0.1-SNAPSHOT.jar web.jar
#为Docker容器设置对外的端口号。在启动时,可以使用-p选项或者-P选项。
EXPOSE 8085
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","web.jar"]
#设置时区 避免ORA-01882: timezone region not found
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
2、服务器新建一个docker文件夹,将maven打包好的springBoot 的 jar包和Dockerfile 文件 复制到服务器的docker文件夹下
maven 插件如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--指定mainClass 防止springboot镜像部署报:No main manifest attribute, in XXX.jar-->
<configuration>
<mainClass>com.dx.wechat.cloud.web.WechatCloudWebApplication</mainClass>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>wechat-web</imageName>
<baseImage>java</baseImage>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.war</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
Spotify 的 docker-maven-plugin 插件是用maven插件方式构建docker镜像的。
imageName指定了镜像的名字,本例为 forep/eureka-server
dockerDirectory指定 Dockerfile 的位置
resources是指那些需要和 Dockerfile 放在一起,在构建镜像时使用的文件,一般应用 jar 包需要纳入。
转自: https://blog.csdn.net/forezp/article/details/70198649
3、制作镜像
docker文件夹下,执行下面命令, 看好,最后面有个"."点!!!!, 然后 -t 参数 是指定此镜像的tag名
docker build -t 自定义的镜像名 .
4、查看镜像
完成后通过docker images命令查看我们制作的镜像
docker images
5、创建并启动容器
[root@localhost docker]# docker run -di --name=name -p 8085:8085 自定义的镜像名
-d参数是让容器后台运行
-p 是做端口映射,此时将服务器中的8085端口映射到容器中的8085(项目中端口配置的是8085)端口
2守护式方式创建容器:(守护式容器是一直运行的,退出容器都是exit,退出只是退出终端,它还是在后台运行)
docker run -di --name=自定义容器名称 镜像名称:标签id ( REPOSITORY)/bin/bash
docker run -di --name=mytomcat -p 8080:8080 tomcat:7-jre7