【问题标题】:Is there any way to access a docker (nginx) container via a local url like http://mydomain.dev?有没有办法通过像 http://mydomain.dev 这样的本地 url 访问 docker (nginx) 容器?
【发布时间】:2023-03-03 23:48:02
【问题描述】:

我基本上使用来自:https://github.com/wouterds/docker 的设置(但不使用 mysql)

以下是我的 docker-compose.yml 文件:

nginx:
    image: nginx:1.10.2
    ports:
        - 80:80
    restart: always
    volumes:
        - ./nginx/conf:/etc/nginx/conf.d
        - ~/server/firebase-test/code:/code
    links:
        - php
    depends_on:
        - php

php:
    build: php
    expose:
        - 9000
    restart: always
    volumes:
        - ./php/conf/php.ini:/usr/local/etc/php/conf.d/custom.ini
        - ~/server/firebase-test/code:/code

如果我去http://localhost它可以正常工作

但我正在使用这个容器设置来开发一堆不同的网站。有时我需要它们同时运行,所以它们都尝试使用不可能的 localhost url。有没有办法让我手动让它们在本地被 http://websitename.devhttp://[container-name].dev 引用? docker-compose 是否自动生成某种网络映射,我可以使用它来访问容器而不是 http://localhost

对 Docker 来说非常新,所以我有点迷茫,过去一小时谷歌搜索除了一个名为“docker-hostmanager”的工具外没有其他结果,但它不适用于 v2 语法。

【问题讨论】:

    标签: php networking nginx docker docker-compose


    【解决方案1】:

    我相信您应该能够使用 server_name 指令配置 nginx 来执行此操作:

    server {
        listen       80;
        server_name  example.org  www.example.org;
        ...
    }
    

    http://nginx.org/en/docs/http/server_names.html

    【讨论】:

    • 但这会映射域 inside docker。我需要它可以在 docker 外部 访问。例如,docker 容器充当服务器,我的主机操作系统能够像世界上任何其他站点一样通过浏览器访问它。
    • 我相信容器仍然可以从主机访问,因为 nginx 端口 (80) 在 docker-compose.yml 中从容器映射到主机。
    • 您可能还需要在主机操作系统上的 hosts 文件中添加一个条目,将 nginx server_name 映射到 localhost IP,例如,127.0.0.1 example.org
    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2015-04-27
    • 2019-10-17
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多