Try Docker Compose 따라해보기
docker compose version 확인
$ docker compose version
서비스 디렉토리 생성
$ mkdir composetest
$ cd composetest
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)
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)
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"]
docker-compose.yml 생성
$ cat > docker-compose.yml
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
docker compose
실행
$ docker compose up
docker compose
중단
$ docker compose down
docker-compose yml
에 volumes 추가
services:
web:
build: .
ports:
- "8000:5000"
volumes:
- .:/code
environment:
FLASK_DEBUG: "true"
redis:
image: "redis:alpine"
docker compose를 백그라운드로 실행
$ docker compose up -d
$ docker compose up -d
docker compose에 의해 실행된 컨테이너 목록 확인
$ docker compose ps
docker compose scale을 통해 개수 조정
$ docker compose scale redis={개수}
$ docker compose scale redis=3
docker compose의 로그 확인
$ docker compose logs web
docker compose 중단
$ docker compose stop
docker compose 삭제
$ docker compose down
docker compose의 volume까지 삭제
$ docker compose down --volumes