本机怎么访问docker容器内的网站呢,已将容器的80端口映射到了本机的80端口

比如我在容器内部署了 laravel.test 我在本机怎么访问到这个laravel.test啊 现在localhost是可以的

这是本机 ping localhost

C:\Users\Administrator>ping localhost

正在 Ping DESKTOP-ID2TSG [::1] 具有 32 字节的数据:
来自 ::1 的回复: 时间<1ms
来自 ::1 的回复: 时间<1ms
来自 ::1 的回复: 时间<1ms
来自 ::1 的回复: 时间<1ms

::1 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失)往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

本机怎么访问docker容器内的网站呢,已将容器的80端口映射到了本机的80端口

我能访问 127.0.0.1的东西 localhost 也可以

本机怎么访问docker容器内的网站呢,已将容器的80端口映射到了本机的80端口

但是ping localhost 的时候跟ping 其他我自定义域名的时候不一样 下面是ping测试域名的
C:\Users\Administrator>ping erp.test

正在 Ping erp.test [127.0.0.1] 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128

127.0.0.1 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失)往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

这是我本机的hosts文件内容

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
127.0.0.1 erp.test
127.0.0.1 localhost
#192.168.10.10  health.test
#127.0.0.1 aaaa.test
# Added by Docker Desktop
192.168.1.102 host.docker.internal
192.168.1.102 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

容器内部的nginx配置
server
{
listen 80;
server_name erp.test;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/erp;

#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END

#ERROR-PAGE-START  错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END

#PHP-INFO-START  PHP引用配置,可以注释或修改
include enable-php-81.conf;
#PHP-INFO-END

#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/erp.test.conf;
#REWRITE-END

#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
{
    return 404;
}

#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
    allow all;
}

#禁止在证书验证目录放入敏感文件
if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
    return 403;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires      30d;
    error_log /dev/null;
    access_log /dev/null;
}

location ~ .*\.(js|css)?$
{
    expires      12h;
    error_log /dev/null;
    access_log /dev/null;
}
access_log  /www/wwwlogs/erp.test.log;
error_log  /www/wwwlogs/erp.test.error.log;

}

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

docker 在本机?映射 80 端口了,按道理在本机电脑hosts 添加就行啊 127.0.0.1 laravel.test

1年前 评论
周小云 (楼主) 1年前
php_yt (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
php_yt (作者) 1年前
周小云 (楼主) 1年前
php_yt (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
讨论数量: 21

改 host 文件?

1年前 评论
周小云 (楼主) 1年前

修改 host 呢是最简单的做法,但是每次有新的项目都要手动加一下!

当然你也可以在 Docker 中部署一个 CoreDNS 服务,具体参考《使用 CoreDNS 搭建本地域名服务》

1年前 评论
周小云 (楼主) 1年前
周小云 (楼主) 1年前

如果你只是想要单纯的 用 laravel.test 访问的话,你可以这样;先把本地hosts文件添加 laravel.test docker容器ip:端口号;docker 容器中不需要做任何修改;

1年前 评论

注意 docker的ip是动态(如果你没有做静态处理的话); 如果你可以使用127.0.0.1:端口号访问到容器部署的项目,那你就在宿主机的host 配置 laravel.test 127.0.0.1:端口号

1年前 评论
周小云 (楼主) 1年前
周小云 (楼主) 1年前

docker 在本机?映射 80 端口了,按道理在本机电脑hosts 添加就行啊 127.0.0.1 laravel.test

1年前 评论
周小云 (楼主) 1年前
php_yt (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
php_yt (作者) 1年前
周小云 (楼主) 1年前
php_yt (作者) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前
周小云 (楼主) 1年前

ping没有用啊,改hosts 文件 127.0.0.1 laravel.test

1年前 评论
周小云 (楼主) 1年前
Dash007 (作者) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!