[Docker] DevContainer

1 minute read


DevContainer

  • SSH to EC2(Mac 기준)
  • Script to Install Docker on EC2(Ubuntu)
  • VSCode DevContainer
  • CLI to remote Docker

SSH to EC2

# generate a new ssh key(a new key is made in the ~/.ssh dir)
ssh-keygen -t ed25519 -C "zsu58@icloud.com"

# start ssh-agent in the background
eval "$(ssh-agent -s)"

# check to see if your ~/.ssh/config file exists in the default location
open ~/.ssh/config

# if the file doesn't exist, create the file
touch ~/.ssh/config

# open the file and modify the file to contain the following lines(check whether id_ed25519 is right)
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

# Add SSH private key to the ssh-agent and store passphrase in the keychain
ssh-add ~/.ssh/id_ed25519

# paste the ssh key in ec2
scp -i /Users/zsu/Projects/DE/aws/ubuntu_test.pem /Users/zsu/.ssh/id_ed25519.pub ubuntu@[IP-Address]:/home/ubuntu/.ssh/

# (EC2에서)
cat id_ed25519.pub >> ~/.ssh/authorized_keys

# ssh to ec2
ssh ubuntu@[IP-Address]

Script to Install Docker on EC2(Ubuntu)

#!/usr/bin/env bash

export DEBIAN_FRONTEND=noninteractive
export HOME=/root

# System dependencies
apt-get update && apt-get install -y make nano

# Install Docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io

# Install Docker Compose
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

# Enable standard user (ubuntu) to manage containers (required for Remote Containers)
groupadd docker
usermod -aG docker ubuntu
newgrp docker

VSCode DevContainer

# 아래 부분은 필요 없는 것으로 보임
# # Command+Shift+P 누른 후 아래 사항 검색
# remote-ssh connect to host

# # Add New SSH Host 클릭 후, ssh 계정@IP주소 입력
# ssh ubuntu@[IP-Address]

# # config 파일에 저장 (~/.ssh/config 선택)

# # config 파일의 연결할 EC2에 IdentityFile 추가
# Host [IP-Address]
#   HostName [IP-Address]
#   User ubuntu
#   IdentityFile ~/.ssh/id_ed25519

# Command+Shift+p 누른 후 아래 사항 검색
remote-ssh connect to host

# EC2의 IP주소 누르면 해당 EC2로 진입
# 이 상태에서 Remote Container 사용하면 EC2의 Docker Container에 연결 가능

CLI to remote Docker

docker context create devcont_zsu --docker "host=ssh://ubuntu@[IP-Address]"
docker context use devcont_zsu

# 원래대로 돌아오기
docker context use default

ref