When I try and service docker through docker compose, it is unable to connect to Consul.
This is my application.properties.
spring.application.name=postservice
server.port=8080
spring.data.mongodb.database=posts
spring.data.mongodb.host=${MONGODB_HOST:MongoDBPosts}
spring.data.mongodb.port=${MONGODB_PORT:27017}
management.security.enabled=false
spring.cloud.consul.host=http://consul
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}-4efb1523-76a3-f476-e6d8-452220593089
# Traefik configuration
spring.cloud.consul.discovery.tags[0]=traefik.enable=true
spring.cloud.consul.discovery.tags[1]=traefik.http.routers.${spring.application.name}.entrypoints=post
spring.cloud.consul.discovery.tags[2]=traefik.http.routers.${spring.application.name}.rule=PathPrefix('/path/')
This is my docker compose
version: "3.1"
networks:
Pro290_network:
external: false
services:
Traefik:
image: traefik:latest
container_name: traefik
command:
- '--api.insecure=true'
- '--providers.consulcatalog=true'
- '--providers.consulcatalog.prefix=traefik'
- '--providers.consulcatalog.endpoint.address=consul:8500'
- '--serversTransport.insecureSkipVerify=true'
- '--entryPoints.web.address=:80'
- '--entryPoints.developer_api.address=:8081'
- '--log.level=DEBUG'
ports:
- 80:80
- 8081:8081
volumes:
- /var/run/docker.sock:/var/run/docker.sock
# - ./traefik.yaml:/etc/traefik/traefik.yaml
networks:
- Pro290_network
restart: always
MongoDBPosts:
container_name: MongoDBPosts
image: mongo:latest
restart: always
hostname: MongoDBPosts
ports:
- "27017:27017"
networks:
- Pro290_network
PostService:
container_name: postservice
build: ./postservice
image: postservice:latest
networks:
- Pro290_network
ports:
- "6001:8080"
depends_on:
- MongoDBPosts
- Consul
# labels:
# - "traefik.enable=true"
# - "traefik.http.routers.postservice.rule=Host(`postservice.Pro290_network.com`)"
# - "traefik.http.services.postservice.loadbalancer.server.port=8080"
# - "traefik.http.routers.postservice.rule=PathPrefix(`/post/`)"
Consul:
image: hashicorp/consul:latest
container_name: consul
command: "agent -dev -client=0.0.0.0"
ports:
- "8500:8500"
networks:
- Pro290_network
labels:
- 'traefik.enable=true'
- 'traefik.consulcatalog.connect=true'
restart: unless-stopped
And this is my main class
package pro290.post_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import okhttp3.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
//import okhttp3.*;
@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class PostServiceApplication {
@GetMapping(path = "/health")
@ResponseStatus(code = HttpStatus.OK)
public void Health() { }
public static void main(String[] args) throws IOException {
SpringApplication.run(PostServiceApplication.class, args);
//registerToConsul();
}
}
When I’ve changed the
spring.cloud.consul.host=http://consul
to
spring.cloud.consul.host=http://localhost
My docker container for my post service shows that there is an error connecting to consul when it’s localhost, but not when it’s consul.