도커란?

이 글에서 도커가 뭔지에 대해서는 설명하지 않습니다.

궁금하시면 따로 찾아보시는 걸 권장드리나, 무지성으로 따라만 해도 됩니다.

목표

이런 문제를 해결하는 것이 목표입니다.

Docker 설치

우선 도커가 설치되어 있는지 확인합니다.

Untitled

만약 설치되어 있는데, 아이콘이 이와 다르다면 구버전이니 삭제 후 재설치가 필요합니다.

Managed Software Center에서 삭제/설치할 수 있습니다.

Untitled

init_docker.sh 실행

#!/usr/bin/env bash
# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    init_docker.sh                                     :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: aguiot-- <[email protected]>          +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2019/11/18 08:17:08 by aguiot--          #+#    #+#              #
#    Updated: 2020/02/20 14:34:42 by aguiot--         ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

# <https://github.com/alexandregv/42toolbox>

# Ensure USER variabe is set
[ -z "${USER}" ] && export USER=$(whoami)

################################################################################

# Config
docker_destination="/goinfre/$USER/docker" #=> Select docker destination (goinfre is a good choice)

################################################################################

# Colors
blue=$'\\033[0;34m'
cyan=$'\\033[1;96m'
reset=$'\\033[0;39m'

# Uninstall docker, docker-compose and docker-machine if they are installed with brew
brew uninstall -f docker docker-compose docker-machine &>/dev/null ;:

# Check if Docker is installed with MSC and open MSC if not
if [ ! -d "/Applications/Docker.app" ] && [ ! -d "~/Applications/Docker.app" ]; then
	echo "${blue}Please install ${cyan}Docker for Mac ${blue}from the MSC (Managed Software Center)${reset}"
	open -a "Managed Software Center"
	read -n1 -p "${blue}Press RETURN when you have successfully installed ${cyan}Docker for Mac${blue}...${reset}"
	echo ""
fi

# Kill Docker if started, so it doesn't create files during the process
pkill Docker

# Ask to reset destination if it already exists
if [ -d "$docker_destination" ]; then
	read -n1 -p "${blue}Folder ${cyan}$docker_destination${blue} already exists, do you want to reset it? [y/${cyan}N${blue}]${reset} " input
	echo ""
	if [ -n "$input" ] && [ "$input" = "y" ]; then
		rm -rf "$docker_destination"/{com.docker.{docker,helper},.docker} &>/dev/null ;:
	fi
fi

# Unlinks all symlinks, if they are
unlink ~/Library/Containers/com.docker.docker &>/dev/null ;:
unlink ~/Library/Containers/com.docker.helper &>/dev/null ;:
unlink ~/.docker &>/dev/null ;:

# Delete directories if they were not symlinks
rm -rf ~/Library/Containers/com.docker.{docker,helper} ~/.docker &>/dev/null ;:

# Create destination directories in case they don't already exist
mkdir -p "$docker_destination"/{com.docker.{docker,helper},.docker}

# Make symlinks
ln -sf "$docker_destination"/com.docker.docker ~/Library/Containers/com.docker.docker
ln -sf "$docker_destination"/com.docker.helper ~/Library/Containers/com.docker.helper
ln -sf "$docker_destination"/.docker ~/.docker

# Start Docker for Mac
open -g -a Docker

echo "${cyan}Docker${blue} is now starting! Please report any bug to: ${cyan}aguiot--${reset}"

어떤 카뎃 분께서 만드신 스크립트입니다. 이 스크립트를 실행하면 Docker Desktop이 켜집니다.

Untitled

켜지고 나면 이런 창을 볼 수 있습니다.

컨테이너 설정

docker run -dit --name test -p 22222:22 debian:latest
# 또는
docker run -dit --name node -p 12345:22 node:lts-bullseye
# 등등

적절한 이미지를 사용해 도커 컨테이너를 생성해주세요.

SSH로 연결할 포트를 반드시 하나는 열어주세요.

이제 컨테이너에 연결해주세요. 연결하는 방법은 아래와 같습니다.

docker exec -it test bash

접속 후 필요에 따라 clang, clangd, lldb, node, rustup 등 개발에 필요한 패키지들을 설치해주세요.

# 데비안 기준

# SSH 서버 설치
apt update && apt install openssh-server

# SSH로 접속할 사용자 생성
adduser user

# SSH 서버 설정
mkdir -p /run/sshd

# SSH 서버 실행
/usr/sbin/sshd

# 추가로, ssh 키를 컨테이너 안으로 복사했다면...
chmod 0400 ~/.ssh/id_rsa*

그리고 SSH 서버를 실행하면 컨테이너 설정은 완료됩니다.

vscode 설정

vscode에서 Remote - SSH 확장을 설치합니다.

Untitled

설치하면 왼쪽 아래에 remote 버튼이 생기는데, remote 버튼을 누른 후 Connect to Host를 선택합니다.

Add New SSH Host…를 선택한 후 ssh user@localhost -p 22222처럼 입력하면 끝!

이제 Connect to Host를 누르면 localhost가 나타납니다. 설정이 완료되었습니다!

컨테이너를 껐다 켠 경우

# 컨테이너에 접속 후
docker exec -it test bash

# SSH 서버 시작
/usr/sbin/sshd

컨테이너를 지웠다 다시 만든 경우

vi ~/.ssh/known_hosts
# [localhost]:22222 부분 삭제

개발환경 설정에 필요한 패키지 설치로 고통받으시는 분들께 도움이 되기를 바라며 마치겠습니다.