【问题标题】:Docker Virtualhost outside container容器外的 Docker 虚拟主机
【发布时间】:2018-06-19 00:07:05
【问题描述】:

我的Docker 运行正常,但是我想更改服务器名称的名称,现在可以使用这种方式http://localhost:8080,但是我想为同一主机使用另一个网址,可以吗?

我已经在容器内配置了虚拟主机,并将127.0.0.1 test.local 添加到/etc/hosts 我容器内的所有内容中。
现在,从我的容器内部,URL(test.local) 响应良好,但是当我从外部尝试时,只能使用 localhost:8080

任何帮助

【问题讨论】:

    标签: docker docker-compose dockerfile docker-container


    【解决方案1】:

    我认为您需要一个按名称侦听的 docker 容器,而不是使用具有不同端口的 localhost。

    1. 在 /etc/hosts 中添加您需要的服务器名称的 dns 条目

      $ 纳米 /etc/hosts

      127.0.0.1 test.local

    2. 编辑 composer.yml

    版本:'2'

     services: 
     web:
        build: ./docker/
        ports:
            - "80:80"
        volumes:
            - .:/var/www/app
        tty: true
        extra_hosts:
            - "test.local:127.0.0.1"
        hostname: test
        domainname: local
    
    1. 在 DockerFile 中添加服务器名称:

      运行 printf "127.0.0.1 api.local" >> /etc/hosts

    2. 在 DockerFile 中配置虚拟主机

    3. 构建容器:

      $ docker-compose up -d

    4. 检查主机名是否已应用于容器:

    $ docker exec -it test_local_web_1 主机名

    它应该返回:

    $ test.local

    1. 检查服务器是否正在运行并通过服务器名称进行侦听:

    $ curl -get http://test.local

    【讨论】:

      【解决方案2】:

      要问的主要问题是您的情况:

      使用虚拟主机进行本地 Docker 开发

      请阅读 --network=host 或 Docker-Compose network_mode: "host"

      请检查:

      https://docs.docker.com/v17.09/engine/userguide/networking/default_network/configure-dns/

      Multiple vhosts on one and the same docker container

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-22
        • 1970-01-01
        • 2019-09-20
        • 2017-10-02
        • 2019-08-25
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        相关资源
        最近更新 更多