

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

# 为 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>
   ```