How to Persist Data in Local DynamoDB Docker Container

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
Git
How to specify which Node version to use in Github Actions

When you want to specify which Node version to use in your Github Actions, you can use actions/setup-node@v2. The alternative way is to use a node container. When you try to use a publicly available node container like runs-on: node:alpine-xx, the pipeline gets stuck in a queue. runs-on is not …

AWS
Using semantic-release with AWS CodePipeline and CodeBuild

Here is the usual pattern of getting the source from a git repository in AWS CodePipeline. In the pipeline, we use AWS CodeStart to connect to a repo and get the source. Then, we pass it to the other stages, like deploy or publish. For some unknown reasons, CodePipeline downloads …

DBA
mysqldump Error: Unknown table ‘COLUMN_STATISTICS’ in information_schema (1109)

mysqldump 8 enabled a new flag called columm-statistics by default. When you have MySQL client above 8 and try to run mysqldump on older MySQL versions, you will get the error below. mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM ‘$”number-of-buckets-specified”‘) FROM information_schema.COLUMN_STATISTICS WHERE SCHEMA_NAME = ‘myschema’ AND TABLE_NAME = ‘craue_config_setting’;’: Unknown …