[Docker] compose example : flask

제이브로·2024년 2월 26일
0

Docker

목록 보기
45/48
post-thumbnail

1. docker compose example

Try Docker Compose 따라해보기

2. example : flask

2.1 docker compose version

docker compose version 확인

$ docker compose version

2.2 서비스 디렉토리 생성

서비스 디렉토리 생성

$ mkdir composetest
$ cd composetest

2.3 app.py 생성

app.py 파일 생성

  • $ cat > app.py
import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

2.4 requirements.txt

requirements.txt 파일 생성

  • $ cat > requirements.txt
import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

2.5 dockerfile 생성

dockerfile 생성

  • $ cat > dockerfile
FROM python:3.10-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

2.6 docker-compose.yml 생성

docker-compose.yml 생성

  • $ cat > docker-compose.yml
services:
  web:
    build: .
    ports:
      - "8000:5000"
  redis:
    image: "redis:alpine"

2.7 docker compose up

docker compose 실행

  • $ docker compose up

docker compose up

2.8 docker compose down

docker compose 중단

  • $ docker compose down

2.9 docker-compose yml 수정

docker-compose yml에 volumes 추가

services:
  web:
    build: .
    ports:
      - "8000:5000"
    volumes:
      - .:/code
    environment:
      FLASK_DEBUG: "true"
  redis:
    image: "redis:alpine"

2.10 docker compose up

docker compose를 백그라운드로 실행

  • $ docker compose up -d
$ docker compose up -d

2.11 docker compose ps

docker compose에 의해 실행된 컨테이너 목록 확인

$ docker compose ps

2.12 docker compose scale

docker compose scale을 통해 개수 조정

  • $ docker compose scale redis={개수}
$ docker compose scale redis=3

2.13 docker compose logs

docker compose의 로그 확인

$ docker compose logs web

2.14 docker compose stop

docker compose 중단

$ docker compose stop

2.15 docker compose down

docker compose 삭제

$ docker compose down

2.16 docker compose down --volumes

docker compose의 volume까지 삭제

$ docker compose down --volumes

3. References

  1. docker docs compose
profile
기록하지 않으면 기록되지 않는다.

0개의 댓글