简体   繁体   English

如何从 AWS SAM 本地 Docker 容器访问另一个 Docker 容器(运行节点快速服务器)?

[英]How do I access another Docker container (running a node express server) from a AWS SAM Local Docker container?

We are developing locally using SAM Local to invoke a Lambda in an API Gateway.我们正在使用 SAM Local 在本地开发以调用 API 网关中的 Lambda。 SAM Local does this using a Docker container (setup as close to the Lambda node runtime as possible). SAM Local 使用 Docker 容器(设置尽可能接近 Lambda 节点运行时)执行此操作。 We want this Lambda to access some data in an API Mocking service in the shape of some Node express servers running in another container (this could also just be run locally too if needed).我们希望这个 Lambda 以在另一个容器中运行的一些 Node express 服务器的形式访问 API Mocking 服务中的一些数据(如果需要,这也可以只在本地运行)。 Both containers are in a user-created Docker bridge network created as follows:两个容器都在用户创建的 Docker 桥接网络中,创建如下:

docker network create sam-demo

The API mocking service is run and added to the bridge network:运行 API 模拟服务并添加到桥接网络:

docker run --network sam-demo --name mock -d -P mock:latest

The Lambda is invoked in debug mode and added to the bridge network: Lambda 在调试模式下被调用并添加到桥接网络中:

sam local start-api -t template.json -d 9229 --docker-network sam-demo

Inspecting the bridge network reveals both the SAM local lambda (wizardly_knuth) and the mocks are there:检查桥接网络会发现 SAM 本地 lambda (wizardly_knuth) 和模拟都在那里:

        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "5ebfa4295a56e4df840676a2e214891543fd4e8cb271ed70ddd67946ab451119": {
                "Name": "wizardly_knuth",
                "EndpointID": "xxx",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "d735c9aa840e4ce7180444cf168cd6b68451c9ca29ba87b7cb23edff11abea7b": {
                "Name": "mock",
                "EndpointID": "xxx",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }

Now, what should the URL be for the Lambda to hit the mock?现在,Lambda 命中模拟的 URL 应该是什么? According to Docker docs it should be the IPv4Address of the mock container ie http://172.20.0.2 but I am not sure what port or how to find what port?根据Docker 文档,它应该是模拟容器的 IPv4Address,即http://172.20.0.2但我不确定哪个端口或如何找到哪个端口?

I can exec into the mock and ping the SAM Local container successfully BUT I can't do the same from the SAM Local container as the shell doesn't have ping, curl, nc or anything installed.我可以执行模拟并成功 ping SAM 本地容器,但我不能从 SAM 本地容器执行相同的操作,因为 shell 没有安装 ping、curl、nc 或任何东西。

I can't hit the Mock container directly from my machine as it is a Mac and I believe there are issues with doing so.我无法直接从我的机器上访问 Mock 容器,因为它是一台 Mac,我相信这样做会有问题。

Any advice or next steps are greatly appreciated.非常感谢任何建议或后续步骤。

Much thanks,非常感谢,

Sam山姆

UPDATE更新


In the end I gave up on this approach as I could not figure out what the URL for the Lambda should be to hit the mock within the Docker Bridge network.最后我放弃了这种方法,因为我无法弄清楚 Lambda 的 URL 应该是什么才能在 Docker Bridge 网络中命中模拟。

The alternative approach was to actually just hit the mock Docker container directly from the Lambda using this URL (the mock container is exposing port 3002):-另一种方法是使用此 URL 直接从 Lambda 访问模拟 Docker 容器(模拟容器暴露端口 3002):-

http://docker.for.mac.localhost:3002/

Hope this might help somebody out.... please let me know if anyone solves the bridge network issue I originally posted about.希望这可以帮助某人.... 如果有人解决了我最初发布的桥接网络问题,请告诉我。

Thanks,谢谢,

Sam山姆

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

相关问题 从另一个docker容器内部访问在docker容器中运行的服务 - Access service running in docker container from inside another docker container 如何从另一个 docker 容器访问一个 docker 容器的快速路由 - How to get access to express routes of a docker container from another docker container 从本地 vs docker 容器对 mysql 的 express/Node 测试调用 - express/Node testing calls to mysql from local vs docker container 从另一个容器连接到在 docker 容器中运行的 mysql 服务器 - connect to mysql server running in docker container from another container 如何使用 nginx docker 容器访问本地主机上的服务器? - How do I access a server on localhost with nginx docker container? 如何为在 docker 容器内运行的快速服务器启用 xvfb? - How to enable xvfb for an express server running inside a docker container? 如何将 docker 容器端口暴露给另一个容器 - How do I expose a docker container port to another container 在另一个Docker容器中运行一个Docker容器? - Running a docker container inside another docker container? 从 docker 容器内的节点服务器访问 ubuntu localhost - Access ubuntu localhost from node server inside docker container Node - 将 Node Express 连接到在 Docker 容器中运行的 Postgres - Node - Connect Node Express to Postgres Running In Docker Container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM