[英]how to connect to localhost:9092 from docker container using docker-compose and not using docker bridge
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.我在本地机器上的 9092 端口上运行 Kafka 服务器。我正在使用 docker -compose在 docker容器中运行一个服务,该服务需要向 kafka 服务器发送消息。
I tried writing my producer in service code using 'localhost' and IP as well but both are not working.我也尝试使用“本地主机”和 IP 在服务代码中编写我的生产者,但两者都不起作用。
Can anyone help me to fix the issue?谁能帮我解决这个问题?
With docker-compose:使用 docker-compose:
Use the network_mode option to allow connection to localhost ports使用 network_mode 选项允许连接到本地主机端口
network_mode: "host"
Without docker-compose:没有 docker-compose:
Use the --net flag to allow connection to localhost ports使用 --net 标志允许连接到本地主机端口
docker run -it --net=host
You can also use --network flag您还可以使用 --network 标志
--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."根据官方 Docker 文档,这些“使容器可以完全访问本地系统服务,例如 D-bus,因此被认为是不安全的。”
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:当然,如果您将在 localhost:9092 上运行的服务容器化,那么您也可以在 Docker 容器中运行它,并使用 --link 标志将两个 Docker 容器链接在一起:
docker run -t -d myService
docker run -t -d --link myService:myService_ref myOtherService
You could fix the IP address of your host and pass this to docker via docker-compose using the 'extra_hosts' option:您可以修复主机的 IP 地址,并使用“extra_hosts”选项通过 docker-compose 将其传递给 docker:
Something like:就像是:
sudo ifconfig lo0 alias 172.16.123.1
Then in docker compose:然后在 docker compose 中:
extra_hosts:
- "service.localhost:172.16.123.1"
Then within the container you should be able to:然后在容器内,您应该能够:
ping service.localhost
Connect from container to local machine.从容器连接到本地机器。 Make IP alias for localhost为 localhost 创建 IP 别名
sudo ifconfig lo:0 172.18.1.1 up
Add to docker-compose config service lines添加到 docker-compose 配置服务行
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',
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.