Architecture/Container

Docker ์ปจํ…Œ์ด๋„ˆ ๊ธฐ์ˆ ์˜ ๋ฐœ์ „๊ณผ์ •, ๋„์ปค ๊ตฌ์„ฑ์š”์†Œ, ๊ธฐ๋ณธ ํ™˜๊ฒฝ ๊ตฌ์„ฑ ๋ฐ ๋„์ปค ์„ค์น˜ ๋ฐฉ๋ฒ•, ์„ค์น˜ ํ™•์ธ

ํƒฑ์ ค 2023. 9. 24. 16:20

์ปจํ…Œ์ด๋„ˆ ๊ธฐ์ˆ ์˜ ๋ฐœ์ „ ๊ณผ์ •

  • Traditional Deployment
    • ํ•˜๋“œ์›จ์–ด ์œ„์— OS ์„ค์น˜ ํ›„ App ์‹คํ–‰์— ํ•„์š”ํ•œ Library๋ฅผ OS์— ์„ค์น˜
    • ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ํ•œ ์„œ๋ฒ„์—์„œ ์šด์˜ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ (๋น„์šฉ ํšจ์œจ์„ฑ)
    • ์„œ๋น„์Šค๊ฐ„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•จ์— ๊นŒ๋ผ ์„œ๋น„์Šค๊ฐ„ ํ™˜๊ฒฝ ๊ฒฉ๋ฆฌ๊ฐ€ ์š”๊ตฌ๋จ
  • Virtualized Deployment
    • Hypervisor: VM(Virtual Machine)์„ ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•จ
    • Guest OS(VM)์— ํ•˜๋“œ์›จ์–ด ์ž์›์„ ์—๋ฎฌ๋ ˆ์ดํŒ…์„ ํ•˜๋‹ค๋ณด๋‹ˆ ์„ฑ๋Šฅ ํ•˜๋ฝ์ด ๋ฐœ์ƒํ•˜๋ฉฐ, ๊ฐ€์ƒ๋จธ์‹ ์„ ๋„์šฐ๊ธฐ ์œ„ํ•œ ์ž์›์˜ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒ
  • Container Deployment
    • Docker : ๋Œ€ํ‘œ์ ์ธ Container Engine
    • chroot, namespace ๋“ฑ์„ ์ด์šฉํ•œ ๊ฒฉ๋ฆฌ๊ธฐ์ˆ  ์ œ๊ณต
    • Host Process์—์„œ ์‹คํ–‰๋˜๋Š” ํ™˜๊ฒฝ์ด๊ธฐ์— ์„ฑ๋Šฅํ•˜๋ฝ๊ณผ ์ž์› ์˜ค๋ฒ„ํ—ค๋“œ ๋ฌธ์ œ์—์„œ ๋ฒ—์–ด๋‚จ
  • Kubernetes Deployment
    • Kubernetes : ๋Œ€ํ‘œ์ ์ธ Container Orchestration System
    • ์—ฌ๋Ÿฌ ์„œ๋ฒ„์—์„œ ๊ตฌ์„ฑ๋œ ํ™˜๊ฒฝ์—์„œ Container๋ฅผ ์–ด๋–ป๊ฒŒ ์ž˜ ์šด์˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ชฉ์ ์œผ๋กœ ํ•จ

Docker์˜ ๊ตฌ์„ฑ์š”์†Œ

  • Image์™€ Container๋Š” Docker์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋‹จ์œ„๋กœ Image์™€ Container๋Š” 1:N ๊ด€๊ณ„
    • ์ด๋ฏธ์ง€(Image): Container๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•„์š”ํ•œ ์š”์†Œ๋กœ Container์˜ ๋ชฉ์ ์— ๋งž๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ์™€ ์˜์กด์„ฑ์ด ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฉฐ, ์ด๋•Œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ณ„์ธต์œผ๋กœ ๊ตฌ์„ฑ
    • ์ปจํ…Œ์ด๋„ˆ(Container): Host์™€ ๋‹ค๋ฅธ Container๋กœ๋ถ€ํ„ฐ ๊ฒฉ๋ฆฌ๋œ ์‹œ์Šคํ…œ ์ž์›๊ณผ ๋„คํŠธ์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋กœ Container์—์„œ ๋ฌด์—‡์„ ํ•˜๋“  ๊ธฐ๋ฐ˜์ด ๋˜๋Š” Image๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Œ
  • Docker Image์™€ Container ์ƒ์„ฑ ํ”„๋กœ์„ธ์Šค

  • Docker์˜ Image ์ด๋ฆ„ ๊ตฌ์„ฑ (ex: dockerhub/nginx:1.21)
    • ์ €์žฅ์†Œ ์ด๋ฆ„(Repository Name) : ์ƒ๋žต ์‹œ ๊ธฐ๋ณธ ์ €์žฅ์†Œ์ธ Dockerhub๋กœ ์ธ์‹
      • Public ๋„์ปค ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ : Dockerhub, QUAY
      • Private ๋„์ปค ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ : AWS ECR, Docker Registry
    • ์ด๋ฏธ์ง€ ์ด๋ฆ„(Image Name)
    • ์ด๋ฏธ์ง€ ํƒœ๊ทธ(Image Tag) : ์ƒ๋žต ์‹œ ์ตœ์‹  ๋ฆฌ๋น„์ „์„ ๊ฐ€๋ฆฌํ‚ค๋Š” lastest๋กœ ์ธ์‹

