14

我一直在使用 localstack 来开发针对本地的服务。我刚刚通过运行他们的 docker 镜像docker run --rm -p 4567-4583:4567-4583 -p 8080:8080 localstack/localstack

然后我手动运行一个小脚本来设置我的 S3 存储桶、SQS 队列等。

现在,我想让其他人更容易做到这一点,所以我想我只需添加一个 Dockerfile 和 docker-compose.yml 文件。不幸的是,当我尝试启动并运行它时,docker-compose up我收到一个错误,即我的安装脚本中的命令无法连接到 localstack 服务。

make_bucket failed: s3://localbucket Could not connect to the endpoint URL: "http://localhost:4572/localbucket"

Dockerfile:

FROM localstack/localstack

#since this is just local dev set up, localstack doesn't require 
anything specific here.
ENV AWS_DEFAULT_REGION='[useast1]'
ENV AWS_ACCESS_KEY_ID='[lloyd]'
ENV AWS_SECRET_ACCESS_KEY='[christmas]'

COPY bin/localSetup.sh /localSetup.sh
COPY fixtures/notifications.json /notifications.json
RUN ["chmod", "+x", "/localSetup.sh"]
RUN pip install awscli

# expose service & web dashboard ports
EXPOSE 4567-4582 8080

ENTRYPOINT ["/localSetup.sh"]

码头工人-compose.yml

version: '3'
services:
  localstack:
    build: .
    ports:
      - "8080:8080"
      - "4567-4582:4567-4582"

localSetup.sh

#!/bin/bash

aws --endpoint-url=http://localhost:4572 s3 mb s3://localbucket
#additional similar calls but left off for brevity

我尝试在我的脚本命令中切换localhost127.0.0.1,但我最终遇到了同样的错误。我可能在这里遗漏了一些愚蠢的东西。

4

2 回答 2

16

当 localstack 新启动时,还有另一种方法可以创建您的自定义 AWS 资源。由于您已经有一个用于资源的 bash 脚本,因此您可以简单地将脚本卷挂载到/docker-entrypoint-initaws.d/. 所以我的 docker-compose 文件将是:

localstack:
    image: localstack/localstack:latest
    container_name: localstack_aws
    ports:
      - '4563-4599:4563-4599'
      - '8055:8080'
    environment:
      - SERVICES=s3
      - DEFAULT_REGION=us-east-1
    volumes:
      - './localSetup.sh:/docker-entrypoint-initaws.d/make-s3.sh'

此外,我更喜欢awslocalaws --endpointbash 脚本中,因为它为您利用凭据工作和端点。

于 2020-07-07T07:22:31.663 回答
0

尝试将主机名添加到 docker-compose 文件并编辑您的入口点文件以反映该主机名。

码头工人-compose.yml

version: '3'
services:
  localstack:
    build: .
    hostname: localstack
    ports:
      - "8080:8080"
      - "4567-4582:4567-4582"

localSetup.sh

#!/bin/bash

aws --endpoint-url=http://localstack:4572 s3 mb s3://localbucket

这是我docker-compose-dev.yaml用来测试使用 localstack 的应用程序的。我使用了命令docker-compose -f docker-compose-dev.yaml up,我也使用了localSetup.sh您使用的相同。

version: '3'
services:

  localstack:
    image: localstack/localstack
    hostname: localstack
    ports:
      - "4567-4584:4567-4584"
      - "${PORT_WEB_UI-8082}:${PORT_WEB_UI-8082}"
    environment:
      - SERVICES=s3
      - DEBUG=1
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      - backend

  sample-app:
    image: "sample-app/sample-app:latest"
    networks:
      - backend
    links:
      - localstack
    depends_on:
      - "localstack"

networks: 
  backend:
    driver: 'bridge'
于 2018-12-12T20:29:02.183 回答