正确的IP地址以访问Apache Docker容器中的Web应用程序 [英] Correct IP address to access web application in apache docker container

查看:81
本文介绍了正确的IP地址以访问Apache Docker容器中的Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 docker-compose.yml 中定义了一个简单的Apache docker设置:

I have an easy apache docker setup defined in a docker-compose.yml:

services:
    apache: 
        image: php:7.4-apache
        command: /bin/bash -c "/var/www/html/startup.sh && exec 'apache2-foreground'"
        volumes:
            - ./:/var/www/html
            - /c/Windows/System32/drivers/etc/hosts:/tmp/hostsfile
        ports:
            - "80:80"

startup.sh 脚本中,我想通过卷从主机OS修改 hosts 文件.在这里,我想动态添加条目以将主机名 test.local 解析为docker Web应用程序的ip地址,如下所示:

From the startup.sh script I want to modify the hosts file from the host OS through the volume. Here I want to dynamically add an entry to resolve the hostname test.local to the ip address of the docker web application like so:

<ip-address> test.local

这样,我应该可以在本地浏览器中使用指定的主机名 http://test.local 打开应用程序.

This way I should be able to open the application with the specified hostname http://test.local in my local browser.

在编写 startup.sh 脚本之前,我想尝试通过从我获得的容器IP地址手动打开 http://172.19.0.2 上的应用程序/p>

Before writing the startup.sh script I wanted to try to manually open the application at http://172.19.0.2 via the containers IP address I got from

docker inspect apache_test

但是该页面无法打开: ERR_CONNECTION_TIMED_OUT

But the page won't open: ERR_CONNECTION_TIMED_OUT

我不能从该IP访问该应用程序吗?我想念什么?我使用错误的IP地址吗?

Shouldn't I be able to access the application from that IP? What am I missing? Do I use the wrong IP address?

BTW :我正在将Windows的Docker桌面与Hyper-V后端一起使用

BTW: I am using Docker Desktop for Windows with the Hyper-V backend

编辑:我可以通过 http://localhost 访问该应用程序,但是由于我想向 hosts 归档这不是解决方案.

Edit: I am able to access the application via http://localhost but since I want to add a new entry to the hosts file this is not the solution.

推荐答案

显然无法通过IP访问(ping)您的Linux容器.

现在有一些确实很脏的解决方法其中涉及更改Docker安装的 .ps1 文件,以获取Windows上的 DockerNAT 接口.之后,您需要在Windows路由表中添加新路由,如所述这里:

Now there is some really dirty workaround for this that involves changing a .ps1 file of your Docker installation to get back the DockerNAT interface on windows. After that you need to add a new route in your windows routing table as described here:

route /P add <container-ip> MASK 255.255.0.0 <ip-found-in-docker-desktop-settings>

然后,您可以从Windows主机ping您的docker容器.我没测试过...

Then you might be able to ping your docker container from the windows host. I didn't test it though...

在阅读 hosts 文件将 test.local 解析为容器IP)的解决方案."https://github.com/docker/for-win/issues/5538#issuecomment-611126380" rel ="nofollow noreferrer">此处

I found a solution to my original issue (resolution of test.local to container IP via hosts file) while reading one of the threads linked above here

这涉及在 docker-compose.yml ports 部分的 127.0.0.0/8 IP范围内设置免费回送IP.>:

That involves setting a free loopback IP in the 127.0.0.0/8 IP range in your ports section of the docker-compose.yml:

ports:
    - "127.55.0.1:80:80"

之后,将以下内容添加到您的 hosts 文件中:

After that you add the following to your hosts file:

127.55.0.1 test.local

您可以在 http://test.local

要对其他经过docker化的应用程序执行此操作,也只需选择另一个免费环回地址即可.

To do that for other dockerized applications too just choose another free loopback address.

这篇关于正确的IP地址以访问Apache Docker容器中的Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