Consul share network from docker - spring-boot

There is consul server in docker.
vote-consul-server:
image: consul:1.7.2
environment:
CONSUL_BIND_INTERFACE: eth0
ports:
- "${CONSUL_PORT}:8500"
It generates hosts for registred applications.
The main problem is that anothers applications can't communicate between each other if they are not in docker.
For example: I have config-service (docker with consul), user-service(IDE).
When user-service starts, it asks for configurations from config-server, but consul gives not correct link (available only in docker network).
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://83a6c7ab12d0:8888/
How to publish all links from consul(docker)?

Since every container has own IP and may have own network, you can try to use host networking:
docker run --rm -d --network host --name vote-consul-server
For more details, see host networking tutorial.
Also, you can learn about networking from the container’s point of view

First of all - need to define IP address configuration in bootstrap.yaml
spring:
...
cloud:
consul:
host: ${CONSUL_HOST}
port: ${CONSUL_PORT}
discovery:
ip-address: ${CONSUL_HOST}
prefer-ip-address: true
The main properties are: ip-address and prefer-ip-address.
If all aplications work in one network - problem doesn't exist.
For example: all services work in docker.

Related

Unable to connect from Spring Boot to Dockerized Redis in outside/inside machine

I am connecting to Redis from the spring boot app on the outside machine where the Redis server docker container is not running. When the app tries to connect to Redis, the app can't connect properly until the sent request is timed out. Meanwhile, if I try to connect from:
Inside the machine where the Redis server docker container is running with the host is localhost, I could connect it. And, I don't know why I can't connect by setup host value as a numerical IP/alphabetical (URL), only works with "localhost."
Outside machine where the Redis server docker container is not running with Redis client app GUI for management, I could connect it.
application.properties:
spring.redis.host=pc-1
spring.redis.port=6379
pc-1 is alias from some numerical ip. I'am using hosts feature from
windows to aliasing/redirecting it.
.env:
REDIS_PORT=6379
docker-compose.yml:
redis:
image: redis:latest
ports:
- "${REDIS_PORT}:6379"
command:
# - redis-server
# - --requirepass "${REDIS_PASSWORD}"
networks:
- redis
healthcheck:
test: ["CMD-SHELL", "redis-cli ping"]
interval: 10s
timeout: 10s
retries: 3
I need help on this issue.
Use the --service-ports flag to the docker compose command to publish the ports you've defined in the docker compose file.
Other debugging tips:
Hardcode the ${REDIS_PORT} variable in case the value is not getting set or set a default like ${REDIS_PORT:-default}
Pass the env file explicitly like docker compose --env-file ./somedir/.env up in case the env file is not being pick up
Use docker inspect to get container status, check the networking info

How to network 2 separate docker containers to communicate with eachother?

I'm pretty new to docker, and I've tried searching about networking but haven't found a solution that's worked.
I have a Laravel app that is using Laradock.
I also have an external 3rd party API that runs in its own docker container.
I basically want to specify the container name of the api inside my laravel .env file, and have it dynamically resolve the container ip so I can make API calls from my Laravel app. I can already do this with services that are already part of laradock like mariadb/mysql, but since my API is located in an external container, it can't connect to it.
I tried making a network and attaching them with;
docker network create my-network
Then inside my docker-compose.yml files for each of the containers, I specified;
networks:
my-network:
name: "my-network"
But if I try and ping them with;
docker exec -ti laradock-workspace-1 ping my-api
I can't connect and can't really figure out why. Was hoping someone familiar with docker might be able to explain why since I'm sure it's something very obvious I'm missing. Thanks!
By default Docker Compose uses a bridge network to provision inter-container communication. Read this article for more info about inter-container networking.
What matters for you, is that by default Docker Compose creates a hostname that equals the service name in the docker-compose.yml file.
Consider the following docker-compose.yml:
version: '3.9'
services:
server:
image: node:16.9.0
container_name: server
tty: true
stdin_open: true
depends_on:
- mongo
command: bash
mongo:
image: mongo
environment:
MONGO_INITDB_DATABASE: my-database
When you run docker-compose up, Docker will create a default network and assigns the service name as hostname for both mongo and server.
You can now access the backend container via:
docker exec -it server bash
And now you can ping the mongo container using Dockers internal network (default on port 27017 in this case):
curl -v http://mongo:27017/my-database
That's it. The same applies for your setup.

