amazon-web-servicesdocker-composeamazon-dynamodb

Docker-compose AWS-CLI of dynamoDB is connecting before the dynamoDB local is up


I followed the exact documentation here but I'm having the issue when I run docker-compose where the app-node is trying to connect before the dynamodb-local is fully up and running. I had to restart it to get it to connect. What's the issue here? I used the exact code from the documentation:

version: '3.8'
services:
 dynamodb-local:
   command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
   image: "amazon/dynamodb-local:latest"
   container_name: dynamodb-local
   ports:
     - "8000:8000"
   volumes:
     - "./docker/dynamodb:/home/dynamodblocal/data"
   working_dir: /home/dynamodblocal
 app-node:
   depends_on:
     - dynamodb-local
   image: amazon/aws-cli
   container_name: app-node
   ports:
    - "8080:8080"
   environment:
     AWS_ACCESS_KEY_ID: 'DUMMYIDEXAMPLE'
     AWS_SECRET_ACCESS_KEY: 'DUMMYEXAMPLEKEY'
   command:
     dynamodb describe-limits --endpoint-url http://dynamodb-local:8000 --region us-west-2

I would get the error: Could not connect to the endpoint URL: "http://dynamodb-local:8000/" Running the container again after it fails would get it to work


Solution

  • I found a workaround for this. Since the newer version of DynamoDB no longer accepts connections via localhost:8000 nor the shell for localhost:8000/shell, it will always return a 400 status code. I added the healthcheck for the dynamoDB to wait for a returned 400 status code which would ensure the DB is ready for connection.

        healthcheck:
          test:
            [
              "CMD-SHELL",
              'if [ "$(curl -s -o /dev/null -I -w ''%{http_code}'' http://localhost:8000)" == "400" ]; then exit 0; else exit 1; fi',
            ]
          interval: 5s
          timeout: 5s
          retries: 10
        ```