Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to connect to localhost:9092 from docker container using docker-compose and not using docker bridge

Tags:

I am running Kafka server on my local machine on port 9092. I am running a service in docker container using docker-compose which needs to send message to kafka server.

I tried writing my producer in service code using 'localhost' and IP as well but both are not working.

Can anyone help me to fix the issue?

like image 865
Manish Kumar Avatar asked Mar 08 '17 11:03

Manish Kumar


People also ask

How do I connect docker Compose to localhost?

You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

Can docker speak localhost?

Docker containers inside the same bridge network can easily communicate with each other by using their container ID as the host name. However, Docker Container A cannot easily talk to something on localhost on the host machine.

How do I connect to locally hosted MySQL database with docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.


1 Answers

With docker-compose:

Use the network_mode option to allow connection to localhost ports

network_mode: "host"

Without docker-compose:

Use the --net flag to allow connection to localhost ports

docker run -it --net=host

You can also use --network flag

--network="host"

According to the official Docker documentation these "give the container full access to local system services such as D-bus and is therefore considered insecure."

Of course if you containerise your service that is running on localhost:9092 then you could run that in a Docker container too and link your two Docker containers together using the --link flag:

docker run -t -d myService
docker run -t -d --link myService:myService_ref myOtherService
like image 175
GreensterRox Avatar answered Sep 22 '22 05:09

GreensterRox