๐Ÿ‹Docker ๋ง›๋ณด๊ธฐ๐Ÿณ

์ง„์ด์ง„์ดยท2022๋…„ 6์›” 20์ผ
0
post-thumbnail
post-custom-banner

Docker๋ž€

์ปจํ…Œ์ด๋„ˆ ๊ธฐ๋ฐ˜์˜ ์˜คํ”ˆ์†Œ์Šค ๊ฐ€์ƒํ™” ํ”Œ๋žซํผ์œผ๋กœ ๊ณ„์† ๋ฐ”๋€Œ๋Š” ์„œ๋ฒ„ ํ™˜๊ฒฝ๊ณผ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅ!!!

๊ทธ๋Ÿผ ์™œ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€

Docker๋Š” ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์™€ ๊ฒฉ๋ฆฌ๋˜์–ด ๊ฐ€์ƒ๋จธ์‹ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ฐ€์ƒ๋จธ์‹ ์ด ์•„๋‹ˆ๊ณ  "๊ฒฉ๋ฆฌ๋งŒ" ํ•ด์ฃผ๊ธฐ ๋–„๋ฌธ์— VM๊ณผ ๋‹ค๋ฅด๊ฒŒ ์„ฑ๋Šฅ์ƒ ํ•˜๋ฝ์ด ํฌ์ง€ ์•Š๋‹ค. ( ์ปจํ…Œ์ด๋„ˆํ™” )

  • ๋ง›๋ณด๊ธฐ ์ „ LXC์— ๋Œ€ํ•ด์„œ

    LXC (LinuX Containers)๋Š” ๋‹จ์ผ ์ปจํŠธ๋กค ํ˜ธ์ŠคํŠธ ์ƒ์—์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ณ ๋ฆฝ๋œ ๋ฆฌ๋ˆ…์Šค ์‹œ์Šคํ…œ (์ปจํ…Œ์ด๋„ˆ)๋“ค์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์šด์˜ ์‹œ์Šคํ…œ ๋ ˆ๋ฒจ ๊ฐ€์ƒํ™” ๋ฐฉ๋ฒ•์ด๋‹ค.

    ๋Œ€ํ‘œ์ ์ธ ๊ธฐ์ˆ ์€ cgroups(control groups)๋กœ ํ”„๋กœ์„ธ์Šค๋“ค์˜ ์ž์›์˜ ์‚ฌ์šฉ(CPU, ๋ฉ”๋ชจ๋ฆฌ, ๋””์Šคํฌ ์ž…์ถœ๋ ฅ, ๋„คํŠธ์›Œํฌ ๋“ฑ)์„ ์ œํ•œํ•˜๊ณ  ๊ฒฉ๋ฆฌ์‹œํ‚ค๋Š” ๋ฆฌ๋ˆ…์Šค ์ปค๋„ ๊ธฐ๋Šฅ์ด๋‹ค. ๋˜ํ•œ ์šด์˜ ํ™˜๊ฒฝ์„ ์™„์ „ํžˆ ๊ณ ๋ฆฝ์‹œํ‚ค๊ธฐ ์œ„ํ•ด namespace isolation์„ ์ œ๊ณตํ•œ๋‹ค

    ์ด๋Ÿฐ LXC ๋ฐฉ๋ฒ•์„ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Docker๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค!!

DOCKER IMAGE

์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์˜ ๋ชจ์Œ์œผ๋กœ
์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ Dockerfile์ด๋ผ๋Š” ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค

  1. ์ด๋ฏธ์ง€ ํŒŒ์ผ
FROM openjdk:8-jdk-alpine                            #๋ฒ ์ด์Šค ์ด๋ฏธ์ง€ ํŒŒ์ผ
COPY build/libs/newweek03-0.0.1-SNAPSHOT.jar app.jar #์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€๋กœ ํŒŒ์ผ ๋ณต์‚ฌ
ENTRYPOINT ["java","-jar","/app.jar"]                #์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ๋˜๋ฉด์„œ ์‹คํ–‰
  1. ์ด๋ฏธ์ง€ ์ƒ์„ฑ
    docker build -t {์ด๋ฏธ์ง€๋ช…}:{ํƒœ๊ทธ} {Dockerfile ํŒŒ์ผ ์œ„์น˜}

DOCKER CONTAINER

์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
docker run --name {์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„} -p {ํ˜ธ์ŠคํŠธ ํฌํŠธ}:{์ปจํ…Œ์ด๋„ˆ ํฌํŠธ} -d {์ด๋ฏธ์ง€๋ช…}:{์ด๋ฏธ์ง€ํƒœ๊ทธ}

docker ps                 #์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ
docker stop #{์ปจํ…Œ์ด๋„ˆ๋ช…}   #์ปจํ…Œ์ด๋„ˆ ์ •์ง€
docker ps -a              #์ •์ง€๋œ ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ
docker rm {์ปจํ…Œ์ด๋„ˆ๋ช…}      #์ •์ง€๋œ ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ

