由于未能处理“$ {project.artifactId}”而无法构建docker镜像:缺少':'替换

问题描述 投票:0回答:2

我正在尝试使用以下命令为maven项目构建一个docker镜像:mvn clean install -DskipTests -Pdocker

我有以下Dockerfile:

FROM openjdk:8-jre

ARG serviceuser=${project.artifactId}

##UPDATES AND INSTALL REQUIRED PACKAGES
RUN apt-get update && \
    apt-get install -y gettext-base sudo && \
    apt-get install -y iptables sudo && \
    adduser --shell /bin/bash ${serviceuser} && \
    adduser ${serviceuser} sudo && \
    echo "%sudo ALL=NOPASSWD: ALL" >> /etc/sudoers

USER ${serviceuser}

##MOVE AND COPY PROJECT FILES INTO CONTAINER
COPY ${project.build.finalName}.jar /home/${serviceuser}/

WORKDIR /home/${serviceuser}

pom.xml文件具有以下一些内容:(已将其缩短为仅用于解释)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.book.app</groupId>
  <artifactId>book-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>book-app</name>
  <url>http://maven.apache.org</url>
  <profiles>
    <profile>
      <id>docker</id>
      <build>
        <resources>
          <resource>
            <directory>docker</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}</targetPath>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.3</version>
            <executions>
              <execution>
                <id>default</id>
                <goals>
                  <goal>build</goal>
                  <goal>push</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <contextDirectory>${project.build.directory}</contextDirectory>
              <repository>test-img:2000/v1/${project.artifactId}</repository>
              <tag>${project.version}</tag>
              <tag>latest</tag>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
  </parent>
  <dependencies>
    ...
  </dependencies>
  <build>
    ...
  </build>
</project>

当我构建时,它给我以下错误消息...

引起:com.spotify.docker.client.exceptions.DockerException:无法处理“$ {project.artifactId}”:缺少':'代替

[ERROR]无法处理“$ {project.artifactId}”:缺少':'代替

[INFO]建立失败

非常感谢任何提示或帮助,谢谢。

maven docker docker-compose dockerfile
2个回答
0
投票

你必须改变你的pom文件中的<finalName>..</finalName>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.book.app</groupId>
  <artifactId>book-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>book-app</name>
  <url>http://maven.apache.org</url>
  <profiles>
    <profile>
      <id>docker</id>
      <build>
        <resources>
          <resource>
            <directory>docker</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}</targetPath>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.3</version>
            <executions>
              <execution>
                <id>default</id>
                <goals>
                  <goal>build</goal>
                  <goal>push</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <contextDirectory>${project.build.directory}</contextDirectory>
              <repository>test-img:2000/v1/${project.artifactId}</repository>
              <tag>${project.version}</tag>
              <tag>latest</tag>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
  </parent>
  <dependencies>
    ...
  </dependencies>
  <build>
    <finalName>${project.artifactId}</finalName>
    ...
  </build>
</project>

之后你可以在你的Dockerfile中使用:

FROM openjdk:8-jre

ARG serviceuser=${project.artifactId}

##UPDATES AND INSTALL REQUIRED PACKAGES
RUN apt-get update && \
    apt-get install -y gettext-base sudo && \
    apt-get install -y iptables sudo && \
    adduser --shell /bin/bash ${serviceuser} && \
    adduser ${serviceuser} sudo && \
    echo "%sudo ALL=NOPASSWD: ALL" >> /etc/sudoers

USER ${serviceuser}

##MOVE AND COPY PROJECT FILES INTO CONTAINER
COPY target/book-app.jar /home/${serviceuser}/

WORKDIR /home/${serviceuser}

我不确定为什么你使用serviceuser作为Dockerfile的ARG ..

您可以在pom.xml的文档中阅读:https://maven.apache.org/pom.html


0
投票

只要变量名中包含某些非字母数字字符,Docker就会抛出这种误导性错误。

我假设它试图将它们解释为语法来指定bash样式default value substitution

要防止此错误,请从变量名称中删除该字符。

在你的例子中是.导致错误,对我来说这是一个-我必须用_替换。

© www.soinside.com 2019 - 2024. All rights reserved.