简体   繁体   English

如何使用 docker-compose 而不是使用 docker bridge 从 docker 容器连接到 localhost:9092

[英]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.

相关问题 如何使用docker-compose将localhost卷挂载到docker容器 - how to mount localhost volume to a docker container using docker-compose 当network_mode:“bridge”(docker-compose)时,无法从localhost连接容器端口 - Unable to connect container port from localhost when network_mode: “bridge” (docker-compose) 如何将docker-compose连接到容器网络和localhost网络 - How to connect docker-compose to container network and localhost network ConnectionException 尝试使用 ZBAEDB53E845AE71F13945FCCZ057 从另一个 docker 容器连接到 docker 中的 mongoDB - ConnectionException trying to connect to mongoDB in docker from another docker container using docker-compose 使用 docker-compose 时无法从本地主机访问 docker 容器端口 - unable to access docker container port from localhost when using docker-compose 如何使用 docker-compose 连接到 mongodb? - How to connect to mongodb using docker-compose? 如何使用docker-compose连接到PostgreSQL? - How to connect to PostgreSQL using docker-compose? 如何在docker-machine中使用docker-compose将文件从docker容器复制到主机 - How to copy files from docker container to host using docker-compose in docker-machine 如何使用 docker-compose 强制时间进入 Docker 容器 - How to force time into Docker container using docker-compose Docker-compose:使用 localhost 和 mysql 作为主机名连接到数据库 - Docker-compose: Connect to database using localhost & mysql as hostname
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM