Maven打包docker:dockerfile-maven-plugin

养焱
2023-12-01

pom.xml

<!--为了打包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.13</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <repository>my-${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>

                </configuration>
            </plugin>

        </plugins>
    </build>

Dockerfile,放在pom.xml旁

FROM java:8
ARG JAR_FILE
ADD target/${JAR_FILE} app.jar

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

然后运行maven package即可。

但是通过maven打包都需要连接docker服务器。可以通过环境变量设置DOCKER_HOST来找到docker服务器。
如果不设置,默认是:localhost:2375,代表当前主机有docker环境。

为什么要设置环境变量?下面是源码
com.spotify.plugin.dockerfile.AbstractDockerMojo

@Nonnull
  private DockerClient openDockerClient() throws MojoExecutionException {
    final RegistryAuthSupplier authSupplier = createRegistryAuthSupplier();

    try {
      return DefaultDockerClient
          //创建DockerClient,先从环境里拿到数据
          .fromEnv()

          .readTimeoutMillis(readTimeoutMillis)
          .connectTimeoutMillis(connectTimeoutMillis)
          .registryAuthSupplier(authSupplier)
          .useProxy(useProxy)
          .build();
    } catch (DockerCertificateException e) {
      throw new MojoExecutionException("Could not load Docker certificates", e);
    }
  }

com.spotify.docker.client.DefaultDockerClient#fromEnv

public static Builder fromEnv() throws DockerCertificateException {
    // 从环境里拿到docker服务器地址
    final String endpoint = DockerHost.endpointFromEnv();

    final Path dockerCertPath = Paths.get(Iterables.find(
        Arrays.asList(DockerHost.certPathFromEnv(),
            DockerHost.configPathFromEnv(),
            DockerHost.defaultCertPath()),
        Predicates.notNull()));

    final Builder builder = new Builder();

com.spotify.docker.client.DockerHost#endpointFromEnv

static String endpointFromEnv() {
    //如果从环境变量里面拿不到,那就设置成default
    return firstNonNull(systemDelegate.getenv("DOCKER_HOST"), defaultDockerEndpoint());
  }
private static final String DEFAULT_UNIX_ENDPOINT = "unix:///var/run/docker.sock";
private static final String DEFAULT_WINDOWS_ENDPOINT = "npipe:./pipe/docker_engine";
private static final String DEFAULT_ADDRESS = "localhost";
private static final int DEFAULT_PORT = 2375;
//获取default
static String defaultDockerEndpoint() {
    final String osName = systemDelegate.getProperty("os.name");
    final String os = osName.toLowerCase(Locale.ENGLISH);
    if (os.equalsIgnoreCase("linux") || os.contains("mac")) {
      return DEFAULT_UNIX_ENDPOINT;
    } else if (System.getProperty("os.name").equalsIgnoreCase("Windows 10")) {
      //from Docker doc: Windows 10 64bit: Pro, Enterprise or Education
      return DEFAULT_WINDOWS_ENDPOINT;
    } else {
      return DEFAULT_ADDRESS + ":" + defaultPort();
    }
  }
 类似资料: