将Spring工程打成镜像的两种方法

将Spring工程打成镜像的两种方法

Skafflod

skaffold 是谷歌开源的用于促进在 kubernets 集群里持续集成/持续发布的命令行工具。

github仓库:https://github.com/GoogleContainerTools/skaffold

总的来说 skaffold 是需要 kubernets 环境的。同时其工具风格也是和 kubernets API 非常相似的。在 jenkins-X ,一个用于在 kubernets CI/CD 的工具中,其实现了将Spring工程打成镜像的过程。

首先需要一个 Dockerfile 的实现,附带有 .dockerignore

Dockerfile


FROM openjdk:8-jdk-slim
ENV PORT 8080
ENV CLASSPATH /opt/lib
EXPOSE 8080

# copy pom.xml and wildcards to avoid this command failing if there's no target/lib directory
COPY pom.xml target/lib* /opt/lib/
# NOTE we assume there's only 1 jar in the target dir
# but at least this means we don't have to guess the name
# we could do with a better way to know the name - or to always create an app.jar or something
COPY target/*SNAPSHOT.jar /opt/app.jar
COPY *.sh /opt/
#COPY target/config /opt/config
WORKDIR /opt
CMD $JAVA_COMMAND


其中将 target 生成的jar包放入容器当中。

一个 kubernets API 风格的 yaml 文件 skaffold.yaml


apiVersion: skaffold/v1alpha2
kind: Config
build:
  tagPolicy:
    envTemplate:
      template: "/repo/image:"
  artifacts:
  - imageName: changeme
    workspace: .
    docker: {}
  local: {}
deploy:
  kubectl:
    manifests:

profiles:
- name: dev
  build:
    tagPolicy:
      envTemplate:
        template: "/repo/image:"
    artifacts:
    - docker: {}
    local: {}
  deploy:
    helm:
      releases:
      - name: demo
        chartPath: charts/demo
        setValueTemplates:
          image.repository: "/repo/image"
          image.tag: ""

通过 skaffold build -f skaffold.yaml build并push镜像到镜像仓库中。

这都是在 jenkins-X 容器中实现的。

jib

jib 同样是谷歌开源的用于将 java app 生成镜像的工具。jib 以 maven 和 Gradle 插件的形式来实现的。

github仓库:https://github.com/GoogleContainerTools/jib

jib总的来说封装程度较高,不过使用起来很方便,提供的接口对于简单项目足够使用,具体看官方文档就行了。

具体实现:

加到 maven plugins 中

            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>0.9.10</version>
                <configuration>
                    <from>
                         <image>openjdk:alpine</image>
                    </from>
                    <to>
                        <image>registry.hub.docker.com/repo/image</image>
                    </to>
                    <container>
                        <entrypoint>
                            <arg>java</arg>
                            <arg>-Xms256m</arg>
                            <arg>-Xmx256m</arg>
                            <arg>-Duser.timezone=GMT+08</arg>
                            <arg>-cp</arg>
                            <arg>/app/resources/:/app/classes/:/app/libs/*</arg>
                            <arg>com.example.App</arg>
                            <arg>--spring.profiles.active=dev</arg>
                        </entrypoint>
                    </container>
                    
                    <allowInsecureRegistries>true</allowInsecureRegistries>
                </configuration>
            </plugin>

我测试过可以使用的镜像

然后在 maven 构建过程中使用 mvn compile jib:build

使用私人仓库

如果使用私人仓库,需要将 docker 默认 https 改成 http

docker 守护进程

本机服务器和docker仓库服务器都要加上docker 配置 vim /etc/docker/daemon.json

{
  "registry-mirrors": ["http://hub-mirror.c.163.com"],
  "insecure-registries": ["本机IP:5001"]
}

然后重启 docker 守护进程 service docker restart

getsockopt

getsockopt: connection refused 的问题

Create a systemd drop-in directory for the docker service:
$ sudo mkdir -p /etc/systemd/system/docker.service.d
Create a file called /etc/systemd/system/docker.service.d/http-proxy.conf that adds the HTTP_PROXY environment variable:
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Or, if you are behind an HTTPS proxy server, create a file called /etc/systemd/system/docker.service.d/https-proxy.conf that adds the HTTPS_PROXY environment variable:
[Service]
Environment="HTTPS_PROXY=https://proxy.example.com:443/"
If you have internal Docker registries that you need to contact without proxying you can specify them via the NO_PROXY environment variable:
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
Or, if you are behind an HTTPS proxy server:
[Service]
Environment="HTTPS_PROXY=https://proxy.example.com:443/" "NO_PROXY=localhost,127.0.0.1,docker registry.somecorporation.com"
Flush changes:
$ sudo systemctl daemon-reload
Restart Docker:
$ sudo systemctl restart docker
Verify that the configuration has been loaded:
$ systemctl show --property=Environment docker
Environment=HTTP_PROXY=http://proxy.example.com:80/
Or, if you are behind an HTTPS proxy server:
$ systemctl show --property=Environment docker
Environment=HTTPS_PROXY=https://proxy.example.com:443/

来自 https://stackoverflow.com/questions/50130899/cant-docker-pull-connection-refused 的解决方式