Day 47 Docker(2)

๐Ÿ“‹ย ๊ณต๋ถ€ ๋‚ด์šฉ

hangman web ์‹ค์Šต

flask web page ์‹คํ–‰ํ•ด๋ณด๊ธฐ

  • play-with-docker ์‚ฌ์ดํŠธ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ…Œ์ŠคํŠธ

  • git clone์œผ๋กœ fork ํ•ด๋‘” repo๋ฅผ ๋ฐ›์•„์˜ค๊ณ , ์‹คํ–‰ํ•˜๊ธฐ

1
2
3
4
git clone https://github.com/srlee056/hangman_web.git
cd hangman_web/
pip install -r requirements.txt
python3 -m flask run --host 0.0.0.0 --port=4000

open port ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ํ•ด๋‹น ์„œ๋ฒ„์˜ ํŠน์ • ํฌํŠธ๋กœ ์ ‘์† ๊ฐ€๋Šฅ

๊ฒฐ๊ณผ ํ™”๋ฉด

docker ์ด๋ฏธ์ง€ ๋นŒ๋“œ ์‹ค์Šต

Dockerfile ์ž‘์„ฑ

1
2
3
4
5
6
7
8
9
FROM python:3.8-slim-buster
WORKDIR /app
COPY app.py ./
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
EXPOSE 4000
CMD python3 -m flask run --host 0.0.0.0 --port=4000
# ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ ์–ด๋„ ๋™์ž‘์€ ๊ฐ™์Œ
CMD ["python3", "-m", "flask", "run", "--host", "0.0.0.0", "--port=4000"]

docker build & test

  • build
1
docker build -t hangman-web .
  • run
    • -p ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์„œ ํฌํŠธ ํฌ์›Œ๋”ฉ ์ง„ํ–‰
    • ์™ธ๋ถ€ ํฌํŠธ : ๋‚ด๋ถ€ ํฌํŠธ ์ˆœ์„œ๋กœ ์ž‘์„ฑํ•œ๋‹ค
1
docker run -p 4000:4000 hangman-web

๊ฒฐ๊ณผ ํ™”๋ฉด

push image to docker Hub

1
2
3
docker tag hangman-web seorim/hangman-web
docker login --username=seorim
docker push seorim/hangman-web

uploaded on dockerhub

test in another host

1
2
docker pull seorim/hangman-web
docker run -p 4000:4000 seorim/hangman-web

command line

๊ฒฐ๊ณผ ํ™”๋ฉด

docker run detach ์˜ต์…˜

  • ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์˜ต์…˜
1
docker run -p 4000:4000 -d seorim/hangman-test

docker ps๋กœ active container์ž„์„ ํ™•์ธ ๊ฐ€๋Šฅ

CI/CD

Continuous Integration

One of Software Engineering Practice

๊ธฐ๋ณธ ์›์น™

  • ์ฝ”๋“œ Repository๋Š” ํ•˜๋‚˜๋งŒ ์œ ์ง€ (Main)
  • ์ฝ”๋“œ ๋ณ€๊ฒฝ์„ ์ž์ฃผ ๋ฐ˜์˜
  • ํ…Œ์ŠคํŠธ๋ฅผ ์ตœ๋Œ€ํ•œ ์ถ”๊ฐ€ (Test Coverage)
  • ๋นŒ๋“œ์˜ ์ž๋™ํ™” (Commit Build vs. Nightly Build)
  • ์„ฑ๊ณตํ•œ ๋นŒ๋“œ๋ฅผ ํ”„๋กœ๋•์…˜์œผ๋กœ ๋ฆด๋ฆฌ์Šค ์ž๋™ํ™”: CD

CI/CD

  • CI/CD ์ž๋™ํ™” ๊ณผ์ •์— ๋„์ปค๋ฅผ ํ™œ์šฉ

GitHub Actions

GitHub์˜ CI/CD service

workflow

