Redis Sentinel on Docker Gives Docker Network IP Addresses - docker

My objective is to set up 3x Redis Server and 3x Redis Sentinel on a single Docker VM using Docker compose and expose each of the Redis Servers and Sentinels to the local network.
The static IP of my Docker host is 192.168.2.90.
I've given the Redis Servers ports numbered 6379, 6380, 6381 and exposed those ports through Docker.
My local network is 192.168.2.0/24
My Docker machine's internal network is 172.16.0.0/12.
Everything works great within the Docker containers themselves. The problem comes when I try to connect to Redis using a different machine on my local network.
My python test script successfully connects to Redis Sentinel. The problem is when it discovers the master and slaves, the addresses are all on the 172.16.0.0/12 subnet.
Redis master:
('172.18.3.1', 6379)
Redis slaves:
[('172.18.3.3', 6381), ('192.168.2.90', 6379),('172.18.3.2', 6380)]
When I telnet into the master and run INFO, likewise it gives me the 172.16.0.0/12 addresses.
role:master
connected_slaves:2
slave0:ip=172.18.3.3,port=6381,state=online,offset=252692195,lag=0
slave1:ip=172.18.3.2,port=6380,state=online,offset=252692195,lag=0
I cannot figure out how to get Redis Server and Redis Sentinel to report the 192.168.2.0/24 subnet.
I've defined my Redis Server containers as follows:
redis-a-1:
container_name: redis-a-1
hostname: redis-a-1
image: redis
command: "redis-server --port 6379 --bind-source-addr 192.168.2.90"
environment:
- REDIS_HOST=192.168.2.90
ports:
- "6379:6379"
restart: unless-stopped
networks:
blue-green-network:
ipv4_address: 172.18.3.1
redis-a-2:
container_name: redis-a-2
hostname: redis-a-2
image: redis
command: "redis-server --port 6380 --bind-source-addr 192.168.2.90 --slaveof 192.168.2.90 6379"
environment:
- REDIS_HOST=192.168.2.90
ports:
- "6380:6380"
restart: unless-stopped
networks:
blue-green-network:
ipv4_address: 172.18.3.2
redis-a-3:
container_name: redis-a-3
hostname: redis-a-3
image: redis
command: "redis-server --port 6381 --bind-source-addr 192.168.2.90 --slaveof 192.168.2.90 6379"
environment:
- REDIS_HOST=192.168.2.90
ports:
- "6381:6381"
restart: unless-stopped
networks:
blue-green-network:
ipv4_address: 172.18.3.3
I've tried a bunch of things to get each of the Redis Servers to report their IP addresses to each other as the Docker host's IP address but with no success.
Any thoughts on how I can do this or am I on a fool's erand?
Many thanks.
Note: I am aware that the machine itself is a single point of failure.

Related

docker host: use docker dns to resolve container name from host network

I need to resolve a container name to the IP Address from the docker host.
The reason for this is, i need a container to run on the host network, but it must be also able to resolve the container "backend" which it connects also to. (The container must be send & receive multicast packets)
docker-compose.yml
version: "3"
services:
database:
image: mongo
container_name: database
hostname: database
ports:
- "27017:27017"
backend:
image: "project/backend:latest"
container_name: backend
hostname: backend
environment:
- NODE_ENV=production
- DATABASE_HOST=database
- UUID=5025f846-7587-11ed-9ca7-8b992b5e7dd3
ports:
- "8080:8080"
depends_on:
- database
tty: true
frontend:
image: "project/frontend:latest"
container_name: frontend
hostname: frontend
ports:
- "80:80"
- "443:443"
depends_on:
- backend
environment:
- BACKEND_HOST=backend
connector:
image: "project/connector:latest"
container_name: connector
hostname: connector
ports:
- "1900:1900/udp"
#expose:
# - "1900/udp"
environment:
- NODE_ENV=production
- BACKEND_HOST=backend
- STARTUP_DELAY=1500
depends_on:
- backend
network_mode: host
tty: true
How can i resolve the hostname "backend" via docker from the docker host?
dig backend #127.0.0.11 & dig backend #172.17.0.1 did not work.
A test with a docker ubuntu image & socat proves, that i can receive ssdp multicast packets:
docker run --net host -it --rm ubuntu
socat UDP4-RECVFROM:1900,ip-add-membership=239.255.255.250:0.0.0.0,fork -
The only problem i now have is the DNS/Container name resolution from the host (network).
TL;DR
The container "connector" must be on the host network,but also be able to resolve the container name "backend" to the docker internal IP Address.
NOTE: Perhaps this is better suited on superuser or similar?

