Alex Fruzenshtein
Alex Fruzenshtein

Reputation: 3126

Docker: How to create a table for local dynamo DB?

I'm trying to create a docker container with local Amazon Dynamo DB. And it actually works. But I can not understand how to create a table for this image in Docker file?

Through the javascript I create a table like this:

var params = {
    TableName: 'UserActivity',
    KeySchema: [ 
        {
            AttributeName: 'user_id',
            KeyType: 'HASH',
        },
        {
            AttributeName: 'user_action', 
            KeyType: 'RANGE', 
        }
    ],
    AttributeDefinitions: [
        {
            AttributeName: 'user_id',
            AttributeType: 'S',
        },
        {
            AttributeName: 'user_action',
            AttributeType: 'S',
        }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 2, 
        WriteCapacityUnits: 2, 
    }
};
dynamodb.createTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response

});

And here is my Docker file:

FROM openjdk:8-jre-alpine

ENV DYNAMODB_VERSION=latest

RUN apk add --update curl && \
    rm -rf /var/cache/apk/* && \
    curl -O https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_${DYNAMODB_VERSION}.tar.gz && \
    tar zxvf dynamodb_local_${DYNAMODB_VERSION}.tar.gz && \
    rm dynamodb_local_${DYNAMODB_VERSION}.tar.gz

EXPOSE 8000

ENTRYPOINT ["java", "-Djava.library.path=.", "-jar", "DynamoDBLocal.jar",  "--sharedDb", "-inMemory", "-port", "8000"]

Upvotes: 3

Views: 14450

Answers (2)

Alex Fruzenshtein
Alex Fruzenshtein

Reputation: 3126

The main problem was in base image which I used for local dynamodb. I mean openjdk:8-jre-alpine Looks like the alpine distribution does not have something important for functioning of local dynamodb. So here is a working Dockerfile for local dynamodb:

FROM openjdk:8-jre

ENV DYNAMODB_VERSION=latest

COPY .aws/ root/.aws/

RUN curl -O https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_${DYNAMODB_VERSION}.tar.gz && \
    tar zxvf dynamodb_local_${DYNAMODB_VERSION}.tar.gz && \
    rm dynamodb_local_${DYNAMODB_VERSION}.tar.gz

EXPOSE 8000

ENTRYPOINT ["java", "-Djava.library.path=.", "-jar", "DynamoDBLocal.jar",  "--sharedDb", "-inMemory"]

I'd be happy to read what is the main problem of alpine in this particular case.

Upvotes: 0

Ivan Mushketyk
Ivan Mushketyk

Reputation: 8295

You need to point your DynamoDB client to the local DynamoDB endpoint.

Do something like:

dynamodb = new AWS.DynamoDB({
    endpoint: new AWS.Endpoint('http://localhost:8000')
});
dynamodb.createTable(...);

UPDATE

Alternatively you can use AWS CLI to create tables in your local DynamoDB without using JavaSciprt code. You need to use the "--endpoint-url" to point CLI to your local instance like this:

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

You need to use create-table command to create a table.

Upvotes: 11

Related Questions