dockergraphql-federation

Expose docker containers running in bridge network to localhost


I am running multiple containers for GQL Federation 2 with Apollo Router.

I need to create the supergraph using rover cli, so I am running the containers in custom bridge network, and exposing their ports mapping them to different ports on localhost, however, when I try to access it from localhost, the server throws connection refused.

Here is example of my docker-compose.yaml `version: '3.9'

services:
  users-service:
    build: 
      context: .
      dockerfile: apps/account-api/Dockerfile
    container_name: users-service
    restart: always
    ports:
      - "3001:3000"
    depends_on:
      - postgres
    networks:
      - custom-bridge
    env_file:
      - .env
    volumes:
      - ./users-service:/usr/src/app/users-service
  therapy-service:
    build: 
      context: .
      dockerfile: apps/therapy-api/Dockerfile
    container_name: therapy-service
    restart: always
    ports:
      - "3002:3000"
    depends_on:
      - postgres
    networks:
      - custom-bridge
    env_file:
      - .env
    volumes:
      - ./therapy-service:/usr/src/app/therapy-service
  patientcard-service:
    build: 
      context: .
      dockerfile: apps/patientcard-api/Dockerfile
    container_name: patientcard-service
    restart: always
    ports:
      - "3003:3000"
    depends_on:
      - postgres
    networks:
      - custom-bridge
    env_file:
      - .env
    volumes:
      - ./patientcard-service:/usr/src/app/patientcard-service
  notifications-service:
    build:
      context: .
      dockerfile: apps/notifications-api/Dockerfile
    container_name: notifications-service
    restart: always
    networks:
      - custom-bridge
    ports:
      - "3007:3000"
    depends_on:
      - postgres
    env_file:
      - .env
    volumes:
      - ./notifications-service:/usr/src/app/notifications-service
  gateway-router:
    image: ghcr.io/apollographql/router:v1.13.0
    container_name: gateway-router
    restart: always
    depends_on:
      - users-service
      - therapy-service
      - patientcard-service
      - postgres
      # - drugs-api
    volumes:
      - ./apps/api/src/supergraph-schema.graphql:/supergraph-schema.graphql
      - ./apps/api/src/router.yaml:/router.yaml
    ports:
      - "3000:3000"
    networks:
      - custom-bridge
  postgres:
    image: postgres
    restart: on-failure
    volumes:
      - ./.tmp/db:/var/lib/postgresql/data
    ports:
      - 5444:5432
    environment:
      - POSTGRES_DB=******
      - POSTGRES_USER=******
      - POSTGRES_PASSWORD=******
    networks:
      - custom-bridge
  redis:
    image: redis:7-alpine
    restart: always
    ports:
      - '6333:6379'
    command: redis-server --save 20 1 --loglevel warning --requirepass googleisshit
    volumes: 
      - ./.tmp/redis:/data
    networks:
    - custom-bridge

networks:
  custom-bridge:
    driver: bridge`

I am also exposing port 3000 in every Dockerfile and every service is listening on that port. When I run the service locally it works.

I guess my question is "Does the docker bridge network even expose containers for localhost on the host machine?"

I tried exposing the containers this way and expected to connect to each with localhost:[port], but it doesn't budge.

Edit

docker ps returns this

CONTAINER ID   IMAGE  COMMAND   CREATED   STATUS   PORTS   NAMES
1ae84817cd54   ghcr.io/apollographql/router:v1.13.0   "/dist/router_wrappe…"   20 hours ago   Restarting (1) 10 seconds ago                            gateway-router
7734b35b7411   molecula-backend-patientcard-service   "docker-entrypoint.s…"   20 hours ago   Up 20 hours                     0.0.0.0:3003->3000/tcp   patientcard-service
c9970e456668   molecula-backend-users-service         "docker-entrypoint.s…"   20 hours ago   Up 20 hours                     0.0.0.0:3000->3000/tcp   users-service
c9ee007cbe22   molecula-backend-therapy-service       "docker-entrypoint.s…"   20 hours ago   Up 20 hours                     0.0.0.0:3002->3000/tcp   therapy-service
22c4d96305fe   postgres                               "docker-entrypoint.s…"   20 hours ago   Up 20 hours                     0.0.0.0:5444->5432/tcp   molecula-backend-postgres-1
60621dd7f92b   redis:7-alpine                         "docker-entrypoint.s…"   20 hours ago   Up 20 hours                     0.0.0.0:6333->6379/tcp   molecula-backend-redis-1

So I suppose I should reach the containers from my localhost, but the message says that "localhost didn't send any data".

Also I just realized that I can reach postgreSQL via postico on localhost:5444 without a problem, thus it my applications should be exposed to localhost as well.

Edit Resolved

It seems that commenting volumes in docker-compose.yaml solved this issue. It seems that mounting volumes made the application run inconsistently and produced these access problems I have described earlier. Mounting volumes was supposed to prevent bcrypt being compiled against different OS, but apparently my solution produced more errors than it resolved.


Solution

  • Commenting volumes inside the docker-compose.yaml solved the issue as mounting these volumes was producing inconsistent application.

    Mounting these volumes was supposed to help with bcrypt build as referenced in Install Bcrypt in Docker by Richard Kotze, but in my case produced more bugs than it resolved.