【问题标题】:How to setup AWS ECS + dockerfile-maven-plugin?如何设置 AWS ECS + dockerfile-maven-plugin?
【发布时间】:2018-05-16 20:57:44
【问题描述】:

我正在尝试设置我的项目的 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 给出的instructions,我需要为我的 ECS 服务器身份验证添加配置,但我不知道我需要提供什么用户名/密码。我怀疑这是我的 AWS 登录用户/密码。

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

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

【问题讨论】:

  • أهلا وسام .. رفع الصورة يتم ب ECR و ليس ECS و إن كان الأول هو جزء من الثاني
  • @AbdennourTOUMI 你是说&lt;repository&gt; 组件的ecr 部分应该改为ecs 吗?

标签: maven amazon-web-services docker amazon-ecs maven-docker-plugin


【解决方案1】:

要登录 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(如果您不受其他限制,我建议您这样做)

玩得开心!

【讨论】:

    【解决方案2】:

    我没有在我的 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>
    

    【讨论】:

      【解决方案3】:

      要使用 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. credHelpers 部分添加到您的 Amazon ECR docker 存储库 ID 的 ~/.docker/config.json 文件:
      {
        "credHelpers": {
          "<ecr-id>.dkr.ecr.<aws-region>.amazonaws.com": "ecr-login"
        },
        //...
      }
      

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

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

      ~/.aws/credentials 有你的钥匙

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

      (More info...)

      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
      

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 2015-09-01
        • 2021-12-22
        • 2015-01-18
        相关资源
        最近更新 更多