Cannot reach Spring Boot component through Traefik, using Docker Swarm - spring-boot

I’m trying to run a Spring Boot application consisting of 2 microservices behind a Traefik reversed proxy in Docker Swarm. When using a dual network stack for my Spring Boot webapplication, the application does not respond.
I do have the following networks
NETWORK ID NAME DRIVER SCOPE
c23c6ac30ecd bridge bridge local
0dcb7c122e69 docker_gwbridge bridge local
1e50cdf3eee7 host host local
wbhyv0itkveu ingress overlay swarm
7sxpebq9pp7j marc_default overlay swarm
e953c2393965 none null local
t8u63pf9l3cb traefik-net overlay swarm
And the following configuration to start Traefik
docker service create \
--name traefik \
--constraint=node.role==manager \
--publish 80:80 \
--publish 8080:8080 \
--mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
--network traefik-net \
traefik \
--docker \
--docker.swarmmode \
--docker.domain=traefik \
--docker.watch \
—web
Now, there is a docker-compose.yml file
version: '3'
services:
web:
image: myapp-web
env_file:
- db-params.env
environment:
- server.port=8080
deploy:
labels:
- 'traefik.port=8080'
networks:
- web
- default
be:
image: myapp-be
env_file:
- db-params.env
networks:
- default
networks:
web:
external:
name: traefik-net
And a command to start the composite:
docker stack deploy -c docker-compose.yml marc
In Traefik there is an URL visible: web-marc.traefik, that is defined in /etc/hosts
Unfortunately the is just a time-out when I’n asking”
curl http://marc-web.traefik/
I tried to remove the default network from the web component. It could reach the web component through Traefik, but (of course) it cannot find the be component.
Why don’t I get a reply from Spring Boot?

It's seems to be a bug in Traefik. See https://github.com/containous/traefik/pull/2244

Related

Cannot open Minio in browser after dockerizing it in Spring Boot App

I have a problem in opening minio in the browser. I just created Spring Boot app with the usage of it.
Here is my application.yaml file shown below.
server:
port: 8085
spring:
application:
name: springboot-minio
minio:
endpoint: http://127.0.0.1:9000
port: 9000
accessKey: minioadmin #Login Account
secretKey: minioadmin # Login Password
secure: false
bucket-name: commons # Bucket Name
image-size: 10485760 # Maximum size of picture file
file-size: 1073741824 # Maximum file size
Here is my docker-compose.yaml file shown below.
version: '3.8'
services:
minio:
image: minio/minio:latest
container_name: minio
environment:
MINIO_ROOT_USER: "minioadmin"
MINIO_ROOT_PASSWORD: "minioadmin"
volumes:
- ./data:/data
ports:
- 9000:9000
- 9001:9001
I run it by these commands shown below.
1 ) docker-compose up -d
2 ) docker ps -a
3 ) docker run minio/minio:latest
Here is the result shown below.
C:\Users\host\IdeaProjects\SpringBootMinio>docker run minio/minio:latest
NAME:
minio - High Performance Object Storage
DESCRIPTION:
Build high performance data infrastructure for machine learning, analytics and application data workloads with MinIO
USAGE:
minio [FLAGS] COMMAND [ARGS...]
COMMANDS:
server start object storage server
gateway start object storage gateway
FLAGS:
--certs-dir value, -S value path to certs directory (default: "/root/.minio/certs")
--quiet disable startup information
--anonymous hide sensitive information from logging
--json output server logs and startup information in json format
--help, -h show help
--version, -v print the version
VERSION:
RELEASE.2022-01-08T03-11-54Z
When I write 127.0.0.1:9000 in the browser, I couldn't open the MinIo login page.
How can I fix my issue?
The MinIO documentation includes a MinIO Docker Quickstart Guide that has some recipes for starting the container. The important thing here is that you cannot just docker run minio/minio; it needs a command to run, probably server. This also needs to be translated into your Compose setup.
The first example on that page breaks down like so:
docker run \
-p 9000:9000 -p 9001:9001 \ # publish ports
-e "MINIO_ROOT_USER=..." \ # set environment variables
-e "MINIO_ROOT_PASSWORD=..." \
quay.io/minio/minio \ # image name
server /data --console-address ":9001" # command to run
That final command is important. In your example where you just docker run the image and get a help message, it's because you omitted the command. In the Compose setup you also don't have a command: line; if you look at docker-compose ps I expect you'll see the container is exited, and docker-compose logs minio will probably show the same help message.
You can include that command in your Compose setup with command::
version: '3.8'
services:
minio:
image: minio/minio:latest
environment:
MINIO_ROOT_USER: "..."
MINIO_ROOT_PASSWORD: "..."
volumes:
- ./data:/data
ports:
- 9000:9000
- 9001:9001
command: server /data --console-address :9001 # <-- add this

Communication between 2 dockerized spring boot applications using custom docker network

I have a Hello service and World service. The Hello service has a rest point /hello/hw which internally calls rest end point in World service to return string "Hello World".
The application works fine as expected if the system is not dockerized.
However the issue is when I want to dockerize the system.
I use docker-compose to containerize both services. I have defined a custom network in the docker compose file (named as custom_net).
The rest end point /hello/hw is reachable but Hello service is not able to call the rest end point in the World service.
The application.properties in Hello service has the url of World service as http://localhost:8082/world
I get UnknownHostException when I set the url as http://custom_net:8082/world or to http://custom_net:8092/world
I get Connection refused exception when I don't change the url in application.properties.
The log message states 'failed: Connection refused: localhost/127.0.0.1:8082;'
I am not sure what configurations are needed to bridge the 2 services
The command docker network list shows the name of custom network as 'tempfolder_custom_net'.
The command docker inspect shows that both hello and world service are registered to this network
version: '3'
services:
hello_service:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: hello_service
volumes:
- ./deploy:/root
networks:
- custom_net
depends_on:
- world_service
command: sh -c "java -jar -Dspring.config.location=file:///root/hello/application.yml /root/hello/hello-0.0.1-SNAPSHOT.jar "
ports:
- 8091:8081
world_service:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: world_service
volumes:
- ./deploy:/root
command: sh -c "java -jar -Dspring.config.location=file:///root/world/application.yml /root/world/world-0.0.1-SNAPSHOT.jar "
ports:
- 8092:8082
networks:
- custom_net
networks:
custom_net:
driver: bridge
Application.yml of Hello service ...
server:
port: 8081
services:
world:
url: http://localhost:8082/world
You should build new image base on openjdk:8-jdk-alpine image, and add ENTRYPOINT
You should change this:
server:
port: 8081
services:
world:
url: http://localhost:8082/world
To :
server:
port: 8081
services:
world:
url: http://world_service:8092/world
Make sure, both containers are running.
#Thanh Nuguyen Van thanks for providing useful hints in troubleshooting.
The issue was with the host name world_service. Looks like underscore is not a valid URL. Below is my updated docker compose file. I didn't even have to use custom docker network. I updated the url in application.yml to http://worldservice:8082/world.
Also note that the port is 8082 not 8092. We have to mention the application's port and not the docker exposed port.
version: '3'
services:
helloservice:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: helloservice
volumes:
- ./deploy:/root
#networks:
#- custom_net
depends_on:
- worldservice
command: sh -c "java -Xdebug -jar -Dspring.config.location=file:///root/hello/application.yml /root/hello/hello-0.0.1-SNAPSHOT.jar "
ports:
- 8091:8081
worldservice:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: worldservice
volumes:
- ./deploy:/root
command: sh -c "java -Xdebug -jar -Dspring.config.location=file:///root/world/application.yml /root/world/world-0.0.1-SNAPSHOT.jar "
ports:
- 8092:8082
#networks:
#- custom_net
#networks:
#custom_net:
#driver: bridge

Can't connect to Spring Boot app running on Docker locally

I stuck with the problem that can't open my REST Spring Boot app on localhost:8091 in browser.
Here is my docker-compose.xml (everything is deployed locally on Docker Desktop):
version: '3.3'
services:
postgres:
build:
context: services/postgres
dockerfile: Dockerfile.development
command: postgres
ports:
- "5432:5432"
environment:
- POSTGRESS_USER=postgres
- POSTGRESS_DB=postgres
- POSTGRESS_PASSWORD=qqq
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- "db-data:/var/lib/postgresql/data"
app:
build:
context: services/app
dockerfile: Dockerfile.development
command: java -jar ./app.jar
environment:
- PORT=8091
network_mode: host
image: 'my-java-app'
ports:
- 8091:8091
depends_on:
- postgres
angular:
build:
context: services/angularfrontend
dockerfile: Dockerfile.development
image: 'my-angular-app'
ports:
- 80:80
volumes:
db-data:
Spring Boot App starts normally on 8091 and connects to the database, but then I can't make calls to it's API from my local machine ("connection refused").
Angular app opens normally (on localhost:80), but can't make calls to localhost:8091 Spring Boot app.
The call from angular service container to localhost:8091 fails, right?
Try to override in your angular frontend container the call to the backend
use app:8091 (this is how the backend service is called) instead of localhost:8091.
In the 'angular' container localhost doesn't translate to 'app' container.
You can't get from a container into a different container using localhost.
localhost inside a container will translate to the ip of that container.
Try to make in your angular application the call to the backend configurable, after that override that configuration in docker-compose using environment.
Also do that for the springboot app application.
I don't see in the environment that you override the call to the postgress.
Expose that configuration in application.properties and override in docker-compose, after that remove network_mode: host
If you really want to use network_mode: host, you don't need to specify <source>:<dest> because the app is listening on 8091 directly on the host network:
...
app:
build:
context: services/app
dockerfile: Dockerfile.development
command: java -jar ./app.jar
environment:
- PORT=8091
network_mode: host
image: 'my-java-app'
depends_on:
- postgres
...
If you want to run the java app like the other containers, simply remove this line from the compose file and the network mode will default to bridge:
network_mode: host

Ubuntu cannot reach host when linking docker containers

I have 3 dockerized services. Services A and B run inside same docker-compose file:
docker-compose.yml
version: '3.5'
services:
service_a:
container_name: service_a
networks:
- my_net
service_b:
container_name: service_b
networks:
- my_net
networks: #This is just because I wanted to change the network default name
my_net:
name: my_net
Service C needs make requests against services A and B, but it runs separately using docker without compose (that's because I'm passing --network option). So, I run service C linking A and B:
docker run --network my_net --link service_a --link service_b service_c_docker_image
This is working on MacOS, but not in Ubuntu!
If I run ping command, instead of default service_c_docker_image command:
docker run --network my_net --link service_a --link service_b service_c_docker_image ping service_a
on MacOS, the host is reached properly; on Ubuntu, I get: ping: service_a: Name or service not known. And same with service_b.
Both machines are using same version of docker and docker-compose.
What am I missing?
You may have a typo in your question as that compose file should not run at all, the service level network names my_net should match the top level network name which then can be renamed using name: intra_net. The network set in the docker run command should match what the network was renamed to in the top level networks section (and that network needs to already exist, so run the compose stack first).
working example:
docker-compose.yaml
version: '3.5'
services:
service_a:
image: odise/busybox-curl
command: ["curl", "-s", "service_b:5678"]
depends_on:
- service_b
networks:
- my_net
service_b:
image: hashicorp/http-echo
command: ["-text", "hello world"]
networks:
- my_net
networks:
my_net:
name: infra_network
Run the services docker-compose up -d and check the logs:
> docker-compose logs
Attaching to docker-compose-networks_service_a_1, docker-compose-networks_service_b_1
service_b_1 | 2019/01/06 05:53:55 Server is listening on :5678
service_b_1 | 2019/01/06 05:53:55 service_b:5678 172.19.0.3:46900 "GET / HTTP/1.1" 200 12 "curl/7.39.0" 106.6µs
service_a_1 | hello world
Then start the other container with docker
> docker run --network infra_network odise/busybox-curl curl -s service_b:5678
hello world
Silly me. Actually, my configuration is right but services A and B were not run because an application level error, so links were not working.

Elasticsearch in Docker container cluster

I want to run 2 instances of Elasticsearch on 2 different hosts.
I have built my own Docker image based on Ubuntu 14.04 and the 1.3.2 version of Elasticsearch. If I run 2 ES containers on 1 host, each instance can see and communicate with the other; but when I run 2 instances of ES on 2 different hosts, it didn't work. The 9300 port of the container is bind to the 9300 host's port.
Is it possible to create an ES cluster with my configuration?
I was able to get clustering working using unicast across two docker hosts. I just happen to be using the ehazlett/elasticsearch image, but I do not think this should matter all that much. The really important bit seems to be setting the network.publish_host setting to a public or routable IP its docker host.
Configuration
docker-host-01
eth0: 192.168.1.10
Docker version 1.4.1, build 5bc2ff8/1.4.1
docker-host-02
eth0: 192.168.1.20
Docker version 1.4.1, build 5bc2ff8/1.4.1
Building the Cluster
On Docker Host 01
docker run -d \
-p 9200:9200 \
-p 9300:9300 \
ehazlett/elasticsearch \
--cluster.name=unicast \
--network.publish_host=192.168.1.10 \
--discovery.zen.ping.multicast.enabled=false \
--discovery.zen.ping.unicast.hosts=192.168.1.20 \
--discovery.zen.ping.timeout=3s \
--discovery.zen.minimum_master_nodes=1
On Docker Host 02
docker run -d \
-p 9200:9200 \
-p 9300:9300 \
ehazlett/elasticsearch \
--cluster.name=unicast \
--network.publish_host=192.168.1.20 \
--discovery.zen.ping.multicast.enabled=false \
--discovery.zen.ping.unicast.hosts=192.168.1.10 \
--discovery.zen.ping.timeout=3s \
--discovery.zen.minimum_master_nodes=1
Using docker-compose is much easier than running it manually in command line:
elasticsearch_master:
image: elasticsearch:latest
command: "elasticsearch -Des.cluster.name=workagram -Des.node.master=true -Des.node.data=false"
environment:
- ES_HEAP_SIZE=512m
ports:
- "9200:9200"
- "9300:9300"
elasticsearch1:
image: elasticsearch:latest
command: "elasticsearch -Des.cluster.name=workagram -Des.discovery.zen.ping.unicast.hosts=elasticsearch_master"
links:
- elasticsearch_master
volumes:
- "/opt/elasticsearch/data"
environment:
- ES_HEAP_SIZE=512m
elasticsearch2:
image: elasticsearch:latest
command: "elasticsearch -Des.cluster.name=workagram -Des.discovery.zen.ping.unicast.hosts=elasticsearch_master"
links:
- elasticsearch_master
volumes:
- "/opt/elasticsearch/data"
environment:
- ES_HEAP_SIZE=512m
You should be able to communicate the two containers running in different hosts as far as the host machines are accessible between them in the ports needed. I think your problem is that you are trying to use ElasticSearch multicast discovery, but if then you need to expose also port 54328 of the containers. If it doesn't work you can also try to configure ElasticSearch using unicast, setting adequately the machines IP's in your elasticsearch.yml.

Resources