0

I have a Spring Boot application that I am able to run locally without errors. I am trying to run it in a Docker Container, but I am getting new errors that I don't understand.

Dockerfile:

FROM maven:3.8-jdk-8 AS build
COPY . /usr/app
WORKDIR /usr/app
RUN mvn clean package

FROM openjdk:8-alpine
COPY --from=build /usr/app/target/myapp-*.jar /usr/app/myapp.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/app/myapp.jar"]

Top of the error stack:

WARN 8 --- [           main] com.amazonaws.util.EC2MetadataUtils      : Unable to retrieve the requested metadata (/latest/dynamic/instance-identity/document). Failed to connect to service endpoint:

com.amazonaws.SdkClientException: Failed to connect to service endpoint:
    at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:100) ~[aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:70) ~[aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:75) ~[aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:66) ~[aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.util.EC2MetadataUtils.getItems(EC2MetadataUtils.java:402) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:371) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.util.EC2MetadataUtils.getData(EC2MetadataUtils.java:367) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.util.EC2MetadataUtils.getEC2InstanceRegion(EC2MetadataUtils.java:282) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.regions.InstanceMetadataRegionProvider.tryDetectRegion(InstanceMetadataRegionProvider.java:59) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.regions.InstanceMetadataRegionProvider.getRegion(InstanceMetadataRegionProvider.java:50) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.regions.AwsRegionProviderChain.getRegion(AwsRegionProviderChain.java:46) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.client.builder.AwsClientBuilder.determineRegionFromRegionProvider(AwsClientBuilder.java:475) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:458) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:424) [aws-java-sdk-core-1.11.792.jar!/:na]
    at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46) [aws-java-sdk-core-1.11.792.jar!/:na]
    at org.springframework.cloud.aws.autoconfigure.secretsmanager.AwsSecretsManagerBootstrapConfiguration.smClient(AwsSecretsManagerBootstrapConfiguration.java:63) [spring-cloud-starter-aws-secrets-manager-config-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]

I am not sure if this error matters, as I saw something similar locally (though that is resolved now) and the app still started up.

Error creating bean (I think this is the cause of the crash):

ERROR 23 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration': Unsatisfied dependency expressed through field 'propertySourceLocators'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'awsSecretsManagerPropertySourceLocator' defined in class path resource [org/springframework/cloud/aws/autoconfigure/secretsmanager/AwsSecretsManagerBootstrapConfiguration.class]: Unsatisfied dependency expressed through method 'awsSecretsManagerPropertySourceLocator' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'smClient' defined in class path resource [org/springframework/cloud/aws/autoconfigure/secretsmanager/AwsSecretsManagerBootstrapConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.secretsmanager.AWSSecretsManager]: Factory method 'smClient' threw exception; nested exception is com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.

I compared the local maven version against the one in the the first image, and they are the same. My local java version is 1.8.0_275 while I have tried using images with versions 1.8.0_212 and 1.8.0_312 (I haven't found an image with the exact same version). I even tried copying over the working jar into the image instead of building it as part of the Docker build, but I got the exact same errors.

Edit: Here is my bootstrap.yml file that has the aws region

aws:
  secretsmanager:
    name: myapp-s3-creds

cloud:
  aws:
    region:
      static: eu-west-1
Jordan
  • 33
  • 2
  • 5
  • 1
    This may help - https://stackoverflow.com/questions/59517989/spring-cloud-aws-sqs-fails-to-connect-to-service-endpoint-locally – Sanil Khurana Dec 16 '21 at 22:12
  • "Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region." – xerx593 Dec 16 '21 at 22:18
  • @SanilKhurana I used that question to solve the error when it appeared locally. Not sure why the fix isn't' working in the Docker container. – Jordan Dec 16 '21 at 22:23
  • 2
    The problem is the Docker container does not have access to your `~/.aws` folder, hence to your AWS credentials and config. There are a few ways to share those with the container, see: [question](https://stackoverflow.com/questions/36354423/what-is-the-best-way-to-pass-aws-credentials-to-a-docker-container) – Ervin Szilagyi Dec 16 '21 at 22:23
  • 1
    @xerx593 edited to add bootstrap file which lists the aws region – Jordan Dec 16 '21 at 22:25

1 Answers1

0

If you have a docker-compose.yml file, you can specify the ff environment vars at the environment tag:

environment:
  - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
  - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}

Create a .env file in the directory of your docker-compose file with the values of the aws env vars set:

AWS_ACCESS_KEY_ID = your_access_key_id
AWS_SECRET_ACCESS_KEY = your_secret_access_key

On docker-compose build or up, the values will be copied from the .env file into the placeholders.

Joel Klo
  • 46
  • 4