connect docker container to local site

I have services openhab and mosquitto.
I have internal network between openhab and mosquitto, it is ok
I have in local network 3 computers 192.168.1.16, 192.168.1.17, 192.168.1.18
on 192.168.1.16 run docker and mosquitto container
Now I need add for mosquitto container new ip 192.168.1.20, because I need send data from others computers in network to mosquitto
How can I do it?
my docker-compose file
version: '3.7'
services:
openhab:
image: "openhab/openhab:3.3.0"
container_name: "openhab"
restart: always
networks:
openhabnet:
aliases:
- openhab
ports:
- 8082:8080
- 8444:8443
volumes:
- "/etc/localtime:/etc/localtime:ro"
- "/etc/timezone:/etc/timezone:ro"
- "./openhab_addons:/openhab/addons"
- "./openhab_conf:/openhab/conf"
- "./openhab_userdata:/openhab/userdata"
environment:
CRYPTO_POLICY: "unlimited"
EXTRA_JAVA_OPTS: "-Duser.timezone=Europe/Berlin"
OPENHAB_HTTP_PORT: "8080"
OPENHAB_HTTPS_PORT: "8443"
USER_ID: "1000"
GROUP_ID: "1000"
mosquitto:
image: "eclipse-mosquitto:latest"
container_name: "mosquitto"
user: "1000:1000"
restart: always
networks:
openhabnet:
aliases:
- mosquitto
ports:
- 1884:1883
- 9001:9001
volumes:
- "./mosquitto/config:/mosquitto/config"
- "./mosquitto/log:/mosquitto/log"
- "./mosquitto/data:/mosquitto/data"
environment:
- TZ=Europe/Bratislava
networks:
openhabnet:
driver: bridge
Your mosquito container is already reacheable on the hosts network with the ip of the docker host, 192.168.1.16 and on the ports you forwarded:
ports:
- 1884:1883
- 9001:9001
So on 192.168.1.16:1884 you can reach the mosquito containers 1883 port and 192.168.1.16:9001 you can reach the mosquito container 9001 port from your other computers too, given you allowed these on the firewalls on the computers, including the docker host.
But if you really want an IP for the mosquito container itself on your host network then you will need to do macvlan: https://docs.docker.com/network/macvlan/
With this your container will get a virtual NIC and will connect to the physical network the docker host is running on. But I think you won't need this, please further explain your use case.

Can't connect to Docker Container on Windows

I have a docker-compose file that looks like this
version: "3"
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: michael
POSTGRES_PASSWORD: pass123
admin:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
ports:
- "5050:5050"
I run docker-compose up -d and I can see my apps running from Docker Desktop. I cannot however connect to my pgadmin instance at port 5050 using localhost. Any ideas?
Docker container of pgAdmin by default runs on port 80 as per the documentation here https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html
You are exposing port 5050 through the mapping. Either add a environment variable PGADMIN_LISTEN_PORT to the docker_compose to make pgAdmin run on port 5050
OR
change port mapping to 5050:80 for the pgAdmin service
Check the docker inspect or docker ps results to ensure that you have your port exposed correctly
Try to connect to it using the public IP

Cannot ping docker container created with docker-compose

I want to create a PostgreSQL cluster composed by a master and two slaves within three containers. I want to do that with docker-compose. Everything works fine but I cannot ping containers from my Mac.
Here the code of my docker-compose.yml.
On Stackoverflow there is this thread How could I ping my docker container from my host that address docker standalone and not docker-compose.
version: '3.6'
volumes:
pgmaster_volume:
pgslave1_volume:
pgslave2_volume:
services:
pgmaster:
container_name: pgmaster
build:
context: ../src
dockerfile: Dockerfile
image: docker-postgresql:latest
environment:
NODE_NAME: pgmaster # Node name
ports:
- 5422:5432
volumes:
- pgmaster_volume:/home/postgres/data
networks:
cluster:
ipv4_address: 10.0.2.31
aliases:
- pgmaster.domain.com
pgslave1:
container_name: pgslave1
build:
context: ../src
dockerfile: Dockerfile
image: docker-postgresql:latest
environment:
NODE_NAME: pgslave1 # Node name
ports:
- 5441:5432
volumes:
- pgslave1_volume:/home/postgres/data
networks:
cluster:
ipv4_address: 10.0.2.32
aliases:
- pgslave1.domain.com
pgslave2:
container_name: pgslave2
build:
context: ../src
dockerfile: Dockerfile
image: docker-postgresql:latest
environment:
NODE_NAME: pgslave2 # Node name
ports:
- 5442:5432
volumes:
- pgslave2_volume:/home/postgres/data
networks:
cluster:
ipv4_address: 10.0.2.33
aliases:
- pgslave2.domain.com
networks:
cluster:
driver: bridge
ipam:
config:
- subnet: 10.0.2.1/24
On my Mac, I have a 192.168.0.0 local network. I expect that doing ping 10.0.2.31 I can ping my container but this is not possible. I think this is due to Linux VM created inside Mac where containers live and the IPs are not reachable outside this VM.
Can someone help me to understand how to make the above three IP reachable? IPs are reachable from one container to another.
Here my full code:
https://github.com/sasadangelo/docker-postgres
you should be able to ping your containers from you host.
via public ip:
just use their public ip. (you had been trying to ping your
container local ip, inside the docker network)
how to find the container public IP?
you can get it by running ifconfig inside the container.
or
or by running on your host docker container inspect <container_id>.
it should be there under NetworkSettings.<network_name>.IPAddress )
via container name/id
docker is running some sort of dns on your machine so you can also use
the container name or id - ping <container_name/id>
note
the way to access your containers outside the docker network is via their published ports. you have bound port 5432 on the docker network to port 5442 on your host, therefore the container should listen and accept traffic at 127.0.0.1:5442 (thats your localhost at the port you've bound)

Redis connection refused between Vagrant and Docker

I have a docker like this:
version: '3.5'
services:
RedisServerA:
container_name: RedisServerA
image: redis:3.2.11
command: "redis-server --port 26379"
volumes:
- ../docker/redis/RedisServerA:/data
ports:
- 26379:26379
expose:
- 26379
RedisServerB:
container_name: RedisServerB
image: redis:3.2.11
command: "redis-server --port 6379"
volumes:
- ../docker/redis/RedisServerB:/data
ports:
- 6379:6379
expose:
- 6379
Now I do a vagrant ssh and do
ping RedisServerA
ping RedisServerB
They both work.
Now I try to connect to the redis server:
redis-cli -h RedisServerB
Works fine
Then I try to connect to the other
redis-cli -h RedisServerA -p 26739
It says:
Could not connect to Redis at RedisServerA:26739: Connection refused
Could not connect to Redis at RedisServerA:26739: Connection refused
Twice.
What am I missing here?
Typically in this setup you'd let each container run on its "natural" port. For connections from outside Docker you need the ports: mapping, and you'd access a container via its published port on the host's IP address. For connections between Docker containers (assuming they're on the same network, and if you used bare docker run, you manually created that network), you use the container name and the container's internal port number.
We can clean up the docker-compose.yml file by removing some unnecessary lines (container_name: and expose: don't really have a practical effect) and letting the image run its default command: on the default port, and only remapping with ports:. We'd get:
version: '3.5'
services:
RedisServerA:
image: redis:3.2.11
volumes:
- ../docker/redis/RedisServerA:/data
ports:
- 26379:6379
RedisServerB:
image: redis:3.2.11
volumes:
- ../docker/redis/RedisServerB:/data
ports:
- 6379:6379
Between containers, you'd use the default port
redis-cli -h RedisServerA
redis-cli -h RedisServerB
From outside Docker you'd use the server's host name and the published ports
redis-cli -h server.example.com -p 23679
redis-cli -h server.example.com

Resources