在Docker容器中针对Localstack运行Shell脚本

时间:2018-11-30 19:45:50

标签: docker docker-compose atlassian-localstack

我一直在使用localstack针对本地开发服务。我刚刚通过docker run --rm -p 4567-4583:4567-4583 -p 8080:8080 localstack/localstack

运行他们的docker映像

然后我手动运行一个小脚本来设置我的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"]

docker-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

我曾尝试在脚本命令中将localhost切换为127.0.0.1,但最终出现相同的错误。我可能在这里想念一些愚蠢的东西。

2 个答案:

答案 0 :(得分:1)

还有另一种方法来create your custom AWS resources when localstack freshly start up.,因为您已经拥有用于资源的bash脚本,因此您可以简单地将脚本批量安装到/docker-entrypoint-initaws.d/。 所以我的composefile是:

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'

在bash脚本中,我更喜欢awslocal胜过aws --endpoint,因为它可以为您利用凭证工作和端点。

答案 1 :(得分:0)

尝试将主机名添加到docker-compose文件中,然后编辑入口点文件以反映该主机名。

  

docker-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'