gpt4 book ai didi

ubuntu - 将 Web 请求指向在 Ubuntu 中运行的 Docker 容器

转载 作者:行者123 更新时间:2023-12-02 19:34:37 28 4
gpt4 key购买 nike

假设我有 mywebsite.com 指向我的 Ubuntu 服务器。

我的 Ubuntu 服务器上运行着一个 docker 容器。

是否可以将对我的 Ubuntu 服务器的请求定向到 docker 容器公开的应用程序?

我不完全确定要搜索这个问题。我想我必须将 nginx 设置为代理或其他东西?

我知道可能有更好的服务器设置,我想知道这个特定的设置是否可能。

最佳答案

对的,这是可能的。你需要:

  • 在容器内运行的应用程序或服务器正在监听一个或多个端口
  • 使用正确的命令启动 Docker 容器,以确保您的容器正在监听的端口已发布并绑定(bind)到主机上的端口
  • 确保正确配置任何防火墙规则
  • 确保主机端口上没有其他任何东西在监听(您的 docker 命令将出现“地址正在使用”错误)

  • 例如,您可以为此使用 Apache。

    使用一个简单的 Dockerfile:
    FROM httpd:2.4
    RUN echo "<h1>It works!</h1>" > /usr/local/apache2/htdocs/index.html

    和命令:
    # Builds my Dockerfile above and tags the image as "test"
    docker build . -t test
    # Creates a container with the default command running, which
    # starts Apache. Publishes (-p) port 80 inside the container and
    # binds it to port 80 on the host machine
    docker run --rm -p 80:80 test

    如果您在本地运行此程序(请参阅上面的所有注意事项),您可以访问 http://localhost并查看上面的消息。关键是 -p标志,它发布端口 80。

    https://docs.docker.com/engine/reference/commandline/run/

    nginx 有很多用途。您可以使用 nginx 作为应用程序进行监听以呈现/提供静态页面。您还可以使用 nginx 作为客户端计算机(Web 浏览器等)和另一个正在运行的进程(在另一个容器或另一个服务器中)之间的代理。如果您提供了有关您实际尝试完成的工作的更多详细信息,我可能会为您指明正确的方向。

    关于ubuntu - 将 Web 请求指向在 Ubuntu 中运行的 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47102497/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com