devcontainerのベースをAmazon Linuxにする

devcontainer便利ですよね。
私はいつもUbuntuのイメージを選択していますが、ふと、Amazon Linuxにできないか試したくなりました。

# 普通に作ったUbuntuのベースイメージ

ベースイメージは`ubuntu:20.04`ではなく、devcontaier用にカスタマイズされているようです。

```Dockerfile:.devcontainer/Dockerfile
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile

# [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
ARG VARIANT="jammy"
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>
```

```json-doc:.devcontainer/devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu
{
	"name": "Ubuntu",
	"build": {
		"dockerfile": "Dockerfile",
		// Update 'VARIANT' to pick an Ubuntu version: jammy / ubuntu-22.04, focal / ubuntu-20.04, bionic /ubuntu-18.04
		// Use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon.
		"args": { "VARIANT": "ubuntu-20.04" }
	},

	// Set *default* container specific settings.json values on container create.
	"settings": {},


	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [],

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],

	// Use 'postCreateCommand' to run commands after the container is created.
	// "postCreateCommand": "uname -a",

	// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode"
}
```

# 1. ベースイメージを変える

```diff:.devcontainer/Dockerfile
  # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile
    
  # [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
      ARG VARIANT="jammy"
- FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
+ # FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
+ FROM amazonlinux:2

  # [Optional] Uncomment this section to install additional OS packages.
  # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  #     && apt-get -y install --no-install-recommends <your-package-list-here>
```

**結果：** エラー
**原因：** /etc/passwdにvscodeユーザーがいない

```console
[2022-05-21T11:36:42.234Z] unable to find user vscode: no matching entries in passwd file

[2022-05-21T11:36:42.236Z] Start: Run in container: cat /etc/passwd
[2022-05-21T11:36:42.236Z] Stdin closed!
[2022-05-21T11:36:42.236Z] Error: An error occurred setting up the container.
```

# 2. vscodeユーザーを追加する

```diff:.devcontainer/Dockerfile
  # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile
    
  # [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
  ARG VARIANT="jammy"
  # FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
  FROM amazonlinux:2

  # [Optional] Uncomment this section to install additional OS packages.
  # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  #     && apt-get -y install --no-install-recommends <your-package-list-here>
+ RUN yum install -y shadow-utils
+ RUN adduser vscode
```

**結果：** エラー
**原因：** tarコマンドがない

```console
[2022-05-21T11:45:56.696Z] /bin/sh: line 2: tar: command not found
[2022-05-21T11:45:56.697Z] Exit code 127
```

# 3. tarをインストール

```diff:.devcontainer/Dockerfile
  # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile
    
  # [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
  ARG VARIANT="jammy"
  # FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
  FROM amazonlinux:2

  # [Optional] Uncomment this section to install additional OS packages.
  # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  #     && apt-get -y install --no-install-recommends <your-package-list-here>
- RUN yum install -y shadow-utils
+ RUN yum install -y shadow-utils tar
  RUN adduser vscode
```

**結果：** エラー
**原因：** gzipコマンドがない

```console
[2022-05-21T11:50:07.344Z] tar (grandchild): gzip: Cannot exec: No such file or directory
tar (grandchild): Error is not recoverable: exiting now
tar: Child died with signal 13
tar: Error is not recoverable: exiting now
```

# 4. gzipをインストール

```diff:.devcontainer/Dockerfile
  # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile
    
  # [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
  ARG VARIANT="jammy"
  # FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
  FROM amazonlinux:2

  # [Optional] Uncomment this section to install additional OS packages.
  # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  #     && apt-get -y install --no-install-recommends <your-package-list-here>
- RUN yum install -y shadow-utils tar
+ RUN yum install -y shadow-utils tar gzip
  RUN adduser vscode
```

**結果：** **成功**
起動しました！！！！😝😝😝

# 5. AmazonLinuxっぽくする

* devcontainerの名前をAmazonLinuxに変更(ウィンドウ左下に表示される名前）
* ユーザーをec2-userにする
* sudoコマンドをインストールし、ec2-userがパスワード無しでsudoできるようにする
* オートコンプリートを追加する

```diff:.devcontainer/Dockerfile
  # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile

  # [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
  ARG VARIANT="jammy"
  # FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
  FROM amazonlinux:2

  # [Optional] Uncomment this section to install additional OS packages.
  # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  #     && apt-get -y install --no-install-recommends <your-package-list-here>
- RUN yum install -y shadow-utils tar gzip
+ RUN yum install -y shadow-utils tar gzip sudo bash-completion
- RUN adduser vscode
+ RUN adduser ec2-user
+ RUN echo 'ec2-user ALL=(ALL:ALL) NOPASSWD: ALL' >> /etc/sudoers
```

```diff_jsondoc:.devcontainer/devcontainer.json
  // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
  // https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu
  {
      "name": "AmazunLinux2",
      "build": {
          "dockerfile": "Dockerfile",
          // Update 'VARIANT' to pick an Ubuntu version: jammy / ubuntu-22.04, focal / ubuntu-20.04, bionic /ubuntu-18.04
          // Use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon.
          "args": { "VARIANT": "ubuntu-20.04" }
      },
  
      // Set *default* container specific settings.json values on container create.
      "settings": {},
  
  
      // Add the IDs of extensions you want installed when the container is created.
      "extensions": [],
  
      // Use 'forwardPorts' to make a list of ports inside the container available locally.
      // "forwardPorts": [],
  
      // Use 'postCreateCommand' to run commands after the container is created.
      // "postCreateCommand": "uname -a",
  
      // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
-     "remoteUser": "ec2-user"
+     "remoteUser": "ec2-user"
  }
```

これでどこからどう見てもAmazon Linuxです😝😝😝
`amazon-linux-extras`もできますよ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/41574/a09400f9-0a5c-267e-d2af-6fafd18fdf2e.png)