24

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?

Manish Kumar
  • 245
  • 1
  • 3
  • 7
  • 1
    Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](http://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – Lucas dos Santos Abreu Mar 08 '17 at 12:07
  • A different approach to solve this problem - from a Kafka configuration standpoint, not a Docker configuration - is described in this answer: https://stackoverflow.com/a/57063556/302343 – Tim Jul 16 '19 at 18:39

3 Answers3

27

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
GreensterRox
  • 6,432
  • 2
  • 27
  • 30
  • once he is using `docker-compose` I would suggest to use the `external_links` tag from version 3, instead of using a `docker run`, it would make less changes for him, docs: https://docs.docker.com/compose/compose-file/#externallinks – Lucas dos Santos Abreu Mar 08 '17 at 12:05
  • 4
    net=host is the sledgehammer/last resort/not best practices option. – user2105103 Mar 08 '17 at 16:51
  • @GreensterRox , I am using docker-compose. How can i configure and run it using docker-compose up? – Manish Kumar Mar 09 '17 at 05:39
  • @ManishKumar Looking at docker-compose --help I'm not seeing an option to specify the 'net' arg. Can you try connecting to the IP address of your host instead from within the container? – GreensterRox Mar 09 '17 at 09:00
  • @GreensterRox It got resolved by putting network_mode: "host" inside docker-compose.yml – Manish Kumar Mar 20 '17 at 05:53
  • Your answer is also correct when we run docker without using docker-compose. – Manish Kumar Mar 20 '17 at 05:55
  • @ManishKumar - great ! Glad you got it working. I've updated the answer for anyone else who may need help with this. – GreensterRox Mar 20 '17 at 08:07
  • This solution works, but then you can only connect to the container afterwards on Linux https://stackoverflow.com/a/52554681/664054 – WhiteKnight Nov 11 '21 at 16:16
7

You could fix the IP address of your host and pass this to docker via docker-compose using the 'extra_hosts' option:

Something like:

sudo ifconfig lo0 alias 172.16.123.1 

Then in docker compose:

extra_hosts:
 - "service.localhost:172.16.123.1"

Then within the container you should be able to:

ping service.localhost
GreensterRox
  • 6,432
  • 2
  • 27
  • 30
2

Connect from container to local machine. Make IP alias for localhost

sudo ifconfig lo:0 172.18.1.1 up

Add to docker-compose config service lines

extra_hosts:
      - 'local.machine:172.18.1.1'

And after container restart it can be used to connect

'web-socket-server' => 'ws://172.18.1.1',
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hesy Ra
  • 51
  • 4