Reputation: 778
I have a node application , a next.js app for front end ,Redis and Postgres as databases . I have dockerized Next.js and node.js in different containers . Docker-compose.yaml is as follows
version: '3'
services:
redis-server:
image: 'redis'
restart: always
postgres-server:
image: 'postgres:latest'
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
ports:
- "5433:5432"
volumes:
- ./docker/postgres/data/data/pg_hba.conf:/var/lib/postgresql/app_data/pg_hba.conf
- ./src/db/sql/CREATE_TABLES.sql:/docker-entrypoint-initdb.d/CREATE_TABLES.sql
- ./src/db/sql/INSERT_TO_TABLES.sql:/docker-entrypoint-initdb.d/INSERT_TO_TABLES.sql
- ./src/db/sql/CREATE_FUNCTIONS.sql:/docker-entrypoint-initdb.d/CREATE_FUNCTIONS.sql
node-app:
build: .
ports:
- "4200:4200"
client:
build:
context: ./client
dockerfile: Dockerfile
container_name: client
restart: always
volumes:
- ./:/app
- /app/node_modules
- /app/.next
ports:
- 3000:3000
When using SSR , I cannot make a request to localhost:4200 .Now, I get that it is because they are in different containers and if the request is not from client side , then the client container is being checked for server at port 4200. Now , I am not sure how to simply refer to the container using the container name or something to make an API request for the SSR data (like fetch('node-app/users')
)
Upvotes: 9
Views: 10899
Reputation: 12928
docker-compose sets up a network for all the services in a compose file. To reach another container in the network, you use the name of that container as a hostname, and it will resolve to the right ip.
So in your case doing fetch('http://node-app:4200/users')
should do the trick.
Upvotes: 21