How to correctly config docker-compose to connect to localhost (non-docker service) [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
I'm new to Docker. I'm build a Spring Boot Application deploying on Docker.
Here is my example docker-compose.yml:
version: "3"
services:
user:
container_name: user
image: user-service
build:
context: user-api/
ports:
- "3001:8000"
restart: always
volumes:
- /home/ubuntu/logs/user_service:/opt/app/logs
networks:
- api_network
cms:
container_name: cms
image:cms-service
build:
context: cms-service/
ports:
- "3003:8000"
restart: always
volumes:
- /home/ubuntu/logs/cms_service:/opt/app/logs
networks:
- api_network
networks:
api_network:
driver: bridge
In the server machine, there's a Redis Server running on Ubuntu. I cannot connect the the Redis Server from Docker container to the host machine.
Here is my redis config inside application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=Password123!##
I also tried to change the localhost to
127.0.0.1
172.17.0.1
0.0.0.0
host
host.docker.internal
That's I've found on the internet. But nothing works. Do I need to specifically config anything to allow my Spring Boot Service inside Docker connect to Redis that's running on localhost.
The issue is probably due to the fact your Redis is bound to the address 127.0.0.1 (which is the default configuration) and your containers are not running on the network host.
To solve this, you should reconfigure Redis to bind to both 127.0.0.1 as well as to the IP address of the host as seen from api_network (sudo ip addr show on the host): the easiest thing to do here, if your scenario allows that, is to just bind Redis to 0.0.0.0 (via redis.conf).
As an alternative, you may also want to run your containers on the host network instead of using the api_network bridge: this appears to be overkill according to your issue, by the way, and may lead to security issues and exposed network ports.

How to connect my spring boot app to redis container on docker?

I have created a spring app and i want to connect it to redis server which is deployed on docker-compose i put the needed properties as follow :
spring.redis.host=redis
spring.redis.port=6379
But i keep getting a ConnexionException so how can i Know on which host redis is running and how to connect to it.
Here is my docker-compose file :
version: '2'
services:
redis:
image: 'bitnami/redis:5.0'
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
ports:
- '6379:6379'
volumes:
- 'redis_data:/bitnami/redis/data'
volumes:
redis_data:
driver: local
From docker compose documentation
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name
If you want to access redis by container name ('redis' in this case), the Spring boot application has also be deployed as a docker compose service, but it doesn't appear in the docker-compose file that you've provided in the question, so please add it.
Alternatively If you're trying to run the spring boot application on host machine, use 'localhost' instead of 'redis' in order to access the redis container.
Another approach you can use is "docker network" , Below are the steps to follow :
Create a docker network for redis
docker network create redis-docker
Spin up redis container is redis-docker network.
docker run -d --net redis-docker --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Inspect the redis-docker container
docker network inspect redis-docker
Copy the "IPv4Address" IP and paster in application.yml
Now build , start your application.

Netflix Eureka Server and Client instance registration issue with Docker swarm

We are trying to deploy Netflix Eureka Server on Docker container which is a Spring Boot application. We are deploying it as a docker service with Docker swarm for container orchestration.
Everything works fine when we directly create the container and run the application.
Eureka server registers all the client application with correct IP addresses.
But when we create and deploy it as docker service it registers the application with the wrong IP address.
We have tried below solution as per spring documentation, updated property values in our configuration files.
spring:
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*
eureka:
instance:
preferIpAddress: true
Tried these solutions as well:
https://github.com/spring-cloud/spring-cloud-netflix/issues/1820
https://github.com/spring-cloud/spring-cloud-netflix/issues/2084
While we run the docker service, the container gets assigned one IP address lets say 172.16.1.3 and service started inside docker gets assigned new IP address 172.16.1.4, because of this issue client application registers its self with container's IP into Eureka server. But it is accessible with 172.16.1.4.
So why running docker as service assigns two IP addresses?
Here Eureka server is running on 10.255.53.172 IP address but as we can see in the second image it shows different IP in Instance Info
Eureka instance is registered with containers IP but it's accessible with its service IP in the network
We use this configuration on production using docker swarm mode:
Eureka server configuration
# Defines the Eureka server that is used by the Netflix OSS components to use as the registry
# for server discovery
eureka:
instance:
hostname: ${HOST_NAME:localhost}
appname: eureka-cluster
# enable to register multiple app instances with a random server port
instance-id: ${spring.application.name}:${random.uuid}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 20
Eureka client configuration
eureka:
client:
registerWithEureka: ${REGISTER_WITH_EUREKA:true}
fetchRegistry: ${FETCH_REGISTRY:false}
serviceUrl:
defaultZone: ${EUREKA_DEFAULT_ZONE:http://localhost:8761/eureka/}
instance:
hostname: ${HOST_NAME:${spring.application.name}} # work on swarm
preferIpAddress: ${PREFER_IP_ADDRESS:false}
# enable to register multiple app instances with a random server port
instance-id: ${spring.application.name}:${random.uuid}
leaseRenewalIntervalInSeconds: ${LEASE_RENEWAL_INTERVAl_IN_SECONDS:10}
leaseExpirationDurationInSeconds: ${LEASE_EXPIRATION_DURATIONIN_SECONDS:20}
ON swarm service definition
rd-service:
image: my-eureka-server-microservice-image
ports:
- "8763:8763"
networks:
- backend
client-service:
image: my-eureka-client-microservice-image
networks:
- backend
networks:
environment:
- "EUREKA_DEFAULT_ZONE=http://rd-service:8763/eureka"
- "REGISTER_WITH_EUREKA=true"
- "FETCH_REGISTRY=true"
networks:
backend:
external: true
Important: Services must be in the same docker overlay network used on eureka server docker service.

Resources