

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 打包游戏服务器生成包用于部署
<a name="gamelift-build-intro"></a>

准备好游戏服务器软件，以便在托管资源上部署。部署完成后，该软件将安装到每项托管资源中，随后启动一个或多个游戏服务器进程，准备为玩家提供游戏会话托管服务。

游戏服务器生成包的部署准备流程取决于您使用的 Amazon GameLift Servers 托管选项的类型。所有游戏服务器生成包均需与适用于 Amazon GameLift Servers 的服务器软件开发工具包集成，如[将游戏服务器与 Amazon GameLift Servers 集成](gamelift-sdk-server.md)中所述。

本节中的主题将指导您如何让软件做好在以下场景中部署的准备。
+ 对于 EC2 托管主机，请打包您的服务器软件并将其上传到以Amazon GameLift Servers进行部署。
+ 对于托管式容器托管，使用您的服务器软件构建容器映像，并将其存储在 Amazon Elastic Container Registry 中以完成部署。
+ 对于使用 Amazon GameLift Servers Anywhere 进行托管，按需打包服务器软件，适配自有托管资源的安装要求。

**注意**  
如果您要部署 Amazon GameLift Servers Realtime 配置的脚本，请参阅[上传适用于 Amazon GameLift Servers Realtime 的脚本](https://docs.aws.amazon.com/gameliftservers/latest/realtimeguide/realtime-script-uploading.html)。

**Topics**
+ [为 Amazon GameLift Servers 创建游戏服务器生成包](gamelift-build-cli-uploading.md)
+ [为 Amazon GameLift Servers 构建容器映像](containers-prepare-images.md)

# 为 Amazon GameLift Servers 创建游戏服务器生成包
<a name="gamelift-build-cli-uploading"></a>

将游戏服务器与 Amazon GameLift Servers 集成后（请参阅[准备使用 Amazon GameLift Servers 托管游戏](integration-intro.md)），请将游戏服务器软件安装到您的计算资源进行托管。此过程因您使用的 Amazon GameLift Servers 托管类型而异。

## 针对托管式托管进行部署
<a name="gamelift-build-custom-upload"></a>

如果您使用的是 Amazon GameLift Servers 托管式 EC2 托管，则必须打包游戏服务器软件并将其上传到 Amazon GameLift Servers。当您创建托管式实例集时，Amazon GameLift Servers 会自动将其部署到每个实例集实例。

本节中的主题介绍如何打包编译文件以供上传，如何创建可选的构建安装脚本，然后使用 [AWS Command Line Interface (AWS CLI)](https://aws.amazon.com/cli/) 或 AWS SDK 上传文件。

## 针对 Anywhere 托管进行部署
<a name="gamelift-build-custom-anywhere"></a>

如果您使用 Amazon GameLift Servers 实例集进行自管理式托管，则需自行负责将游戏服务器软件安装到实例集中的每台计算设备并保持更新。

当集成后的游戏服务器进程开始运行时，它会自动初始化并与 Amazon GameLift Servers 服务建立通信。服务器进程将根据 Amazon GameLift Servers 的提示启动游戏会话，并将活动报告给该服务。

**Topics**
+ [针对托管式托管进行部署](#gamelift-build-custom-upload)
+ [针对 Anywhere 托管进行部署](#gamelift-build-custom-anywhere)
+ [打包游戏生成包文件](gamelift-build-packaging.md)
+ [添加构建安装脚本](gamelift-build-cli-uploading-install.md)
+ [为托管主机创建Amazon GameLift Servers构建资源](gamelift-build-cli-uploading-builds.md)

# 打包游戏生成包文件
<a name="gamelift-build-packaging"></a>

在将您配置的游戏服务器上传到 Amazon GameLift Servers 之前，请将游戏生成包文件打包到生成包目录中。此过程对于使用 EC2 托管式实例集进行托管是必需的，也是使用 Anywhere 实例集进行托管时的最佳实践。生成包目录应包含运行游戏服务器和托管游戏会话所需的所有组件。这可能包括：
+ **游戏服务器二进制文件** – 运行游戏服务器所需的二进制文件。构建可以包括多个为相同平台构建的游戏服务器的二进制文件。有关受支持平台的列表，请参阅[获取 Amazon GameLift Servers 开发工具](gamelift-supported.md)。
+ **依赖项** – 运行游戏服务器可执行文件所需的任何相关文件。示例包括资产、配置文件和相关库。
**注意**  
对于使用适用于 C\$1\$1 的 Amazon GameLift Servers 服务器 SDK 创建的游戏生成包（包括使用 Unreal 插件创建的生成包），需包含与构建服务器 SDK 时所用 OpenSSL 版本一致的 OpenSSL DLL 文件。有关更多详细信息，请参阅服务器软件开发工具包自述文件。
+ **安装脚本**（可选）– 用于处理在 Amazon GameLift Servers 托管服务器上安装游戏生成包的任务的脚本文件。将此文件放置到生成包目录的根目录中。Amazon GameLift Servers 会在创建实例集的过程中运行该安装脚本。

您可以设置版本中的任何应用程序（包括安装脚本），以便在其他 AWS 服务上安全地访问您的资源。有关如何执行此操作的信息，请参阅[将您的Amazon GameLift Servers托管游戏服务器连接到其他 AWS 资源](gamelift-sdk-server-resources.md)。

在您打包生成包文件后，请确保您的游戏服务器可以在目标操作系统的干净安装上运行，以验证是否包含所有必需的依赖项以及安装脚本是否准确。

# 添加构建安装脚本
<a name="gamelift-build-cli-uploading-install"></a>

创建适用于您的游戏构建操作系统（OS）的安装脚本：
+ Windows：创建名为 `install.bat` 的批处理文件。
+ Linux：创建名为 `install.sh` 的 Shell 脚本文件。

在创建安装脚本时，请注意以下事项：
+ 该脚本不接受任何用户输入。
+ Amazon GameLift Servers 在托管服务器上的以下位置安装生成包并重新创建生成包中的文件目录：
  + Windows 实例集：`C:\game`
  + Linux 实例集：`/local/game`
+ 在 Linux 实例集安装过程中，run-as 用户具有对实例文件结构的有限访问权限。此用户对安装您的构建文件的目录拥有全部权限。如果您的安装脚本执行的操作需要管理员权限，请使用 **sudo** 指定管理员访问权限。默认情况下，Windows 实例集的 run-as 用户具有管理员权限。与安装脚本相关的权限失败会生成一条事件消息，此消息指示脚本出现问题。
+ 在 Linux 上，Amazon GameLift Servers 支持常用的 Shell 解释器语言，例如 bash。在安装脚本的顶部添加 shebang (例如 `#!/bin/bash`)。要验证对您的首选 Shell 命令的支持，可以远程访问活动的 Linux 实例并打开 Shell 提示符。有关更多信息，请参阅[Connect 连接到舰队实例](fleets-remote-access.md)。
+ 安装脚本不能依赖于 VPC 对等连接。直到 Amazon GameLift Servers 在实例集实例上安装生成包后，VPC 对等连接才可用。

**Example Windows 安装 bash 文件**  
此示例 `install.bat` 文件安装游戏服务器所需的 Visual C\$1\$1 运行时组件，然后将结果写入日志文件。脚本包含根目录下构建包中的组件文件。  

```
vcredist_x64.exe /install /quiet /norestart /log c:\game\vcredist_2013_x64.log
```

**Example Linux 安装 Shell 脚本**  
此示例 `install.sh` 文件在安装脚本中使用 bash 并将结果写入日志文件。  

```
#!/bin/bash
echo 'Hello World' > install.log
```
此示例`install.sh`文件展示了如何使用 Amazon CloudWatch 代理收集系统级和自定义指标以及处理日志轮换。由于 Amazon GameLift Servers 在服务 VPC 中运行，因此您必须向 Amazon GameLift Servers 授予代表您担任 AWS Identity and Access Management （IAM）角色的权限。Amazon GameLift Servers要允许代入角色，请创建一个包含 AWS 托管策略的角色`CloudWatchAgentAdminPolicy`，并在创建队列时使用该角色。  

```
sudo yum install -y amazon-cloudwatch-agent
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install -y collectd
cat <<'EOF' > /tmp/config.json
{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "root",
        "credentials": {
            "role_arn": "arn:aws:iam::account#:role/rolename"
        }
    },
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "/tmp/log",
                        "log_group_name": "gllog",
                        "log_stream_name": "{instance_id}"
                    }
                ]
            }
        }
    },
    "metrics": {
       "namespace": "GL_Metric",
        "append_dimensions": {
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            // Configure metrics you want to collect.
            // For more information, see [Manually create or edit the CloudWatch agent configuration file](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html).
        }
    }
}
EOF
sudo mv /tmp/config.json /opt/aws/amazon-cloudwatch-agent/bin/config.json
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
sudo systemctl enable amazon-cloudwatch-agent.service
```

# 为托管主机创建Amazon GameLift Servers构建资源
<a name="gamelift-build-cli-uploading-builds"></a>

创建构建和上传文件时，有几个选项可供选择：
+ [从文件目录创建构建](gamelift-build-cli-uploading-upload-build.md)。这是最简单且最常用的方法。
+ [使用 Amazon Simple Storage Service（Amazon S3）中的文件创建构建](gamelift-build-cli-uploading-create-build.md)。使用此选项，您可以在 Amazon S3 中管理您的构建版本。

通过这两种方法，Amazon GameLift Servers 都会使用唯一的生成包 ID 和其他元数据创建新的生成包资源。生成以**已初始化**状态开始。Amazon GameLift Servers 获取游戏服务器文件后，生成包将变为**就绪**状态。

生成包准备就绪后，您可以将其部署到新的 Amazon GameLift Servers 实例集中。有关更多信息，请参阅[创建Amazon GameLift Servers托管 EC2 舰队](fleets-creating.md)。当 Amazon GameLift Servers 设置新实例集时，它会将生成包文件下载到每个实例集实例，并安装生成包文件。

# 从文件目录创建构建
<a name="gamelift-build-cli-uploading-upload-build"></a>

要创建存储在任何位置（包括本地目录）中的游戏版本，请使用[https://docs.aws.amazon.com/cli/latest/reference/gamelift/upload-build.html](https://docs.aws.amazon.com/cli/latest/reference/gamelift/upload-build.html) AWS CLI 命令。此命令在 Amazon GameLift Servers 中创建一个新的生成包记录并从您指定的位置上传文件。

**发送上传请求。**在命令行窗口中，键入以下 **upload-build** 命令和参数。

```
aws gamelift upload-build \
    --name user-defined name of build \
    --operating-system supported OS \
    --server-sdk-version server SDK for Amazon GameLift Servers version \
    --build-root build path \
    --build-version user-defined build number \
    --region region name
```
+ **operating-system** – 游戏服务器生成包的运行时环境。您必须指定操作系统值。您稍后无法更新。
+ **server-sdk-version** – 您的游戏服务器与之集成的 Amazon GameLift Servers 服务器 SDK 版本。如果您没有提供值，则 Amazon GameLift Servers 会使用默认值 `4.0.2`。如果您指定的服务器 SDK 版本不正确，则在调用 `InitSdk` 建立与 Amazon GameLift Servers 服务的连接时，游戏服务器生成包可能会失败。
+ **build-root** – 您的构建文件的目录路径。
+ **name** – 新生成包的描述性名称。
+ **build-version** – 生成包文件的版本详细信息。
+ **region**— 您要在其中创建版本的 AWS 区域。在要部署实例集的区域中创建构建。如果您在多个区域中部署游戏，则需在每个区域中创建一个构建。
**注意**  
使用 [https://docs.aws.amazon.com/cli/latest/reference/configure/get.html](https://docs.aws.amazon.com/cli/latest/reference/configure/get.html) 查看您当前的默认区域。要更改默认区域，请使用 [https://docs.aws.amazon.com/cli/latest/reference/configure/set.html](https://docs.aws.amazon.com/cli/latest/reference/configure/set.html) 命令。

*示例*

```
aws gamelift upload-build \
    --operating-system AMAZON_LINUX_2023 \
    --server-sdk-version "5.0.0" \
    --build-root "~/mygame" \
    --name "My Game Nightly Build" \
    --build-version "build 255" \
    --region us-west-2
```

```
aws gamelift upload-build \
    --operating-system WINDOWS_2022 \
    --server-sdk-version "5.0.0" \
    --build-root "C:\mygame" \
    --name "My Game Nightly Build" \
    --build-version "build 255" \
    --region us-west-2
```

为了回应您的上传请求，Amazon GameLift Servers 会提供上传进度。成功上传后，Amazon GameLift Servers 会返回新的生成包记录 ID。上传时间取决于游戏文件的大小和连接速度。

# 使用 Amazon S3 中的文件创建构建
<a name="gamelift-build-cli-uploading-create-build"></a>

您可以将生成包文件存储在 Amazon S3 中，然后从那里将其上传到 Amazon GameLift Servers。创建版本时，您可以指定 S3 存储桶的位置，然后直接从 Amazon S3 中Amazon GameLift Servers检索构建文件。

**创建构建资源**

1. **将构建文件存储在 Amazon S3 中。**创建包含打包构建文件的.zip 文件，然后将其上传到您 AWS 账户的 S3 存储桶。记下存储桶标签和文件名，创建Amazon GameLift Servers版本时需要这些标签。

1. **向 Amazon GameLift Servers 提供对您的生成包文件的访问权限。**按照[在 Amazon S3 中访问游戏构建文件](security_iam_id-based-policy-examples.md#security_iam_id-based-policy-examples-access-storage-loc)中的说明创建 IAM 角色。创建角色后，记录新角色的 Amazon 资源名称（ARN），您在创建构建时需要该名称。

1. **创建构建** 使用Amazon GameLift Servers控制台或创建新的生成记录。 AWS CLI 您必须拥有 `PassRole` 权限，如[Amazon GameLift Servers 的 IAM 权限示例](gamelift-iam-policy-examples.md)中所述。

------
#### [ Console ]

1. 在 [Amazon GameLift Servers 控制台](https://console.aws.amazon.com/gamelift/)的导航窗格中，选择**托管**、**生成包**。

1. 在**构建**页面上，选择**创建构建**。

1. 在**创建构建**页面的**构建设置**下，执行以下操作：

   1. 对于**名称**，输入脚本名称。

   1. 对于**版本**，输入版本。由于构建内容可以更新，版本数据则有助于跟踪更新。

   1. 对于**操作系统（OS）**，选择您的游戏服务器构建的操作系统。您稍后无法更新此值。

   1. 对于**游戏服务器构建**，输入您上传到 Amazon S3 的构建目标的 **S3 URI**，然后选择**目标版本**。如果您忘记了 Amazon S3 URI 和对象版本，选择**浏览 S3**，然后搜索构建对象。

   1. 对于 **IAM 角色**，选择您创建的允许 Amazon GameLift Servers 访问您的 S3 存储桶和生成包对象的角色。

1. （可选）在**标签**下，通过输入**键**和**值**对向构建添加标签。

1. 选择**创建**。

Amazon GameLift Servers 为新生成包分配一个 ID 并上传指定的 .zip 文件。您可以在**构建**页面上查看新构建，包括状态。

------
#### [ AWS CLI ]

使用 [https://docs.aws.amazon.com/cli/latest/reference/gamelift/create-build.html](https://docs.aws.amazon.com/cli/latest/reference/gamelift/create-build.html) 命令定义新构建并上传您的服务器构建文件。

1. 打开命令行窗口，然后切换到您可以使用 AWS CLI的目录。

1. 输入以下 **create-build** 命令：

   ```
   aws gamelift create-build \
       --name user-defined name of build \
       --server-sdk-version server SDK for Amazon GameLift Servers version \
       --operating-system supported OS \
       --build-version user-defined build number \
       --storage-location "Bucket"=S3 bucket label,"Key"=Build .zip file name,"RoleArn"=Access role ARN} \
       --region region name
   ```
   + **name** – 新构建的描述性名称。
   + **server-sdk-version** – 您用于集成游戏服务器和 Amazon GameLift Servers 的 Amazon GameLift Servers 服务器 SDK 版本。如果您没有提供值，则 Amazon GameLift Servers 会使用默认值 `4.0.2`。
   + **operating-system** – 游戏服务器生成包的运行时环境。您必须指定操作系统值。您稍后无法更新。
   + **build-version** – 构建文件的版本详细信息。这些信息可能很有用，因为游戏服务器的每个新版本都需要新的构建资源。
   + **storage-location**
     + **Bucket** – 包含您的构建的 S3 存储桶的名称。示例：“my\$1build\$1files”。
     + **Key** – 包含您的构建文件的 .zip 文件的名称。示例：“my\$1game\$1build\$17.0.1, 7.0.2”。
     + **RoleARN** – 分配给您创建的 IAM 角色的 ARN。例如，“arn: aws: iam:: 111122223333: role/”。GameLiftAccess有关策略示例，请参阅 [在 Amazon S3 中访问游戏构建文件](security_iam_id-based-policy-examples.md#security_iam_id-based-policy-examples-access-storage-loc)。
   + **region**— 在计划部署舰队的 AWS 区域中创建构建。如果您在多个区域中部署游戏，则需在每个区域中创建一个构建。
**注意**  
我们建议使用 [https://docs.aws.amazon.com/cli/latest/reference/configure/get.html](https://docs.aws.amazon.com/cli/latest/reference/configure/get.html) 命令[https://docs.aws.amazon.com/cli/latest/reference/configure/get.html](https://docs.aws.amazon.com/cli/latest/reference/configure/get.html)命令检查当前的默认区域。要更改默认区域，请使用 [https://docs.aws.amazon.com/cli/latest/reference/configure/set.html](https://docs.aws.amazon.com/cli/latest/reference/configure/set.html) 命令。

   *示例*

   ```
   aws gamelift create-build \
       --operating-system WINDOWS_2022 \
       --storage-location "Bucket"="my_game_build_files","Key"="mygame_build_101.zip","RoleArn"="arn:aws:iam::111122223333:role/gamelift" \
       --name "My Game Nightly Build" \
       --build-version "build 101" \
       --region us-west-2
   ```

1. 要查看新构建，请使用 [https://docs.aws.amazon.com/cli/latest/reference/gamelift/describe-build.html](https://docs.aws.amazon.com/cli/latest/reference/gamelift/describe-build.html) 命令。

------

# 为 Amazon GameLift Servers 构建容器映像
<a name="containers-prepare-images"></a>

本主题介绍如何创建包含游戏服务器软件的容器映像，以便与 Amazon GameLift Servers 配合使用。游戏服务器容器映像包括游戏服务器可执行文件及其运行所需的任何依赖项。游戏服务器容器映像与 Amazon GameLift Servers 托管式容器托管解决方案一起使用。有关构建完整解决方案的详细信息，请参阅：
+ [借助 Amazon GameLift Servers 托管容器进行托管的开发路线图](gamelift-roadmap-containers.md)
+ [容器在 Amazon GameLift Servers 中的工作原理](containers-howitworks.md)

完成以下任务，准备好将游戏服务器容器映像部署到 Amazon GameLift Servers 容器实例集中。在开始这些任务之前，请完成游戏服务器代码与 Amazon GameLift Servers 服务器 SDK 的集成。

**Topics**
+ [创建游戏服务器容器映像](#containers-prepare-images-build)
+ [将容器映像推送到 Amazon ECR](#containers-prepare-images-upload)

## 创建游戏服务器容器映像
<a name="containers-prepare-images-build"></a>

在基于 Linux 的平台上或使用已安装 Docker 的适用于 Linux 的 Windows 子系统（WSL），按以下说明操作。

**创建游戏服务器容器映像**

1. 使用游戏服务器软件准备工作目录。在本地计算机上，创建一个工作目录来整理游戏服务器容器的文件。将容器部署到用于托管的 Amazon GameLift Servers 资源时，您的容器映像使用此文件结构。例如：

   ```
   [~/]$ mkdir -p work/glc/gamebuild && cd work && find .
   .
   ./glc
   ./glc/gamebuild
   ```
**注意**  
如果您正在尝试此功能，但游戏服务器版本还没有正常运行，请尝试我们的示例游戏服务器 [SimpleServer](https://github.com/aws-solutions-library-samples/guidance-for-custom-game-backend-hosting-on-aws/tree/main/BackendFeatures/AmazonGameLiftIntegration/SimpleServer)，该服务器可在上使用 GitHub。

1. 使用提供的模板创建一个新的 Dockerfile。

1. 按照 Dockerfile 模板中的说明，对其进行更新以供自己使用。
   + 根据需要更新基础映像。
   + 为游戏服务器生成包设置环境变量。

1. 构建容器映像。运行 `docker build`，指定您自己的存储库名称。例如：

   ```
   [~/work/glc]$ docker build -t <local repository name>:<optional tag> .
   ```

   您可以使用`docker images`命令查看存储库和镜像 IDs ，如以下示例所示：

## 游戏服务器容器映像的 Dockerfile 模板
<a name="w2aab9c11c13c17c15b1"></a>

此模板包含游戏服务器容器要在 Amazon GameLift Servers 实例集中可用所需的最少指令。根据需要修改游戏服务器的内容。

```
# Base image
# ----------
  # Add the base image that you want to use,
  # Make sure to use an image with the same architecture as the
  # Instance type you are planning to use on your fleets.
FROM public.ecr.aws/amazonlinux/amazonlinux
  #
# Game build directory
# --------------------
  # Add your gamebuild directory to the env variable below.
  # The game build provided here needs to be integrated with server sdk for Amazon GameLift Servers.
ENV GAME_BUILD_DIRECTORY="<ADD_GAME_BUILD_DIRECTORY>" \
  #
# Game executable and launch parameters
# ---------------
  # Add the relative path to your executable in the 'GAME_EXECUTABLE' env variable below.
  # The game build provided over here needs to be integrated with server sdk for Amazon GameLift Servers.
  # This template assumes that the executable path is relative to the game build directory.
  # Add any launch parameters to pass into your executable in the 'LAUNCH_PARAMS' env variable below.
  # Add 'HOME_DIR' to identify where the game executable and logs exist.
GAME_EXECUTABLE="<ADD NAME OF EXECUTABLE WITHIN THE GAME DIRECTORY>" \
LAUNCH_PARAMS=<ADD LAUNCH PARAMETERS> \
HOME_DIR="/local/game" \


# Install dependencies as necessary
RUN yum install -y shadow-utils
    
RUN mkdir -p $HOME_DIR
COPY ./$GAME_BUILD_DIRECTORY/ $HOME_DIR
    
# Change directory to home
WORKDIR $HOME_DIR
    
# Set up for 'gamelift' user
RUN useradd -m gamelift && \
  echo "gamelift ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
  chown -R gamelift:gamelift $HOME_DIR

# Add permissions to game build
RUN chmod +x ./$GAME_EXECUTABLE
    
USER gamelift
    
# Check directory before starting the container
RUN ls -lhrt .
    
# Check path before starting the container
RUN echo $PATH
    
# Start the game build
ENTRYPOINT ["/bin/sh", "-c", "./$GAME_EXECUTABLE", "$LAUNCH_PARAMS"]
```

## 将容器映像推送到 Amazon ECR
<a name="containers-prepare-images-upload"></a>

创建要部署到 Amazon GameLift Servers 的容器映像后，将该映像存储在 Amazon ECR 的公共或私有存储库中。该存储库会分配有一个 URI 值，Amazon GameLift Servers 会使用此值来获取该映像的快照，以便部署到容器实例集中。

**注意**  
如果您还没有 Amazon ECR 私有存储库，请[创建一个](https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-create.html)。

**将容器映像推送到 Amazon ECR**

1. 获取 Amazon ECR 凭证。在将容器映像推送到 Amazon ECR 之前，需先以临时形式获取您的 AWS 凭证，并将其提供给 Docker。Docker 需要这些凭证才能登录。

   ```
   [~/work/glc]$ aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.us-west-2.amazonaws.com
   WARNING! Your password will be stored unencrypted in 
   /home/user-name/.docker/config.json.
   Configure a credential helper to remove this warning.
   See https://docs.docker.com/engine/reference/commandline/login/#credentials-store
   
   Login Succeeded
   ```

1. 复制您要使用的 [Amazon ECR 私有存储库](https://console.aws.amazon.com/ecr/private-registry/repositories)的 URI。

1. 向容器映像应用 Amazon ECR 标签。  
**Example**  

   ```
   [~/work/glc]$ docker tag <IMAGE ID from above> <Amazon ECR private repository URI>:<optional tag>
   ```

1. 将容器映像推送到 Amazon ECR  
**Example**  

   ```
   [~/work/glc]$ docker image push  <Amazon ECR private repository URI>
   ```