前提
fast-api + postgreSQL を使用してapiサーバを作成しようとしています。
ソースコードを書き終え、docker compose
を使用してコンテナを立ち上げようとしたのですが、うまくポートがつながらずサーバに通信することができません。
開発は asdf + poetry で行っています。
実現したいこと
- WSLやWindowsからコンテナ上で動いているサーバに
localhost
でアクセスできるようにしたい。
該当のソースコード
ディレクトリ構成は以下です。
. ├ radish_db/ │ ├ main.py │ ├ docker/ │ ├ radish_db │ │ └ Dockerfile │ ├ docker-compose.yaml ├ pyproject.toml
pyproject.toml
toml
1[tool.poetry] 2name = "radish_db" 3version = "0.1.0" 4description = "" 5authors = ["*********"] 6 7[tool.poetry.dependencies] 8python = "^3.10" 9fastapi = {extras = ["all"], version = "^0.88.0"} 10sqlalchemy = "^1.4.45" 11pyhumps = "^3.8.0" 12psycopg2 = "^2.9.5" 13 14[tool.poetry.group.dev.dependencies] 15flake8 = "^6.0.0" 16black = "^22.12.0" 17isort = "^5.11.4" 18mypy = "^0.991" 19pytest = "^7.2.0" 20 21[build-system] 22requires = ["poetry-core>=1.0.0"] 23build-backend = "poetry.core.masonry.api" 24 25[tool.black] 26line-length = 120 27exclude = ''' 28( 29 /( 30 \.eggs 31 |\.git 32 |\.mypy_cache 33 |\.venv 34 |build 35 |dist 36 )/ 37 | snapshots/snap_.*\.py 38 | .*\.conf\.py 39) 40''' 41 42[tool.isort] 43# https://black.readthedocs.io/en/stable/compatible_configs.html#isort 44multi_line_output = 3 45include_trailing_comma = true 46force_grid_wrap = 0 47use_parentheses = true 48ensure_newline_before_comments = true 49line_length = 120 50skip_glob = [ 51 ".venv", 52 "**/snapshots/snap_*.py", 53 "*.conf.py", 54]
Dockerfile
Dockerfile
1FROM python:3.10-bullseye AS deps 2RUN apt-get update && apt-get install -y \ 3 curl \ 4 libpq-dev # psycopg2 \ 5 && apt-get clean \ 6 && rm -rf /var/lib/apt/lists/* 7 8 9RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/etc/poetry python3 - --version 1.3.1 10 11WORKDIR /app 12COPY ./pyproject.toml ./poetry.lock /app/ 13RUN /etc/poetry/bin/poetry export -f requirements.txt --output requirements.txt 14 15 16# --- 17FROM python:3.10-bullseye 18RUN apt-get update \ 19 && apt-get install -y --no-install-recommends \ 20 libpq5 \ 21 && apt-get clean \ 22 && rm -rf /var/lib/apt/lists/* 23 24WORKDIR /app 25 26COPY --from=deps /app/requirements.txt /app/requirements.txt 27RUN pip install --upgrade pip 28RUN pip install --no-cache-dir -r requirements.txt 29 30COPY ./ /app
docker-compose.yaml
yaml
1version: '3' 2services: 3 apiserver: 4 build: 5 context: . 6 dockerfile: ./docker/radish_db/Dockerfile 7 ports: 8 - 8000:8000 9 command: uvicorn radish_db.main:app 10 tty: true 11 depends_on: 12 - db 13 14 db: 15 image: postgres:15.1-bullseye 16 volumes: 17 - postgres_data:/var/lib/postgresql/data 18 environment: 19 - POSTGRES_USER=${DB_USER} 20 - POSTGRES_PASSWORD=${DB_PASS} 21 - POSTGRES_DB=${DB_NAME} 22 - TZ="Asia/Tokyo" 23 command: -p ${DB_PORT} 24 25volumes: 26 postgres_data:
試したこと
❯ docker compose up -d ... ❯ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f6277968143 radish-db-apiserver "uvicorn radish_db.m…" 2 minutes ago Up 8 seconds 0.0.0.0:8000->8000/tcp radish-db-apiserver-1 07a17277d0cc postgres:15.1-bullseye "docker-entrypoint.s…" 2 minutes ago Up 9 seconds 5432/tcp radish-db-db-1 ❯ curl http://localhost:8000/ curl: (52) Empty reply from server
docker ps
を見るとコンテナの8000番とlocalhostの8000番がつながっているように見えますが、curlでは応答が帰ってきません。
❯ docker exec -it radish-db-apiserver-1 bash root@5f6277968143:/app# curl http://localhost:8000/ {"detail":"Not Found"}
また、docker exec
でコンテナ内に入ってcurlしてみると応答は返ってくるのでサーバ自体は動いているのかと思います。
補足情報(FW/ツールのバージョンなど)
Windows10 Home
WSL2 Ubuntu 20.04.4
Docker version 20.10.21
Python 3.10.8
Poetry (version 1.3.1)
回答1件