์‚ฌ์šฉํ•ด๋ณด๊ธฐ ( Spring boot )

  1. test๋Š” ์ œ์™ธํ•˜๊ณ  build ํ•ด๋ณด์ž ๋น ๋ฅธ๊ฒŒ ์ข‹์ž–์•„

  2. Docker ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ
    docker build -t my-springboot .

  3. Docker ์ปจํ…Œ์ด๋„ˆ ๋งŒ๋“ค๊ธฐ
    docker run --name sb -p 8080:8080 -d my-springboot

์ด๋ฏธ์ง€ ๋ ˆ์ด์–ด

image๋ฅผ ์ด์šฉํ•ด docker run์„ ํ•˜๊ฒŒ ๋˜๋ฉด ํŒŒ์ผ ์‹œ์Šคํ…œ ์˜์—ญ์— ์ด๋ฏธ์ง€๋ฅผ ๋ณต์‚ฌํ•œ ํ›„ ์ตœ์ƒ๋‹จ์— ์ปจํ…Œ์ด๋„ˆ ๋ ˆ์ด์–ด(Container layer) ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ์–‡์€ ๋ ˆ์ด์–ด๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ์œ ๋‹ˆ์˜จ ํŒŒ์ผ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์Šคํƒ ๊ตฌ์กฐ๊ฐ€ ํ•˜๋‚˜์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ฒ˜๋Ÿผ ๋ณด์ด๋„๋ก ํ•˜๊ฒŒ ํ•œ๋‹ค.

  • DBeaver๋กœ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ

  1. Mysql ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
    docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql

  2. ํ…Œ์ด๋ธ” ์ƒ์„ฑ

  3. ๋ฐ์ดํ„ฐ ์ €์žฅ

  4. ํ…Œ์ŠคํŠธ

    docker stop some-mysql
    docker rm some-mysql
    docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql

์ด๋ฏธ์ง€ ํ™œ์šฉ

1. ๋„์ปค ํ—ˆ๋ธŒ Repository ๋งŒ๋“ค๊ธฐ

์‚ฌ์ดํŠธ : https://hub.docker.com/

  • Repository์— pushํ•˜๊ธฐ
    - Tagging
    docker images
    docker tag {์ด๋ฏธ์ง€๋ช…}:{ํƒœ๊ทธ} {๋ฆฌํŒŒ์ง€ํ† ๋ฆฌ ์ด๋ฏธ์ง€๋ช…}:{ํƒœ๊ทธ}
    - Login
    docker login
    - Push
    docker push {๋ฆฌํŒŒ์ง€ํ† ๋ฆฌ ์ด๋ฏธ์ง€๋ช…}:{ํƒœ๊ทธ}

2. EC2 ์—์„œ ์ด๋ฏธ์ง€ ์‚ฌ์šฉํ•˜๊ธฐ

  1. cmd ๊ฒฝ๋กœ ๋ช…๋ น์–ด
    ssh -i {key} ubuntu@{ํผ๋ธ”๋ฆญip}

  2. ๋„์ปค ์„ค์น˜(install.sh์— ๋ณต์‚ฌ) ํ›„ ์‹คํ–‰

    sudo apt-get update
    
    sudo apt-get -y install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common
    
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
    sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
    
    sudo apt-get update
    
    sudo apt-get install -y docker-ce
    
    sudo usermod -aG docker $USER
  3. ์ด๋ฏธ์ง€ PULL
    docker pull {๋„์ปค ํ—ˆ๋ธŒ ์ด๋ฏธ์ง€๋ช…}:{ํƒœ๊ทธ}

  4. ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰
    docker run --name {์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„} -p 80:8080 -d {์ด๋ฏธ์ง€ ์ด๋ฆ„}:{ํƒœ๊ทธ}

๋‹ค์ค‘ ์ปจํ…Œ์ด๋„ˆ ์šด์˜

Docker Compose

์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ชจ์•„์„œ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํˆด๋กœ docker-compose.yml ๋ผ๋Š” ํŒŒ์ผ์— ์ปจํ…Œ์ด๋„ˆ์˜ ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ์ •์˜ํ•จ์œผ๋กœ์จ ๋™์ผ ํ˜ธ์ŠคํŠธ์ƒ์˜ ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ผ๊ด„์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. docker-compose.yaml ์„ค์ • ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

    version: "3.9"
    services:
      some-springboot:
        image: my-springboot
        ports:
          - 8080:8080
        networks:
          - backend
      some-mysql:
        image: mysql
        volumes:
          -  C:/Users/USER/Documents/Git/weeklytest/spring0607/mysql-data:/var/lib/mysql
        ports:
          - 3306:3306
        environment:
          - MYSQL_ROOT_PASSWORD=1234
        networks:
          - backend
    networks:
      backend:
    
  2. application.properties

    spring.datasource.url=jdbc:mysql://some-mysql/mydb
    spring.datasource.username=root
    spring.datasource.password=1234
    spring.jpa.hibernate.ddl-auto=update
  3. ์‹คํ–‰
    docker-compose up -d

  4. ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ
    docker-compose ps

    MySQL ๋ฐ Docker Compose๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์ค‘ ์ปจํ…Œ์ด๋„ˆ ์•ฑ ๋งŒ๋“ค๊ธฐ

profile
๊ฐœ๋ฐœ ์–ด๋ฆฐ์ด
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€