Search code examples
amazon-web-servicesamazon-s3docker-composevisual-studio-2022localstack

Connection refused error (127.0.0.1:4566) for Localstack S3


I'm quite new to AWS and docker.

recently I got one task to store files(json) to aws s3 and to develop that I'm integrating LocalStack extention to simulate AWS s3 on my local machine.

I'm using .Net core 6 with visual studio 2022. also installed AWSToolkit and there I'm getting error"unable to connect to aws: The security tocken included in the request is invalid" inside docker-compose file, I've added S3 as a only value to SERVICES field. do I need any other services too there to configure S3, I'm not sure about that. docker code :

version: "3.7"
services: 
  localstack: 
    container_name: awslocal
    image: localstack/localstack 
    hostname: awslocal 
    network_mode: bridge 
    ports: 
        - "4566:4566"            # LocalStack Gateway
    environment: 
        - SERVICES=s3 
        - DOCKER_HOST=unix:///var/run/docker.sock 
        - AWS_DEFAULT_REGION=eu-west-1
        - AWS_ACCESS_KEY_ID=test
        - AWS_SECRET_ACCESS_KEY=test
    volumes: 
        - "${TEMPDIR:-/tmp/newFolder}:/tmp/newFolder"
        - "/var/run/docker.sock:/var/run/docker.sock"
  myApp:
    image: ${DOCKER_REGISTRY-}myApp
    network_mode: bridge
    build:
        context: .
        dockerfile: myApp/Dockerfile

Here is the code snippet I'm using

var config = new AmazonS3Config
{
   ServiceURL = "http://127.0.0.1:4566",
   ForcePathStyle = true,
};
var credentials = new BasicAWSCredentials("test", "test");
var s3Client = new AmazonS3Client(credentials, config);
var listObjectsRequest = new ListObjectsRequest
{
   BucketName = "my-bucket"
};
var response = await s3Client.ListObjectsAsync(listObjectsRequest);

but I'm getting error as connection refused. enter image description here

so far I've tried below things

  1. both application and localstack container is in same network called "bridge"
  2. added inbound/outbound network rules for port "4566"
  3. everything looking fine from command line since I'm able to hit and get buckets for the same IP i.e http://127.0.0.1:4566
  4. tried http://localhost:4566 instead of http://127.0.0.1:4566

Can Anyone please help me with this. Thanks in advance :)


Solution

  • Hi — It appears to me that you’re accessing AWS resources such as S3 in LocalStack by running your application code in another container.

    Your application container should be configured to use LocalStack as its DNS server. Once this is done, the domain name localhost.localstack.cloud will resolve to the LocalStack container.

    To configure your application container:

    • add a user-managed docker network;
    • either determine your LocalStack container IP, or configure your LocalStack container to have a fixed known IP address;
    • set the DNS server of your application container to the IP address of the LocalStack container.

    Here is a sample Docker Compose configuration that should give you a basic idea of how to set this up:

    version: "3.8"
    
    services:
      localstack:
        container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
        image: localstack/localstack
        ports:
          # Now only required if you need to access LocalStack from the host
          - "127.0.0.1:4566:4566"            
          # Now only required if you need to access LocalStack from the host
          - "127.0.0.1:4510-4559:4510-4559"
        environment:
          - DEBUG=${DEBUG:-0}
        volumes:
          - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
          - "/var/run/docker.sock:/var/run/docker.sock"
        networks:
          ls:
            # Set the container IP address in the 10.0.2.0/24 subnet
            ipv4_address: 10.0.2.20
    
      # sample application to showcase the example
      application:
        image: ghcr.io/localstack/localstack-docker-debug:main
        entrypoint: ""
        command: ["sleep", "infinity"]
        dns:
          # Set the DNS server to be the LocalStack container
          - 10.0.2.20
        networks:
          - ls
    
    networks:
      ls:
        ipam:
          config:
            # Specify the subnet range for IP address allocation
            - subnet: 10.0.2.0/24
    

    If you are using LocalStack, just for S3, please prefer to use localstack/localstack:s3-latest. Its a S3-compatible image with only S3 APIs, and is lightweight & faster compared to the original LocalStack image.