Skip to content

Commit

Permalink
docker第一节 maven插件构建docker镜像
Browse files Browse the repository at this point in the history
  • Loading branch information
eacdy committed Sep 6, 2016
1 parent 95d5948 commit 64c0ae1
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
* [第3节:Turbine](chapter5/5.3 Turbine.md)
* [第6章:配置中心](chapter6/6 配置中心.md)
* [第7章:API Gateway](chapter7/7 API Gateway.md)
* [第8章:使用Docker构建微服务](chapter8/8 使用Docker构建微服务.md)
* [第1节:使用Maven插件构建Docker镜像](chapter8/8.1 使用Maven插件构建Docker镜像.md)

14 changes: 14 additions & 0 deletions chapter8/8 使用Docker构建微服务.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 第8章:使用Docker构建微服务

Docker是一个新生事物。Docker是什么,能做什么,有什么优缺点,以及其如何入门不在本文讨论范围。关于Docker的入门教程网上有很多,笔者也打算近期将使用Docker一年多的经验进行总结,同样以Gitbook的形式开源出来。故而闲言碎语就不多说了,本文直接讲述如何使用Maven构建微服务Docker镜像,以及使用Docker构建微服务。



### 准备工作

| 安装软件 | 功能 | 必要程度 |
| --------- | -------------- | ---- |
| Docker | Docker ||
| Kitematic | Docker的GUI管理工具 | 建议安装 |


225 changes: 225 additions & 0 deletions chapter8/8.1 使用Maven插件构建Docker镜像.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# 第1节:使用Maven插件构建Docker镜像

### 工具

工欲善其事,必先利其器。笔者经过调研,有以下几款Docker的Maven插件进入笔者视野:

| 插件名称 | 官方地址 |
| ------------------- | ---------------------------------------- |
| docker-maven-plugin | https://github.com/spotify/docker-maven-plugin |
| docker-maven-plugin | https://github.com/fabric8io/docker-maven-plugin |
| docker-maven-plugin | https://github.com/bibryam/docker-maven-plugin |

笔者从Stars、文档易用性以及更新频率三个纬度考虑,选用了第一款。



### 使用插件构建Docker镜像

#### 简单使用

我们以之前的项目:microservice-discovery-eureka为例:

1. 在pom.xml中添加下面这段

```xml
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>microservice-discovery-eureka</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
```

2. 执行命令:

```shell
mvn clean package docker:build
```

3. 进入漫长的等待后(当然可以使用国内的Docker镜像加速,譬如DaoCloud),我们会发现控制台有类似如下内容:

```
[INFO] Building image microservice-discovery-eureka
Step 1 : FROM java
---> ea40c858f006
Step 2 : ADD /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar //
---> 7e192e0639b6
Removing intermediate container b76d530b8558
Step 3 : ENTRYPOINT java -jar /microservice-discovery-eureka-0.0.1-SNAPSHOT.jar
---> Running in 54f4639386a5
---> 502c73acf139
Removing intermediate container 54f4639386a5
Successfully built 502c73acf139
[INFO] Built microservice-discovery-eureka
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
```

恭喜,构建成功了。

5. 我们打开Kitematic,会发现其上已经刚刚构建好的Docker镜像了,如下图

![Kitematic](images/docker-1.png)

6. 点击Create按钮,我们会发现该Docker镜像会很快地启动。

7. 停止镜像,点击Settings,我们将8761端口映射出去

![设置镜像](images/docker-2.png)

8. 访问测试:

访问[http://localhost:8761](http://localhost:8761) ,能够正常看到Eureka界面。




#### 使用Dockerfile进行构建

上文讲述的方式是最简单的方式,很多时候,我们还是要借助Dockerfile进行构建的,首先我们在/microservice-discovery-eureka/src/main/docker目录下,建立文件Dockerfile

```
FROM java:8
VOLUME /tmp
ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 9000
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
```

修改pom.xml

```xml
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository name ... only [a-z0-9-_.] are allowed-->
<imageName>microservice-discovery-eureka-dockerfile</imageName>
<!-- 指定Dockerfile所在的路径 -->
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
```

其他步骤一样。这样即可使用Dockerfile进行构建Docker镜像啦。



#### 将Docker镜像push到DockerHub上

1. 首先修改Maven的全局配置文件settings.xml,添加以下段落

```xml
<servers>
<server>
<id>docker-hub</id>
<username>你的DockerHub用户名</username>
<password>你的DockerHub密码</password>
<configuration>
<email>你的DockerHub邮箱</email>
</configuration>
</server>
</servers>
```

2. 在DockerHub上创建repo,例如:test,如下图

![DockerHub](images/docker-3.png)

3. 项目pom.xml修改为如下:注意imageName的路径要和repo的路径一致

```xml
<build>
<plugins>
<!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<!-- 详见:https://github.com/spotify/docker-maven-plugin Invalid repository
name ... only [a-z0-9-_.] are allowed -->
<!-- 如果要将docker镜像push到DockerHub上去的话,这边的路径要和repo路径一致 -->
<imageName>eacdy/test</imageName>
<!-- 指定Dockerfile所在的路径 -->
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 以下两行是为了docker push到DockerHub使用的。 -->
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
</plugins>
</build>
```

4. 执行命令:

```shell
clean package docker:build -DpushImage
```

5. 搞定,等构建成功后,我们会发现Docker镜像已经被push到DockerHub上了。



#### TIPS

1. imageName必须符合正则[a-z0-9-_.],否则将会构建失败
2. 该插件还有其他的使用方法,譬如将构建Docker镜像绑定在某个Maven的phase上执行、譬如对Docker密码的加密等等。文档[https://github.com/spotify/docker-maven-plugin](https://github.com/spotify/docker-maven-plugin)上写得非常清楚,不做赘述了。




#### 代码地址(任选其一)

> [https://git.oschina.net/itmuch/spring-cloud-study/tree/master/docker/microservice-discovery-eureka](https://git.oschina.net/itmuch/spring-cloud-study/tree/master/docker/microservice-discovery-eureka)
>
> [https://github.com/eacdy/spring-cloud-study/tree/master/docker/microservice-discovery-eureka](https://github.com/eacdy/spring-cloud-study/tree/master/docker/microservice-discovery-eureka)


#### 参考文档

> [http://developer.51cto.com/art/201404/434879.htm](http://developer.51cto.com/art/201404/434879.htm)
Binary file added chapter8/images/docker-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapter8/images/docker-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapter8/images/docker-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 64c0ae1

Please sign in to comment.