I can’t migrate to database from container
I write docker exec -it my_container_app bash
Writing
alembic revision –autogenerate -m “comment”
I get the following error
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
Is the server running on that host and accepting TCP/IP connections?
(Background on this error at: https://sqlalche.me/e/20/e3q8)
And this happens precisely from the container. If I run migrations from Shell, then everything works.
My docker-compose file
version: "3.0"
services:
db:
container_name: "db"
image: postgres:14.1-alpine
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
ports:
- "5432:5432"
networks:
- custom
db_test:
container_name: "db_test"
image: postgres:14.1-alpine
restart: always
environment:
- POSTGRES_USER=postgres_test
- POSTGRES_PASSWORD=postgres_test
- POSTGRES_DB=postgres_test
ports:
- "5433:5432"
networks:
- custom
app:
container_name: "app"
image: app:latest
environment:
REAL_DATABASE_URL: "postgresql+asyncpg://postgres:postgres@db:5432/postgres"
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:80"
networks:
- custom
networks:
custom:
driver: bridge
My Dockerfile
FROM python:3.9
# Установка telnet
RUN apt-get update && apt-get install -y telnet
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
EXPOSE 80/tcp
I tried explicitly specifying localhost in the REAL DATABASE_URL like “postgresql+asyncpg://postgres:postgres@localhost:5432/postgres”, but it didn’t help.
New contributor
Vladislav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.