17

微服务之docker(二)

 4 years ago
source link: http://www.cnblogs.com/fujingtao5470/p/12286743.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一.SpringCloud/SpringBoot整合docker

使用docker的maven组建构建springboot应用( 官方文档:https://spring.io/guides/gs/spring-boot-docker/

1.springboot项目pom.xml中添加配置文件

<!--docker镜像前缀-->

<properties>

<docker.image.prefix>xdclass</docker.image.prefix>

</properties>

<!--打包docker镜像插件-->

<build>

<!--打包的项目名称-->

<finalName>docker-demo</finalName>

<plugins>

<plugin>

<groupId>com.spotify</groupId>

<artifactId>dockerfile-maven-plugin</artifactId>

<version>1.3.6</version>

<configuration>

<repository>${docker.image.prefix}/${project.artifactId}</repository>

<buildArgs>

<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>

</buildArgs>

</configuration>

</plugin>

</plugins>

</build>

配置讲解:

Spotify 的 docker-maven-plugin 插件是用maven插件方式构建docker镜像的。

${project.build.finalName} 为产出物名称,可以省略,缺省为${project.artifactId}-${project.version}

2.创建Dockerfile文件(Dockerfile.txt)

(1)创建Dockerfile,默认是根目录,(可以修改为src/main/docker/Dockerfile,如果修则需要指定路径,具体指定自行百度)

Dockerfile文件是由一系列命令和参数构成的脚本,这些命令应用于基础镜像, 最终创建一个新的镜像。

Dockerfile.txt文件的内容如下(key vlaue格式):

FROM openjdk:8-jdk-alpine

VOLUME /tmp

ARG JAR_FILE

COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java","-jar","/app.jar"]

EXPOSE 8080

配置讲解:( 参考文档:https://yeasy.gitbooks.io/docker_practice/image/dockerfile/

FROM <image>:<tag> 需要一个基础镜像,可以是公共的或者是私有的, 后续构建会基于此镜像,如果同一个Dockerfile中建立多个镜像时,可以使用多个FROM指令

VOLUME 配置一个具有持久化功能的目录,主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp。改步骤是可选的,如果涉及到文件系统的应用就很有必要了。/tmp目录用来持久化到 Docker 数据文件夹,因为 Spring Boot 使用的内嵌 Tomcat 容器默认使用/tmp作为工作目录

ARG 设置编译镜像时加入的参数, ENV 是设置容器的环境变量

COPY : 只支持将本地文件复制到容器 ,还有个ADD更强大但复杂点

ENTRYPOINT 容器启动时执行的命令

EXPOSE 8080 暴露镜像端口

3.通过命令构建本地镜像

(1)在idea工具terminal面板中输入命令构建镜像:mvn install dockerfile:build

(2)查看本地构建的镜像:docker images

(3)登录阿里云服务器

(4)控制台-容器镜像服务-管理-操作指南(阿里云镜像仓库:https://dev.aliyun.com/search.html)

==================以下命令建议以阿里云镜像仓库操作指定为准, 每个阿里云镜像仓库的命令不一样 ==========================

(5)登录阿里云镜像仓库:docker login --username=不忘初心5470 registry.cn-beijing.aliyuncs.com

(6)将本地镜像打标签:docker tag [ImageId] registry.cn-beijing.aliyuncs.com/springcloud5470/eureka:[镜像版本号]

(7)推送本地的镜像到阿里云镜像仓库:docker push registry.cn-beijing.aliyuncs.com/springcloud5470/eureka:[镜像版本号]

===================生产环境服务器获取阿里云镜像仓库的镜像并进行部署==============================================

(8)生产环境服务器登录阿里云镜像仓库:docker login --username=不忘初心5470 registry.cn-beijing.aliyuncs.com

(9)拉取镜像仓库的指定镜像到生产环境服务器 docker pull registry.cn-beijing.aliyuncs.com/springcloud5470/eureka:[镜像版本号]

(10)查看获取的镜像:docker images

(11)启动容器(启动服务):docker run -d --name xdclass_docker_demo1 -p 8099:8080  a1b9fc71720d

(12)查看启动日志 docker logs -f  containerid【容器ID】

(13)配置阿里云端口映射

(14)浏览器访问测试


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK