I use docker-compose built a flask app with postgresql. But flask can not connect postgresql. Error message:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "postgres-db" (192.168.16.2) and accepting
TCP/IP connections on port 5432?
When I go inside the docker container, and ping postgres-db, it works well. And I start the flask app, it works well too. It only can not connect when I use docker-compose up
my docker-compose file:
version: '3.7'
services:
postgres-db:
restart: always
image: postgres:11.1-alpine
privileged: true
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: Aa123456
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- ./db/postgres/data:/var/lib/postgresql/data/pgdata
api:
build:
context: ./api
dockerfile: Dockerfile
volumes:
- './api:/usr/src/app'
ports:
- 5002:5000
environment:
- FLASK_CONFIG=development
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=postgres://postgres:Aa123456@postgres-db:5432/cms
- DATABASE_TEST_URL=postgres://postgres:Aa123456@postgres-db:5432/cms_test
- SECRET_KEY=ZQbn05PDeA7v11
depends_on:
- postgres-db
container_name: api
links:
- postgres-db:postgres-db
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
restart: unless-stopped
ports:
- 8080:8080
depends_on:
- api
- client
client:
build:
context: ./client
dockerfile: Dockerfile
volumes:
- './client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3008:3000
environment:
- NODE_ENV=development
- REACT_APP_SERVICE_URL=http://localhost:8080
- CHOKIDAR_USEPOLLING=true
depends_on:
- api
docker-compose up -dagain, does it work? (A frequent problem is applications starting before databases are fully initialized.)links:aren't necessary and are a potential source of complication, and I'd suggest just removing that.