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

查看:99
本文介绍了如何为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 网络,但这仅适用于想要访问容器到容器,而不是互联网到容器的情况.

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

如果你想在前面设置一个域名,你需要有一个网络服务器实例,允许你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.

即在 Nginx 中:

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

我建议您遵循 本教程,也许可以查看docker run 参考.

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

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

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