dart docker-compose无法连接到postgres使用管道与Flutter

y3bcpkx1  于 2023-03-27  发布在  Docker
关注(0)|答案(1)|浏览(107)

我有以下的docker-compose文件:

version: '1'
services:

 postgres:
    image: postgres:latest
    container_name: postgres_api
    restart: always
    ports:
      - 5432:5432
    environment:
     - POSTGRES_PASSWORD=123
     - POSTGRES_USER=postgres
     - POSTGRES_DB=postgres
    # - PGDATA=/var/lib/postgresql/data/pgdata
 lib:
    restart: on-failure
    container_name: main_api
    build: ./
    network_mode: host
    # entrypoint: dart run /app/bin/conduittest.dart conduit:conduit
    environment:
      - DB_USERNAME=postgres
      - DB_PASSWORD=123
      - DB_HOST=127.0.0.1
      - DB_PORT=5432
      - DB_NAME=postgres
      - SECRET_KEY=SECRET_KEY
    depends_on:
      - postgres
    ports:
      - 8888:8888
volumes:
  dart_api:

下面是我的flutter dart API项目的DockerFile:

FROM dart:2.18.6-sdk

# Resolve app dependencies.
WORKDIR /app
COPY pubspec.* ./
RUN dart pub get

# Copy app source code and AOT compile it.
COPY . .
# Ensure packages are still up-to-date if anything has changed
RUN dart pub get

#RUN dart compile exe bin/conduittest.dart -o bin/conduittest
RUN dart pub global activate conduit 4.1.6

# Build minimal serving image from AOT-compiled `/server` and required system
# libraries and configuration files stored in `/runtime/` from the build stage.
#FROM scratch
#COPY --from=build /runtime/ /
#COPY --from=build /app/bin/conduittest.dart /app/bin/

# Start server.
EXPOSE 8888
#CMD ["/app/bin/conduittest","dart"]
#RUN dart run conduit db upgrade

ENTRYPOINT  ["dart","run","/app/bin/conduittest.dart","conduit:conduit"]

我在conrainer中使用dart run conduit db upgrade创建了数据库,但我仍然无法使用Insomnia/Postman API请求连接到postgres数据库。它说“错误:无法连接到服务器”Successful conduit db upgrade
我尝试在不同的端口上启动docker-compose,但仍然不起作用。我很确定我的postgres工作正常。我将感谢帮助

gcxthw6b

gcxthw6b1#

与其使用network_mode: host,不如使用Docker网络,并使用它将容器连接在一起。
然后,您的docker-compose文件可能看起来像这样:

version: "3.9"

services:
 postgres:
    image: postgres:latest
    container_name: postgres_api
    restart: always
    ports:
      - 5432:5432
    environment:
     - POSTGRES_PASSWORD=123
     - POSTGRES_USER=postgres
     - POSTGRES_DB=postgres
    # - PGDATA=/var/lib/postgresql/data/pgdata
    networks:
      - database
 lib:
    restart: on-failure
    container_name: main_api
    build: ./
    # entrypoint: dart run /app/bin/conduittest.dart conduit:conduit
    environment:
      - DB_USERNAME=postgres
      - DB_PASSWORD=123
      - DB_HOST=postgres_api
      - DB_PORT=5432
      - DB_NAME=postgres
      - SECRET_KEY=SECRET_KEY
    depends_on:
      - postgres
    ports:
      - 8888:8888
    networks:
      - database
volumes:
  dart_api:

networks:
  database:

相关问题