使用 docker-compose 服务名称从 LocalStack lambda 函数连接到数据库容器
Connect from LocalStack lambda function to a database container using docker-compose service name
我正在尝试从 localstack 中的 lambda 函数连接到另一个容器中的数据库 运行,但 lambda 函数无法解析服务名称。
version: '3.9'
services:
db:
image: mysql:5.7
container_name: db
restart: always
environment:
MYSQL_DATABASE: 'my_db'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql
networks:
- development
localstack:
image: localstack/localstack:latest
hostname: localstack
container_name: localstack
environment:
- AWS_DEFAULT_REGION=us-west-2
- EDGE_PORT=4566
- SERVICES=s3,lambda,apigateway,sqs
- DEBUG=1
- HOSTNAME_EXTERNAL=localstack
- DB_HOST=db
- DB_USER=admin
- DB_PASSWORD=password
networks:
- development
depends_on:
- db
ports:
- '4566-4583:4566-4583'
volumes:
- "${TEMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
networks:
development:
volumes:
my-db:
问题是 db
可以从 localstack 容器解析(例如 docker-compose exec localstack ping db
),但是 lambda 函数说 db
无法解析。
我认为您可以添加 LAMBDA_DOCKER_NETWORK=development
as an environment variable to the localstack service to connect the lambda functions to that network (see discussion),但是当我这样做时,我收到一条错误消息,显示 network development not found
docker.errors.NotFound: 404 Client Error for http+docker://localhost/v1.41/containers/f98dc74016048bc2295cf1bbf4af09c03e9a78faf5b1462f1130a76e0ca4f850/start: Not Found ("network development not found")
我也看到了这个:
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http+docker://localhost/v1.41/containers/1071c60ce1dd44800c38f726ef526388ce1a090bb1e394724cdfcbb76fb7e906/start
看起来连接到正确网络的方法是在 environment
:
中进行设置
LAMBDA_DOCKER_NETWORK=<networkname>
所以在上面的例子中:
LAMBDA_DOCKER_NETWORK=development
我正在尝试从 localstack 中的 lambda 函数连接到另一个容器中的数据库 运行,但 lambda 函数无法解析服务名称。
version: '3.9'
services:
db:
image: mysql:5.7
container_name: db
restart: always
environment:
MYSQL_DATABASE: 'my_db'
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql
networks:
- development
localstack:
image: localstack/localstack:latest
hostname: localstack
container_name: localstack
environment:
- AWS_DEFAULT_REGION=us-west-2
- EDGE_PORT=4566
- SERVICES=s3,lambda,apigateway,sqs
- DEBUG=1
- HOSTNAME_EXTERNAL=localstack
- DB_HOST=db
- DB_USER=admin
- DB_PASSWORD=password
networks:
- development
depends_on:
- db
ports:
- '4566-4583:4566-4583'
volumes:
- "${TEMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
networks:
development:
volumes:
my-db:
问题是 db
可以从 localstack 容器解析(例如 docker-compose exec localstack ping db
),但是 lambda 函数说 db
无法解析。
我认为您可以添加 LAMBDA_DOCKER_NETWORK=development
as an environment variable to the localstack service to connect the lambda functions to that network (see discussion),但是当我这样做时,我收到一条错误消息,显示 network development not found
docker.errors.NotFound: 404 Client Error for http+docker://localhost/v1.41/containers/f98dc74016048bc2295cf1bbf4af09c03e9a78faf5b1462f1130a76e0ca4f850/start: Not Found ("network development not found")
我也看到了这个:
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http+docker://localhost/v1.41/containers/1071c60ce1dd44800c38f726ef526388ce1a090bb1e394724cdfcbb76fb7e906/start
看起来连接到正确网络的方法是在 environment
:
LAMBDA_DOCKER_NETWORK=<networkname>
所以在上面的例子中:
LAMBDA_DOCKER_NETWORK=development