为 Amazon GameLift Servers 构建容器映像 - Amazon GameLift Servers

为 Amazon GameLift Servers 构建容器映像

本主题介绍如何创建包含游戏服务器软件的容器映像,以便与 Amazon GameLift Servers 配合使用。游戏服务器容器映像包括游戏服务器可执行文件及其运行所需的任何依赖项。游戏服务器容器映像与 Amazon GameLift Servers 托管式容器托管解决方案一起使用。有关构建完整解决方案的详细信息,请参阅:

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

创建游戏服务器容器映像

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

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

    [~/]$ mkdir -p work/glc/gamebuild && cd work && find . . ./glc ./glc/gamebuild
    注意

    如果您正在试用此功能,但还没有正常运行的游戏服务器生成包,可尝试使用我们的示例游戏服务器 SimpleServer,该服务器已在 GitHub 上公开提供。

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

  3. 按照 Dockerfile 模板中的说明,对其进行更新以供自己使用。

    • 根据需要更新基础映像。

    • 为游戏服务器生成包设置环境变量。

  4. 构建容器映像。运行 docker build,指定您自己的存储库名称。例如:

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

    您可以使用 docker images 命令查看您的存储库和映像 ID,如以下示例所示:

此模板包含游戏服务器容器要在 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

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

注意

如果您还没有 Amazon ECR 私有存储库,请创建一个

将容器映像推送到 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
  2. 复制您要使用的 Amazon ECR 私有存储库的 URI。

  3. 向容器映像应用 Amazon ECR 标签。

    [~/work/glc]$ docker tag <IMAGE ID from above> <Amazon ECR private repository URI>:<optional tag>
  4. 将容器映像推送到 Amazon ECR

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