

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建置 的容器映像 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>
   ```