DockerでPythonアプリにパッケージをインストールする
1.iconPythonアプリケーションの準備
code:test.py
# app.py
import gspread
from oauth2client.service_account import ServiceAccountCredentials
print("Hello, gspread!")
2.iconrequirements.txt の作成`
必要なPythonパッケージを指定するためにrequirements.txt を作成する
code:txt
gspread
oauth2client
3.iconDockerfileの作成
code:Dockerfile
# ベースイメージとして公式のPythonイメージを使用
FROM python:3.9-slim
# 作業ディレクトリを作成
WORKDIR /app
# requirements.txt をコンテナにコピー
COPY requirements.txt /app/
# 依存関係をインストール
RUN pip install --no-cache-dir -r requirements.txt
# アプリケーションのコードをコンテナにコピー
COPY . /app
# アプリケーションを実行