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.

so far I've tried below things
- both application and localstack container is in same network called "bridge"
- added inbound/outbound network rules for port "4566"
- 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
- tried http://localhost:4566 instead of http://127.0.0.1:4566
Can Anyone please help me with this. Thanks in advance :)
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.cloudwill resolve to the LocalStack container.To configure your application container:
Here is a sample Docker Compose configuration that should give you a basic idea of how to set this up: