iris_classifier/
├── Dockerfile
├── pyproject.toml
├── poetry.lock
├── src/
│ ├── main.py
│ └── model.joblib
└── README.md
from flask import Flask, request, jsonify
import joblib
import numpy as np
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
app = Flask(__name__)
def train_model():
iris = load_iris()
model = RandomForestClassifier(n_estimators=100)
model.fit(iris.data, iris.target)
joblib.dump(model, 'model.joblib')
return model
try:
model = joblib.load('model.joblib')
except:
model = train_model()
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
features = np.array(data['features']).reshape(1, -1)
prediction = model.predict(features)
return jsonify({
'prediction': int(prediction[0]),
'class_name': iris.target_names[prediction[0]]
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
[tool.poetry]
name = "iris-classifier"
version = "0.1.0"
description = "Iris classification service"
authors = ["Your Name <your@email.com>"]
[tool.poetry.dependencies]
python = "^3.9"
flask = "^2.0.1"
scikit-learn = "^1.0.2"
numpy = "^1.21.0"
joblib = "^1.1.0"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
# Dockerfile
# 첫 번째 스테이지: 빌더 이미지
FROM python:3.9-slim as builder
# Poetry 설치
ENV POETRY_HOME=/opt/poetry
ENV POETRY_VERSION=1.7.1
RUN python3 -m pip install poetry==${POETRY_VERSION}
# 작업 디렉토리 설정
WORKDIR /app
# Poetry 설정 파일 복사
COPY pyproject.toml poetry.lock ./
# Poetry를 사용하여 의존성 설치
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi
# 소스 코드 복사
COPY src/ ./src/
# 두 번째 스테이지: 최종 이미지
FROM python:3.9-slim
# 작업 디렉토리 설정
WORKDIR /app
# 빌더에서 필요한 파일들만 복사
COPY --from=builder /usr/local/lib/python3.9/site-packages/ /usr/local/lib/python3.9/site-packages/
COPY --from=builder /app/src/ ./src/
# 비root 사용자 생성 및 전환
RUN useradd -m appuser && chown -R appuser /app
USER appuser
# 서비스 포트 설정
EXPOSE 8080
# 애플리케이션 실행
CMD ["python", "src/main.py"]
curl -sSL https://install.python-poetry.org | python3 -
poetry new iris-classifier
cd iris-classifier
poetry add flask scikit-learn numpy joblib
poetry install
poetry run python src/main.py
docker build -t iris-classifier .
docker run -p 8080:8080 iris-classifier
curl -X POST http://localhost:8080/predict \
-H "Content-Type: application/json" \
-d '{"features": [5.1, 3.5, 1.4, 0.2]}'
{"prediction": 0, "class_name": "setosa"}
# pip.conf 설정이 이렇다면:
[global]
index-url = https://your-company-pypi.com/simple
trusted-host = your-company-pypi.com
find-links = https://your-company-packages.com/links
# poetry config.toml은 이렇게 됩니다:
[repositories]
company = "https://your-company-pypi.com/simple"
[certificates.company]
cert = false
# pyproject.toml은 이렇게 됩니다:
[[tool.poetry.source]]
name = "company"
url = "https://your-company-pypi.com/simple"
default = true
[[tool.poetry.source]]
name = "package-links"
url = "https://your-company-packages.com/links"
# config.toml
[certificates.custom]
cert = "/path/to/your/certificate.pem" # 실제 인증서 경로
curl -X POST http://localhost:8080/predict \
-H "Content-Type: application/json" \
-d '{"features": [5.1, 3.5, 1.4, 0.2]}'