How to Persist Data in Local DynamoDB Docker Container
- By : Mydatahack
- Category : Infrastructure, Installation and Configuration
- Tags: AWS, Docker, DynamoDB
Having DynamoDB running locally is handy for local development. There are two ways to run DynamoDB locally, (1) using a jar file or (2) using a docker image. Generally speaking, running container is better than running a jar file in your local computer because it gives you more flexibility and consistency. AWS offers a DynamoDB local Docker image. We can pull the image and run it in your local machine easily.
# install docker pull amazon/dynamodb-local # start docker run -dp 8000:8000 --name localDynamoNoMount amazon/dynamodb-local
Now we can start creating tables and inserting data into this. However, once you stop the container, all the data will be lost because everything is stored in memory.
To persist data, the best option is to mount a volume to this. We can do this by using Dockerfile to create a local data folder in the container and map it to the volume on the local machine.
There is already an example available for both Dockerfile. See dynamodb-local-persist.
Steps
Create a Dockerfile as below
FROM amazon/dynamodb-local WORKDIR /home/dynamodblocal RUN mkdir ./db && chown -R 1000 ./db CMD ["-jar", "DynamoDBLocal.jar", "-dbPath", "./db", "-sharedDb"] VOLUME ["./db"]
Then build and run with the command below.
docker build -t local-dynamodb-data . # checking the newly created image docker image ls # run with volume docker run -d --name mydynamodb -p 8000:8000 -v dynamodb-local:/home/dynamodblocal/db local-dynamodb-data
Then we can create table.
# Create Table aws dynamodb create-table ^ --endpoint-url http://localhost:8000 ^ --table-name Music ^ --attribute-definitions ^ AttributeName=Artist,AttributeType=S ^ AttributeName=SongTitle,AttributeType=S ^ --key-schema ^ AttributeName=Artist,KeyType=HASH ^ AttributeName=SongTitle,KeyType=RANGE ^ --provisioned-throughput ^ ReadCapacityUnits=10,WriteCapacityUnits=5 # List Table aws dynamodb list-tables --endpoint-url http://localhost:8000
Once you stop the local dynamodb docker and restart it, you can still see the table created persists.
docker stop mydynamodb docker start mydynamodb
Alternatively, we can use docker-compose to run the container instead of using the docker build and run command with the existing Dockerfile.
version: "3.8" services: dynamodb: build: context: . dockerfile: Dockerfile container_name: dynamoDbLocalDockerCompose ports: - "8111:8000" volumes: - dynamodb-local:/home/dynamodblocal/db volumes: dynamodb-local: driver: local
Once we have this file, we can run the docker-compose up command.
docker-compose up