16

I am using docker enabled dynamoDB local as mentioned here

and following is my JS code:

AWS.config.update({
  region: 'sas',
  endpoint:  'http://docker.for.mac.host.internal:8000' //'http://localhost:8000'
});

and create table function below:

function createTable() {
    let params = {
        TableName: 'sas',
        KeySchema: [{
            AttributeName: 'title',
            KeyType: 'HASH',
        }],
        AttributeDefinitions: [{
            AttributeName: 'title',
            AttributeType: 'S'
        }],
        ProvisionedThroughput: {
            ReadCapacityUnits: 1,
            WriteCapacityUnits: 1,
        }
    };
    dynamoDB.createTable(params, function(err, data) {
        if (err)
            console.log(err); // an error occurred
        else
            console.log(data);
    });
}   

i could see created table sas using cli :

aws dynamodb list-tables --endpoint-url http://localhost:8000 --region=sas  

but NOT listing the table in the and it's always empty.

http://localhost:8000/shell/

any idea's?

NOTE: i can see my table with above code, by running dynamodb jar locally

 java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb 
Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47
  • for me, what did the trick was to check the region in my .aws\config file and set it the same as the one you mention in your JS code - sas (for me it is set to eu-west-2), maybe it helps someone ... – R13mus Mar 16 '21 at 06:51

3 Answers3

17

make sure you are also passing -sharedDb to the docker image.

If -sharedDb is not present then dynamodb-local will use
access keys+region as namespaces to separate tables
(as if they were under different aws accounts)

Adrian Praja
  • 402
  • 2
  • 5
  • 2
    It wouldn't work for me by just adding `-sharedDb` to the Docker run command. I had to use `docker run -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb` – andyberry88 May 19 '19 at 17:13
  • @andyberry88 great, this finally worked! Also don't forget you have to re-create the tables after you first started with `-sharedDb`. – Dmitriy Popov Feb 20 '20 at 13:34
  • I was trying to do it with `docker-compose` instead but couldn't find a way to have the tables listed in the /shell. Running it with `docker` directly worked. – Jonathan Morales Vélez Jul 04 '20 at 09:25
  • 5
    I was able to do it with docker compose. I overrode the command like this: `command: -jar DynamoDBLocal.jar -sharedDb` – Mendhak Jul 09 '20 at 12:46
12

As @Mendhak points out in the comments on another answer here, if you are using docker-compose, you will need to share the db, which you can do by overriding the command in docker-compose.yml

version: "3.7"
services:
  dynamodb-local:
    image: amazon/dynamodb-local:latest
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    command: -jar DynamoDBLocal.jar -sharedDb

I didn't see this anywhere in the sprawling AWS docs, so I wanted to leave this answer to improve visibility for other wandering devs.

Credit to Mendhak - you could upvote his comment as well as this answer, if it helped you!

Soft Bullets
  • 4,425
  • 2
  • 18
  • 15
4

You can specify -sharedDb param when start docker dynamodb.

docker run -itd -p 8000:8000  --name dev-db amazon/dynamodb-local:latest -jar DynamoDBLocal.jar -sharedDb
myfreax
  • 409
  • 5
  • 8