Answer the question
In order to leave comments, you need to log in
How to forward ports in docker-compose?
I'm new to devOps, if I ask a question incorrectly, please don't swear too much.
The doker-compose file has several microservices, postgres and kafka. Problem : Microservices do not see each other and kafka. Only the server service on port 8080 and postgres on port 5432 stick out. How to forward ports and what URL should be specified in @FeignClient in the client class?
docker-compose:
version: '3.8'
volumes:
pg_market:
services:
server:
image: 'server:latest'
build:
context: .
container_name: server
depends_on:
- pg_db
ports:
- "8080:8080"
links:
- pg_db:database
recommendation:
image: 'recommendation:latest'
build:
context: .
container_name: recommendation
depends_on:
- pg_db
ports:
- "8082:8082"
links:
- pg_db:database
pg_db:
image: postgres
environment:
- POSTGRES_PASSWORD=1
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
volumes:
- pg_market:/var/lib/postgresql/data
ports:
- "5432:5432"
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_CREATE_TOPICS: recommendation
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
server:
port: 8082
spring:
datasource:
url: jdbc:postgresql://database/postgres
driver-class-name: org.postgresql.Driver
username: postgres
password: 1
kafka:
bootstrap-servers: localhost:9092
client-id: local
auto-commit-interval: 1000
topic: recomendation
@Component
@FeignClient(name = "market", url = "localhost:8082/api/v1/")
@EnableDiscoveryClient
public interface Client {
}
Answer the question
In order to leave comments, you need to log in
All I had to do was add "network_mode: host" to docker-compose. After the file took the following form, everything worked:
version: '3.8'
volumes:
pg_market:
services:
server:
image: 'server:latest'
build:
context: .
container_name: server
depends_on:
- pg_db
ports:
- "8080:8080"
network_mode: host
links:
- pg_db:database
recommendation:
image: 'recommendation:latest'
build:
context: .
container_name: recommendation
depends_on:
- pg_db
ports:
- "8082:8082"
network_mode: host
links:
- pg_db:database
pg_db:
image: postgres
environment:
- POSTGRES_PASSWORD=1
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
volumes:
- pg_market:/var/lib/postgresql/data
ports:
- "5432:5432"
network_mode: host
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
network_mode: host
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_CREATE_TOPICS: recommendation
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
Since docker-compoe is used, services will automatically connect to the virtual network and will be able to access each other by the name of the service (if there is no need to access the service from the host machine or from outside it, then ports can not be forwarded).
For example, to connect to kafka in the config, you must specify:
kafka:
bootstrap-servers: kafka:9092
client-id: local
auto-commit-interval: 1000
topic: recomendation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question