使用docker-compose编排容器时,如容器之间需要互相通信,使用本地连接,需要使用容器名来代替localhost
"connection": "postgresql+psycopg2://postgres:123456@localhost/database"
连接串中的localhost需要替换成postgresql容器名
version: '3' services: db: build: ./db environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=123456 - POSTGRES_DB=database volumes: - ./db/init:/docker-entrypoint-initdb.d/ container_name: db restart: always ports: - "5432:5432" web: build: ./web restart: always volumes: - ./web/codes:/code ports: - "80:8080" links: - db depends_on: - db container_name: web
即
"connection": "postgresql+psycopg2://postgres:123456@db/database"