LoginSignup
3
1

More than 1 year has passed since last update.

devcontainerのベースをAmazon Linuxにする

Posted at

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

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

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

.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>
.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. ベースイメージを変える

.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ユーザーがいない

[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ユーザーを追加する

.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コマンドがない

[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をインストール

.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コマンドがない

[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をインストール

.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できるようにする
  • オートコンプリートを追加する
.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
.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

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1