Push, PR ๋“ฑ ํŠธ๋ฆฌ๊ฑฐ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์‹œ์ž‘๋˜๋Š” ์ผ๋ จ์˜ ๋™์ž‘์„ ์ง€์นญ

  • {workflow_name}.yaml(or .yml)
  • Job์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๋ฉฐ, ๊ฐ Job์€ ์ผ๋ จ์˜ Step์„ ์ˆ˜ํ–‰ํ•จ

GitHub Actions ์‚ฌ์šฉ ์‹ค์Šต

  1. ๊ณตํ†ต์ ์ธ ๋ถ€๋ถ„

workflow ํŒŒ์ผ์˜ ๊ตฌ์กฐ๋Š” ๋ณดํ†ต ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
name: app-name

on: # trigger events
    push:
        branches: ["main"]  # name of branches
    pull_request:
        branches: ["main"]

permissions:
    contents: read

jobs:
    build:
        runs-on: ubuntu-latest

        steps:
        ...
  1. ํ…Œ์ŠคํŠธ ์ž๋™ํ™”
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
    uses: actions/setup-python@v3
    with:
    python-version: "3.10"
- name: Install dependencies
    run: |
    python -m pip install --upgrade pip
    pip install flake8
    if [ -f requirements.txt ]; then pip install -r requirements.txt; fi    
- name: Lint with flake8
    run: |
    # stop the build if there are Python syntax errors or undefined names
    flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
    # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
    flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics    
- name: Test with unittest
    run: |
    python3 -m unittest discover -p 'test*.py' # python3 test.py    
  1. Docker Build & Push ์ž๋™ํ™”
  • repository settings -> secrets & variables -> Actions ์—์„œ ๋กœ๊ทธ์ธ ์ •๋ณด (ID/PW) ์ €์žฅํ•˜๊ณ  ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ${{secrets.DOCKER_USER}} ํ˜•์‹์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํŒŒ์ผ์—์„œ ์‚ฌ์šฉํ•  ๋• env์—์„œ ์ง€์ •ํ•˜๊ฑฐ๋‚˜, ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
- name: Login to the Docker Hub
    env:
        DOCKER_USER: ${{secrets.DOCKER_USER}}
        DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
    run: docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: Build the Docker image
    run: docker build --tag ${{secrets.DOCKER_USER}}/hangman-web:latest .
- name : Push the Docker image to the Docker Hub
    run: docker push ${{secrets.DOCKER_USER}}/hangman-web:latest

๐Ÿ‘€ย CHECK

(์–ด๋ ต๊ฑฐ๋‚˜ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๋“ฑ ๋‹ค์‹œ ํ™•์ธํ•  ๊ฒƒ๋“ค)

โ— ๋Š๋‚€ ์ 

์˜ค๋Š˜์€ dockerfile์„ ์ง์ ‘ ์ž‘์„ฑํ•ด๋ณด๊ณ , GitHub Actions๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค. Dockerfile์€ ์ง์ ‘ ์ž‘์„ฑํ•ด์„œ ๋ฌธ์ œ์—†์ด ๋นŒ๋“œ ๋๋‹ค๋Š” ๊ฒƒ์— ์˜์˜๋ฅผ ๋’€๊ณ , GitHub Actions๋Š” ์ด๋ฏธ ์‚ฌ์šฉํ•ด ๋ณธ ์ ์ด ๋ช‡๋ฒˆ ์žˆ์–ด์„œ ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ฐ•์˜ ๋“ฃ๊ณ  ์ •๋ฆฌํ•˜๋Š”๊ฒŒ ์ผ์ฐ ๋๋‚˜์„œ ๋ธ”๋กœ๊ทธ์— ๋Œ“๊ธ€๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค. ๋‹ค์Œ์—” viewer ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ๊นŒ ๊ณ ๋ฏผ์ค‘.

Hugo๋กœ ๋งŒ๋“ฆ
Jimmy์˜ Stack ํ…Œ๋งˆ ์‚ฌ์šฉ ์ค‘