如何在Docker中为容器分配域名? [英] How to assign domain names to containers in Docker?

查看:1316
本文介绍了如何在Docker中为容器分配域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天,我读了很多关于如何设置和运行Docker堆栈的信息。但是,我一直想念的一件事是如何设置特定容器通过域名响应访问权限,而不仅仅是使用docker dns通过容器名称进行响应。

I am reading a lot these days about how to setup and run a docker stack. But one of the things I am always missing out on is how to setup that particular containers respond to access through their domain name and not just their container name using docker dns.

我的意思是说我有一个可以从外部访问的微服务,例如:users.mycompany.com,它将进入处理用户api的微服务容器

What I mean is, that say I have a microservice which is accessible externally, for example: users.mycompany.com, it will go through to the microservice container which is handling the users api

然后,当我尝试访问customer-list.mycompany.com时,它将进入处理客户列表的微服务容器

Then when I try to access the customer-list.mycompany.com, it will go through to the microservice container which is handling the customer lists

,使用docker dns,我可以访问它们并将它们链接到docker网络,但这仅适用于要访问容器到容器而不是Internet到容器的情况。

Of course, using docker dns I can access them and link them into a docker network, but this only really works for wanting to access container to container, but not internet to container.

有人知道我该怎么做吗?或设置它的最佳方法。

Does anybody know how I should do that? Or the best way to set that up.

推荐答案

因此,您需要使用端口发布的概念,以便使用端口可以通过主机上的端口访问容器中的文件。使用此功能,您可以从 Nginx 中设置一个简单的 proxy_pass ,它将 users.mycompany.com 重定向到 myhost:1337 假设您将端口发布到 1337

So, you need to use the concept of port publishing, so that a port from your container is accessible via a port from your host. Using this, you can can setup a simple proxy_pass from an Nginx that will redirect users.mycompany.com to myhost:1337 (assuming that you published your port to 1337)

因此,如果要执行此操作,则需要使用以下方法设置容器以暴露某个端口:

So, if you want to do this, you'll need to setup your container to expose a certain port using:

docker run -d -p 5000:5000 training/webapp # publish image port 5000 to host port 5000

然后您可以从主机 curl 您的 localhost:5000 访问该容器。

You can then from your host curl your localhost:5000 to access the container.

curl -X GET localhost:5000

如果要在前面设置域名,则需要一个Web服务器实例,该实例允许您将主机名 proxy_pass 放入容器。

If you want to setup a domain name in front, you'll need to have a webserver instance that allows you to proxy_pass your hostname to your container.

ie在 Nginx 中:

server {
  listen 80;
  server_name users.mycompany.com;
  location / {
    proxy_pass http://localhost:5000;
  }
}

我建议您遵循本教程,然后检查 docker运行参考

I would advise you to follow this tutorial, and maybe check the docker run reference.

这篇关于如何在Docker中为容器分配域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