0

I have a server which is a home network running postgres server on port 5432 on localhost...now I have wrote a app(runs on 3001) which i want to containarized and expose a port on 3001 to receive the incoming request. Then app inside the container will request outside(outgoing traiffic from the container to home network) the for the postgres server on port 5432 which is on home network

My docker-compose file

version: '3.4'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: company
    container_name: company
    restart: always
    command: bash -c "npm run start:dev"
    ports:
      - 3001:3001 <-- this doesn't take care of the incoming traffic to container
    network_mode: 'host' <-- this only resolves the postgres connection from container to home network
    volumes:
      - ./:/app

although using other stackoverflow solutions, I added the network_mode: 'host' which takes care of the traffic(tcp) from the app inside container to the home network postgres 5432....but the incoming traffic to the container doesn't connect.

My Dockerfile

FROM node:16.13.0 AS builder
WORKDIR /app
COPY ./ ./

EXPOSE 3001
RUN npm install
RUN npm run build

From this solution link I downgraded the docker-compose...but it doesn't solve the incoming traffic to container..

My docker-compose version is 1.27.4 My Docker version is 20.10.8

dancheg
  • 549
  • 1
  • 4
  • 14
Riyad Zaigirdar
  • 691
  • 3
  • 8
  • 22
  • It is not quite clear, what _exactly_ the issue is you're trying to solve here. What do you mean by _incoming traffic to the container doesn't connect_? Is there any error message? Are you sure, your app is listening on port 3001 and accepting connections? In any case, downgrading docker-compose will not solve your issue, as it was already said in the linked forum post; instead use only _either_ `ports` or `network_mode: host`. – acran Dec 06 '21 at 10:56

1 Answers1

-1

If i understand that correctly, you need to put 2 different docker images to your compose file, and start 2 different docker services with those images. That might look somewhat like that:

version: '3.4'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: company
    container_name: company
    restart: always
    command: bash -c "npm run start:dev"
    ports:
      - 3001:3001
    network_mode: 'host'
    volumes:
      - ./:/app
  postgres_service:
    image: postgres_image:latest
    ports:
      - 5432:5432
dancheg
  • 549
  • 1
  • 4
  • 14
  • I clearly said that the postgres is installed in the servers main home network...as the container created creates its own network....so the app inside the network has to find the port outside the containers network which is servers main home network – Riyad Zaigirdar Dec 06 '21 at 10:54