Search code examples
flutterdartdocker-composeconduit

docker-compose can't connect to postgres using conduit with flutter


I have the following docker-compose file:

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:

And the following DockerFile for my flutter dart api project:

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"]

I created database using dart run conduit db upgrade inside a conrainer, but i still can't connect to postgres database using Insomnia/Postman api requests. It says "Error: Couldn't connect to server" Successful conduit db upgrade

I tried launching docker-compose on different ports but it still didn't work. I am pretty sure my postgres works. I would appreciate the help


Solution

  • Instead of using network_mode: host, you should rather use docker networks and connect the containers together using that.

    Your docker-compose file could then look something like this:

    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: