php - Docker - 无法从 Mac 主机访问容器

标签 php mysql lamp docker

我似乎无法访问在我的 Mac 上本地运行的简单 Docker 容器。我可以从容器运行 curl localhost 并看到提供了默认的 Apache 页面,但我无法从我机器的浏览器中访问它。

我想知道我是否有 VirtualBox 配置问题或其他问题。对诊断问题有帮助吗?

docker 文件

# Build the image of ubuntu 12.04 LTS
from ubuntu:precise

# Run apt-get update
run apt-get -y update

# Install LAMP
run DEBIAN_FRONTEND=noninteractive apt-get -y install lamp-server^
run apt-get -y install vim-tiny curl wget

# Put custom scripts in the container and give proper permissions to them
add ./startup.sh /usr/local/bin/startup.sh
run chmod 755 /usr/local/bin/startup.sh

add site.vhost /etc/apache2/sites-available/site
run a2ensite site

# Expose port 80 to the host machine
expose 80

站点.vhost

<VirtualHost *:80>

    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        #Order allow,deny allow from all
    </Directory>

    # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg.
    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

启动.sh

#!/bin/bash

a2dissite default
apache2ctl graceful
/usr/bin/mysqld_safe &

开始...

我将使用 docker build -t test1 . 构建镜像 - 这似乎运行良好。

在初始设置中,我运行 docker run -d -v $(pwd)/mysql:/tmp/mysql test1/bin/bash -c "cp -rp/var/lib/mysql/*/tmp/mysql" 来设置 MySQL。

然后我运行 docker run -i -t -v $(pwd)/mysql:/var/lib/mysql -v $(pwd)/www:/var/www -p 8080:80 test1/bin/bash 启动实际实例。

到达终端后,我运行 service apache2 start,一切似乎都运行良好。如果我运行 curl localhost,我会得到默认页面。

一切看起来都很好,只是从 Host 那里命中它不起作用。我应该能够导航到 http://127.0.0.1:8080 并将其转发到端口 80 上的容器,对吗?

最佳答案

您需要将端口从 VM 转发到主机。来自 docker 的默认“随机”范围的示例是: (来自http://docs.docker.io/en/latest/installation/mac/#forwarding-vm-port-range-to-host)

# vm must be powered off
for i in {49000..49900}; do
 VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i";
 VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i";
done

但是如果你想专门转发8080:

VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8080,tcp,,8080,,8080"

等如果您还使用 8080 在 Mac 本身上进行测试,则可能需要选择不同的端口。

关于php - Docker - 无法从 Mac 主机访问容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946423/

相关文章:

mysql - 优化此查询 - 我应该做哪些索引或更改?

javascript - 使用 PHP 从服务器下载 apk 时出现垃圾输出

php - mysql无缓冲查询锁定不锁定

python - 为什么Python数据库查询会以这种方式返回记录集结果?

mysql - sql server 中的 XMLNS 问题

mysql - 无法登录新的 mysql 服务器以安装 CentOS 7

php - 将 Docker 用于多个 php 应用程序

php - Apache2 没有获得正确的路径

添加 PHP 扩展时出现 PHP 错误

php - openshift mysql 数据库连接问题