docker容器化部署技巧

2019-12-31  本文已影响0人  yutons

springcloud微服务项目通过maven打包并上传镜像到私有仓库

docker插件选择

1、dockerfile-maven-plugin为最新版本,建议选用,邀请Dockerfile应与pom.xml处于同一目录下

2、开发环境许配置DOCKER_HOST,配置方式如下:

2.1 预备条件

    一台可远程访问的Docker主机
    maven3以上

2.2 开启Docker远程访问端口

    ```
    # vim /lib/systemd/system/docker.service
    ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2377 -H unix:///var/run/docker.sock

    systemctl daemon-reload
    systemctl restart docker
    ```
2.3 在开发环境配置系统环境变量

    ```
    # 因docker环境默认为localhost:2375,如果本机未安装docker运行环境,可设置环境变量DOCKER_HOST=tcp://ip:port
    DOCKER_HOST = tcp://192.168.113.89:2375
    ```

为项目添加pom插件

1、编写pom.xml(dockerfile-maven-plugins)

```
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.0</version>
    <configuration>
        <repository>${docker.image.prefix}/${project.artifactId}</repository>
        <tag>${project.artifactId}</tag>
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>
```

```
# ${docker.image.prefix}变量需要在pom的properties内定义

<properties>
    <docker.image.prefix>192.168.113.89/yutons</docker.image.prefix>
</properties>
```

2、编写pom.xml(docker-maven-plugins)

```
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.1.1</version>
    <!-- 
    插件绑定在某个phase执行
    mvn install 时能够docker:build
    mvn deploy 时能够docker:push
     -->
     <!--执行 mvn package 时 自动 执行 mvn docker:build-->
    <executions>
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
        <execution>
            <id>push-image</id>
            <phase>deploy</phase>
            <goals>
                <goal>push</goal>
            </goals>
            <configuration>
                <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName>
            </configuration>
        </execution>   
    </executions>
    <configuration>
        <!--
        <imageName>regcenter1</imageName>
        <!-- 这里一点不同 这个地方的dockerhost必须是http 不能使tcp -->
        <dockerHost>http://192.168.1.238:2375</dockerHost>
        <baseImage>maven</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image --> 
        <resources>
            <resource>
            <targetPath>/</targetPath>
            <directory>${project.build.directory}</directory>
            <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
        -->


        <!-- Dockerfile文件位置 -->
        <!-- <dockerDirectory>${project.basedir}</dockerDirectory> -->
        <!-- 镜像仓库:版本 -->
        <imageName>registry.cn-shanghai.aliyuncs.com/ycmm/${project.artifactId}:${project.version}</imageName>
        <!-- 基础镜像 -->
        <baseImage>maven</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- 远程docker环境地址 -->
        <dockerHost>http://192.168.1.238:2375</dockerHost>
        <!-- 覆盖相同标签镜像 -->
        <forceTags>true</forceTags>
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags>
        <!-- 资源 -->
        <resources>
             <resource>                                             <!-- 指定资源文件 -->
                 <targetPath>/</targetPath>                         <!-- 指定要复制的目录路径,这里是当前目录 -->
                 <directory>${project.build.directory}</directory>  <!-- 指定要复制的根目录,这里是target目录 -->
                 <include>${project.build.finalName}.jar</include>  <!-- 指定需要拷贝的文件,这里指最后生成的jar包 -->
             </resource>
        </resources>
        <!-- 上传仓库认证 需要在Maven settiing.xml中配置 -->
        <serverId>docker-ali</serverId>
        <registryUrl>registry.cn-shanghai.aliyuncs.com</registryUrl>
    </configuration>
</plugin>
```

4、编写Dockerfile存在在pom.xml同级目录

```
# dockerfile 基础配置
FROM daocloud.io/library/java:8u40-b22
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} /app/app.jar
WORKDIR /app/
EXPOSE 8889
ENTRYPOINT ["java","-jar","./app.jar"]
```

5、打包发布远程docker镜像

```
mvn clean package dockerfile:build -DskipTests
```
上一篇下一篇

猜你喜欢

热点阅读