๊ธฐ๋ณธ ํ™˜๊ฒฝ ์„ค์น˜

  • Container๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Linux๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ์„œ๋น„์Šค์ด๊ธฐ์— Ubuntu ํ™˜๊ฒฝ์„ ์ถ”์ฒœ

Docker

  • Single Container๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋กœ ์ปค๋งจ๋“œ ๋ผ์ธ์—์„œ ๋ช…๋ น์–ด ์‹คํ–‰ ๊ฐ€๋Šฅ
  • ๊ด€๋ จ ๋งํฌ : https://docs.docker.com/engine/install/ubuntu
  • ํ•˜๊ธฐ์˜ Shell Script ์‹คํ–‰ (install-docker.sh)
#!/usr/bin/env bash
## INFO: https://docs.docker.com/engine/install/ubuntu/

set -euf -o pipefail

DOCKER_USER=ubuntu

# Install dependencies
sudo apt-get update && sudo apt-get install -y \
  apt-transport-https \
  ca-certificates \
  curl \
  gnupg \
  lsb-release

# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Set up the stable repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker CE
sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Use Docker without root
sudo usermod -aG docker $DOCKER_USER
  • Docker ์‹คํ–‰ ์‹œ ํ•˜๊ธฐ์˜ ๊ถŒํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด /var/run/docker.sock ํŒŒ์ผ์˜ ๊ถŒํ•œ ๋ณ€๊ฒฝ ์ง„ํ–‰ ํ•„์š”
# Docker Test : with Permission Error
docker ps -a
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

# Solution 1 : Change File Permission
sudo chmod 666 /var/run/docker.sock

# Solution 2 : Change Group Ownership
sudo chown root:docker /var/run/docker.sock

Docker-Compose

  • yaml ํŒŒ์ผ ๊ธฐ๋ฐ˜์œผ๋กœ Multi Container๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” Client ์„œ๋น„์Šค
  • yaml ํŒŒ์ผ์— ๋ช…๋ น์–ด๋ฅผ ์ ์–ด์„œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ด€๋ฆฌ
  • ๊ด€๋ จ ๋งํฌ : https://docs.docker.com/compose/install
  • ํ•˜๊ธฐ์˜ Shell Script ์‹คํ–‰ (install-docker-compose.sh)
#!/usr/bin/env bash
## INFO: https://docs.docker.com/compose/install/

set -euf -o pipefail

DOCKER_COMPOSE_VERSION=v2.1.1

# Download and install
sudo curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

๋„์ปค ์„ค์น˜ ํ›„

๋„์ปค ๋ฒ„์ „ ํ™•์ธ

sudo docker version

๊ถŒํ•œ ์˜ค๋ฅ˜ ์‹œ ๋ช…๋ น์–ด ์ž…๋ ฅ

 sudo chmod 666 /var/run/docker.sock

๋„์ปค ์ด๋ฏธ์ง€ ๋‹ค์šด๋ฐ›๊ธฐ

docker pull ubuntu:latest

๋„์ปค ์ด๋ฏธ์ง€ ํ™•์ธ

docker images

์ด๋ฏธ์ง€๋ฅผ ์ปจํ…Œ์ด๋„ˆ๋กœ ์‹คํ–‰ํ•œ ๋’ค bash shell ์—ด๊ธฐ -> ์ƒˆ๋กœ์šด shell๋กœ ์ ‘์†๋œ ๊ฒƒ ํ™•์ธ

docker run -i -t —name ubuntu01 ubuntu /bin/bash

 

exit ํ•˜๊ณ  ๋น ์ ธ๋‚˜์™€์„œ ๋‹ค์‹œ ubuntu01 ์‹œ์ž‘ํ• ๋•Œ

docker restart ubuntu01

 

๋‹ค์‹œ ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†

docker attach ubuntu01

์‹คํ–‰ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ ํ›„ ์ข…๋ฃŒ

docker ps -a -q

docker stop (์ปจํ…Œ์ด๋„ˆid)

์ปจํ…Œ์ด๋„ˆ ์žฌ์‹คํ–‰ํ•ด๋ณด๊ธฐ

docker restart 4eee6afdeec6

 

์žฌ์‹คํ–‰ ํ™•์ธ

docker ps

์ปจํ…Œ์ด๋„ˆ ํ™•์ธํ•˜๊ธฐ

docker ps -a

์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ

docker rm

 

์ด๋ฏธ์ง€ ์‚ญ์ œ

docker rmi ์ด๋ฏธ์ง€์ด๋ฆ„:์ด๋ฏธ์ง€ํƒœ๊ทธ
728x90