docker compose容器互聯

使用docker-compose編排容器時,如容器之間須要互相通訊,使用本地鏈接,須要使用容器名來代替localhostweb

"connection": "postgresql+psycopg2://postgres:123456@localhost/database"

鏈接串中的localhost須要替換成postgresql容器名sql

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

docker

"connection": "postgresql+psycopg2://postgres:123456@db/database"
相關文章
相關標籤/搜索