简体   繁体   English

docker for mac:从另一个容器访问 mongo

[英]docker for mac: access mongo from another container

I have two container, one is a container for node.js code and another for a mongodb database.我有两个容器,一个是 node.js 代码的容器,另一个是 mongodb 数据库的容器。 I set the network_mode to host for both containers, there are certain network restrictions making me have to do this.我将 network_mode 设置为两个容器的主机,有一定的网络限制让我不得不这样做。 Additionally, both container are on the same physical mac machine.此外,两个容器都在同一台物理 mac 机器上。 If I want to connect to the mongodb database from the node.js container, what should my connection string look like.如果我想从 node.js 容器连接到 mongodb 数据库,我的连接字符串应该是什么样的。 I know that if I used a bridged network, I would use the name of the mongo container for the host name.我知道如果我使用桥接网络,我会使用 mongo 容器的名称作为主机名。 However, in this case I have tried localhost, 0.0.0.0, 127.0.0.1, etc. none of which are working.但是,在这种情况下,我尝试了 localhost、0.0.0.0、127.0.0.1 等,但都没有工作。 How would I access the mongdb database in this case?在这种情况下,我将如何访问 mongdb 数据库?

easiest way to use docker inspect and find the IP of the docker container and login/connect to it.使用 docker 的最简单方法检查并找到 docker 容器的 IP 并登录/连接到它。 Alternatively you can use VS Code ( https://code.visualstudio.com/download ) and install docker plugin或者,您可以使用 VS Code ( https://code.visualstudio.com/download ) 并安装 docker 插件

find running docker containers and container ids查找正在运行的 docker 容器和容器 ID

docker ps

get details of the docker specific docker container获取 docker 特定 docker 容器的详细信息

docker inspect <CONTAINER ID> | grep "IPAddress"

example例子

    "SecondaryIPAddresses": null,
    "IPAddress": "172.17.0.2",
            "IPAddress": "172.17.0.2",

VS Code docker plugin VS Code docker 插件在此处输入图像描述

According to this github issue , network_mode doesn't work on Docker for Mac.根据这个 github 问题network_mode不适用于 Mac 的 Docker。

The comment on the issue I linked did say that if you need to access a service on the host from a container, you can use host.docker.internal , but this might not be relevant to your use case.关于我链接的问题的评论确实说,如果您需要从容器访问主机上的服务,您可以使用host.docker.internal ,但这可能与您的用例无关。

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

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