I made a fastapi, alembic, postgres, sqlalchemy application, made a docker image, run http://127.0.0.1:8000/docs and try to create a user, but when sending a request I get the error ConnectionRefusedError: [Errno 111] Connection refused
ERROR: Exception in ASGI application
web-1 | Traceback (most recent call last):
….
web-1 | File “/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py”, line 873, in __connect_addr
web-1 | tr, pr = await connector
web-1 | File “/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py”, line 744, in _create_ssl_connection
web-1 | tr, pr = await loop.create_connection(
web-1 | File “uvloop/loop.pyx”, line 2039, in create_connection
web-1 | File “uvloop/loop.pyx”, line 2016, in uvloop.loop.Loop.create_connection
web-1 | ConnectionRefusedError: [Errno 111] Connection refused
docker compose and Dockerfile
version: '3.8'
services:
db:
image: postgres:14
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
migrate:
build: .
command: poetry run alembic upgrade head
volumes:
- .:/app
depends_on:
- db
environment:
DATABASE_URL: ${DATABASE_URL}
web:
build: .
command: poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
- migrate
environment:
DATABASE_URL: ${DATABASE_URL}
SECRET_KEY: ${SECRET_KEY}
ALGORITHM: ${ALGORITHM}
ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES}
volumes:
postgres_data:
FROM python:3.10.12-slim
WORKDIR /app
COPY pyproject.toml poetry.lock /app/
RUN pip install poetry && poetry config virtualenvs.create false && poetry install --no-dev
COPY . /app
ENV PYTHONUNBUFFERED=1
PYTHONPATH=/app
CMD ["poetry", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
.env
# PostgreSQL
POSTGRES_USER=postgres
POSTGRES_PASSWORD=55555
POSTGRES_DB=test_db
DATABASE_URL=postgresql+asyncpg://postgres:55555@localhost/test_db
# JWT
SECRET_KEY=b3af51adc9749da6dfa6debec10d4854384a662a20dbf6fd1114dce53327548c
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60
I’m recently learning Docker, I tried changing the settings of dockerfile and docker-compose, .env but I still don’t understand what the problem is
123 123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.