简体   繁体   English

Docker容器访问父服务器中的localhost

[英]Docker container access to localhost in parent server

I've little problem with my docker container. 我的docker容器没什么问题。 I'm trying to get access by curl from container to my parent server localhost in order to execute query in elasticsearch. 我试图通过curl从容器访问我的父服务器localhost,以便在elasticsearch中执行查询。 Elastic is working only local area on parent server. Elastic在父服务器上仅在本地区域工作。

curl http://127.0.0.1:9200
curl: (7) Failed to connect to 127.0.0.1 port 9200: Connection refused

The containers ip is 172.17.0.3 and Im able to ping container from parent server 容器ip为172.17.0.3,我无法从父服务器ping容器

Its my docker build 它是我的docker构建

cd ./api_docker
    container="$(docker ps | grep "visitors/api:gniewoy" | cut -d " " -f1)"
    git reset --hard HEAD &&
    git pull &&
    composer update &&
    docker build -t visitors/api:gniewoy . &&
    cd ../
    docker kill $container &&
    docker run -p 705:80  --env-file docker_api_env -d /etc/docker/

After built the continer is working fine on port 705, but is only the elastic issue. 组装完成后,阀芯在705端口上可以正常工作,但这只是弹性问题。 Below are deatils my conainer 以下是我的同伴的死亡故事

"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "443/tcp": null,
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "705"
                    }
                ]
            },
            "SandboxKey": "",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "",
                    "EndpointID": "",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": ""
                }
            }
        }

Im working od Debian 8. Thank you 我正在od Debian 8上工作。谢谢

You need to share the port by -p tag something like -p 9200:9200 您需要通过-p标签共享端口,例如-p 9200:9200

Here the first 9200 is internal port and the second one is container port. 这里的第一个9200是内部端口,第二个是容器端口。

Take a look at this 看看这个

The only way to reach loopback on the host machine is to be on it's same loopback network. 在主机上达到环回的唯一方法是在同一环回网络上。 By default docker will run your container in its own isolated networking namespace, which includes its own separate loopback interface. 默认情况下,docker将在您自己的隔离网络名称空间中运行您的容器,该名称空间包括其自己的单独环回接口。 To run with the host machine's loopback (and the rest of the host machine's networking stack), you would call: 要与主机的回送(以及主机的其余网络堆栈)一起运行,请调用:

docker run --net host ...

With docker-compose.yml, you would include the following section on the relevant service: 使用docker-compose.yml,您将在相关服务中包括以下部分:

network_mode: "host"

If you can configure your elastic server to listen on all interfaces, that would be better from the container side (though this may introduce other security issues). 如果您可以将弹性服务器配置为在所有接口上进行侦听,那么从容器方面来看会更好(尽管这可能会带来其他安全问题)。 You would then, from the container, connect to a non-loopback ip on the host. 然后,您将从容器中连接到主机上的非回送ip。

Your configuration need to be /etc/elasticsearch/elasticsearch.yml 您的配置必须是/etc/elasticsearch/elasticsearch.yml

network :
    host : 127.0.0.1  //or ifconfig (ex eth0 : IP)

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

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