简体   繁体   English

无法从群集和主机内部与Docker容器连接

[英]Cannot connect with Docker container from inside a swarm and the host machine

I'm composing two containers, one with the web services and one with the database. 我正在组成两个容器,一个包含Web服务,一个包含数据库。
Here's my compose file: 这是我的撰写文件:

version: '3.3'
services:
  web:
    image: microsoft/aspnetcore:1.1
    container_name: web
    ports:
    - 5555:80
    links:
    - db
  db:
    image: microsoft/mssql-server-linux:rc1
    container_name: db
    ports:
    - 1533:1433
    environment:
    - "ACCEPT_EULA=Y"
    - "MSSQL_SA_PASSWORD=MyAdminPwd2017"
    - "MSSQL_PID=Developer"

So, from my asp.net core app running in the web service I can access the database at db just using db as hostname. 因此,从web服务中运行的asp.net核心应用程序中,我可以使用db作为主机名访问db的数据库。 But db is not visible from the host (I have a default bridge network). 但是db在主机上不可见(我有默认的桥接网络)。 I do can access my database from the host if I inspect the running db container and find it's ip address, then I can connect to <ip>,1533 . 如果我检查正在运行的db容器并找到它的ip地址,则可以从主机访问数据库,然后可以连接到<ip>,1533

The thing is, the file storing the the credentials to access the database is used both by the web container and the host machine. 事实是, web容器和主机都使用存储用于访问数据库的凭据的文件。 So I need a way to name db so I can access from both worlds (being inside the swarm and outside, from my host machine) 因此,我需要一种命名db的方法,以便可以从两个世界访问(位于主机内部和外部)

Is there a way to achieve that? 有办法实现吗? I tried defining a host network in my docker-compose file and have both services uses that network, but I got a message saying only I one host network can be defined. 我尝试在docker-compose文件中定义一个主机网络,并且两种服务都使用该网络,但是我收到一条消息,说只能定义一个主机网络。

EDIT: tried to improve the question title but I'm still not convinced, improvements are welcome 编辑:试图改善问题的标题,但我仍然不相信,欢迎改进

Ok, so, to answer my own question, what I want is not posible, at least as of today. 好的,所以,要回答我自己的问题,至少在今天,我想要的东西是不可能的。

Somebody wrote a nice article on why I can't achieve what I want. 有人写了一篇很好的文章,说明为什么我无法实现自己想要的。 Having said that, it is doable in Windows Containers, but it might be a temporary "limitation". 话虽如此,它在Windows容器中是可行的,但可能是暂时的“限制”。

Link to the post: https://derickbailey.com/2016/08/29/so-youre-saying-docker-isnt-a-virtual-machine/ and link to the Docker Forum: https://forums.docker.com/t/access-dockerized-services-via-the-containers-ip-address/21151 链接后: https://derickbailey.com/2016/08/29/so-youre-saying-docker-isnt-a-virtual-machine/并链接到泊坞窗论坛: HTTPS://forums.docker。 COM / T /访问dockerized服务-通过最集装箱-IP地址/ 21151

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM