本地主机在 docker for mac 中不起作用

nic*_*net 5 php apache macos docker docker-compose

我正在使用最新的 Docker for mac。我遇到了这个奇怪的问题。我可以通过http://127.0.0.1/而不是http://localhost/访问我的 webapp但是我可以访问https://localhost/(自签名证书)。所以,我不确定这里有什么问题。

这是我的 docker compose。

version: "3"
services:
  php:
    build:
      context: .
    container_name: php
    volumes:
      - .:/var/www/html
      - conf:/etc/apache2/sites-enabled
    ports:
      - "80:80"
      - "443:443"
Run Code Online (Sandbox Code Playgroud)

这是我的 Apache 配置

<VirtualHost _default_:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html

  <Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/apache2/certs/localhost.crt
    SSLCertificateKeyFile /etc/apache2/certs/localhost.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>

    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-6]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  </VirtualHost>
</IfModule>
Run Code Online (Sandbox Code Playgroud)

Nil*_*ule 12

你可以尝试使用docker.for.mac.localhost

  • 现在是“host.docker.internal”。[文档](https://docs.docker.com/docker-for-mac/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host) (40认同)

lig*_*hts 9

只需将@danwild评论放入答案形式即可获得更多可见性。

您现在可以使用 来从容器内访问 localhost host.docker.internal。请参阅文档