2

我正在尝试设置我的项目pom.xml和 Maven settings.xml,以自动化生成 Docker 映像并将其推送到我的AWS ECS私有 Docker 存储库的过程。

在我的pom.xml中,我添加了dockerfile-maven-plugin并将其配置如下:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.3.6</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>build</goal>
                <goal>push</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <finalName>myproject/server</finalName>
        <repository>137037344249.dkr.ecr.us-east-2.amazonaws.com/myproject/server</repository>
        <tag>${docker.image.tag}</tag>
        <serverId>ecs-docker</serverId>
        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
        <buildArgs>
            <VERSION>${project.version}</VERSION>
            <BUILD_NUMBER>${buildNumber}</BUILD_NUMBER>
            <WAR_FILE>${project.build.finalName}.war</WAR_FILE>
        </buildArgs>
    </configuration>
</plugin>

根据 dockerfile-maven-plugin 给出的说明,我需要为我的 ECS 服务器身份验证添加配置,但我不知道我需要提供什么用户名/密码。我怀疑这是我的 AWS 登录用户/通行证。

<servers>
    <server>
        <id>ecs-docker</id>
        <username>where_to_get_this</username>
        <password>where_to_get_this</password>
    </server>
</servers>

此外,欢迎以更好的方式自动生成此 Docker 映像/推送到我的仓库的任何建议。

4

3 回答 3

4

要使用 Spotify dockerfile-maven-plugin 构建 docker映像并将其推送到AWS ECR ,您应该:

  1. 安装amazon-ecr-credential-helper
go get -u github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cli/docker-credential-ecr-login 
  1. 将其移动到已经在执行路径中的某个文件夹:
mv ~/go/bin/docker-credential-ecr-login ~/bin/
  1. 为您的 Amazon ECR docker repo ID添加credHelpers部分到文件:~/.docker/config.json
{
  "credHelpers": {
    "<ecr-id>.dkr.ecr.<aws-region>.amazonaws.com": "ecr-login"
  },
  //...
}

"credsStore": "wincred",(在 Windows 上,如果存在,则从此文件中删除行)

  1. 检查~/.aws/config您所在的地区
[default]
region = <aws-region>

并且~/.aws/credentials有你的钥匙

[ecr-push-user]
aws_access_key_id = <id>
aws_secret_access_key = <secret>

更多信息...

  1. 将 Spotify 添加dockerfile-maven-plugin到您的 pom.xml:
    <properties>
        <docker.image.prefix>xxxxxxxxxxxx.dkr.ecr.rrrrrrr.amazonaws.com</docker.image.prefix>
        <docker.image.name>${project.artifactId}</docker.image.name>
        <docker.image.tag>${project.version}</docker.image.tag>
        <docker.file>Dockerfile</docker.file>
    </properties>

    <build>
      <finalName>service</finalName>

      <plugins>
          <!-- Docker image mastering -->
          <plugin>
              <groupId>com.spotify</groupId>
              <artifactId>dockerfile-maven-plugin</artifactId>
              <version>1.4.10</version>
              <configuration>
                  <repository>${docker.image.prefix}/${docker.image.name}</repository>
                  <tag>${docker.image.tag}</tag>
                  <dockerfile>${docker.file}</dockerfile>
              </configuration>
              <executions>
                  <execution>
                      <id>default</id>
                      <phase>package</phase>
                      <goals>
                          <goal>build</goal>
                          <goal>push</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      </plugins>
    </build>
  1. 确保 Dockerfile 存在,例如:
FROM openjdk:11-jre-slim
VOLUME /tmp
WORKDIR /service
COPY target/service.jar service.jar
ENTRYPOINT exec java -server \
-Djava.security.egd=file:/dev/./urandom \
$JAVA_OPTS \
-jar service.jar
  1. 使用一个命令构建和推送镜像:
mvn package
于 2019-02-09T09:44:41.397 回答
0

要登录 ECR,您必须使用 AWS 命令​​行生成 docker login 命令,然后使用它登录您的 docker 守护程序。我不认为这个用例是由任何 docker maven 插件处理的。

我在我的项目中所做的是在推送之前登录我的 docker 守护进程:

logstring=`aws --profile my-aws-profile ecr get-login --registry-ids my-registry-id`
`$logstring`

在我的情况下,此手动步骤是必需的,因为我们有一个 AWS 帐户,该帐户由生成一次性使用代码的硬件令牌保护,但这不是问题,因为我们每天只需要执行一次(ECR 登录持续12 小时),在我们部署到 ECR 的日子(而不是我们只在本地测试的那些日子)。

所以解决方案:

  • 手动登录到 ECR,以便您的 docker 推送工作,而无需从 maven 登录。
  • 添加一个登录步骤,直接在您的 pom 中编写外部登录脚本
  • 在您提交时尝试使用 AWS CodePipeline 直接构建您的代码,然后部署到 ECR(如果您不受其他限制,我建议您这样做)

玩得开心!

于 2018-04-05T13:35:30.773 回答
0

我没有在我的 Maven 设置文件中配置任何内容。我通常使用以下命令登录

$(aws ecr get-login --no-include-email --region my-region)

然后我运行 maven 命令(docker 命令作为 maven 目标的一部分嵌入),它工作正常。

供您参考,这是我使用 docker 插件设置的 pom 文件

    <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
                <dockerDirectory>docker</dockerDirectory>
               <!--  <serverId>docker-hub</serverId> -->
                <registryUrl>https://${docker.image.prefix}</registryUrl>
                 <forceTags>true</forceTags>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
            <executions>
                <execution>
                    <id>tag-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.image.prefix}/${project.artifactId}:${project.version}</imageName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
于 2018-08-15T23:53:35.770 回答