简体   繁体   English

Docker:如何从Docker容器中卷曲在主机上运行的本地服务器xyz.example.com

[英]Docker: How to curl local server xyz.example.com running on host machine from within a docker container

On my host machine, I have a server xyz.example.com running on nginx. 在主机上,我在nginx上运行服务器xyz.example.com It is not dockerized and not accessible to the outside world, it is simply used for local dev environment. 它没有经过泊坞窗化,外界也无法访问,仅用于本地开发环境。

I would like to be able to access it from within a docker container. 我希望能够从Docker容器中访问它。

For example, I'd like to be able to do the following from within my docker container: 例如,我希望能够在Docker容器中执行以下操作:

curl xyz.example.com

and to see the same output I see when I run the command on my host machine. 并在主机上运行命令时看到相同的输出。

Also, I am using docker-compose. 另外,我正在使用docker-compose。

I guess your container doesn't know about your xyz.example.com because It is declared in your host's /etc/host (you said it is a private server)? 我猜您的容器不知道您的xyz.example.com因为它是在主机的/etc/host (您说它是私有服务器)?

Then you could add the host as extra_host : 然后,您可以将主机添加为extra_host

services:
  some-service:
    extra_hosts:
      - "xyz.example.com:HOST_BRIDGE_IP"

The extra_host will add hostname mappings in container's /etc/host . extra_host将在容器的/etc/host添加主机名映射。

And run your docker-compose.yml replacing HOST_BRIDGE_IP with it's ip in docker bridge: 并在docker-compose.yml HOST_BRIDGE_IP使用ip来运行docker-compose.yml替换HOST_BRIDGE_IP

HOST_BRIDGE_IP=$(docker network inspect bridge \
  --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}') \
&& sed -e "s/HOST_BRIDGE_IP/${HOST_BRIDGE_IP}/g" \
docker-compose.yml | docker-compose --file - up

This would allow virtual host resolution based on DNS in your server. 这将允许基于服务器中的DNS的虚拟主机解析。

Edit: This seems a bit overkill to me, anyone has a better suggestion? 编辑:这对我来说似乎有点矫kill过正,有人有更好的建议吗?

It's probably not routing directly to the host's network. 它可能没有直接路由到主机的网络。 This question may help. 这个问题可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM