How to Containerize a Local DynamoDb

How to Containerize a Local DynamoDb

This is going to be a very quick tutorial. I am going to show you how you can use Docker to spin up a DynamoDb. This can very useful for prototyping an application where you need to persist some data. Since DynamoDb is a NoSQL database it gives developers a lot of flexibility. Also, if you do plan on using DynamoDb in production this is an easy way to get started without needing to pay AWS. I will be starting a series on DynamoDb soon so stay tuned!

What is DynamoDb? Amazon DynamoDB is a fully managed proprietary NoSQL database service that supports key-value and document data structures and is offered by Amazon.com as part of the Amazon Web Services portfolio. DynamoDB exposes a similar data model to and derives its name from Dynamo, but has a different underlying implementation.

TLDR: Here is the fulling working code on my Github.

Install locally

I typically start these posts off by saying to install the framework locally. Since DynamoDb is an AWS service you would first need to create an AWS account. Then, you can spin up a DynamoDb. However, for this tutorial that will not be necessary.

Create Dockerfile

Amazon has been kind enough to create a DynamoDb Docker image for us! We will be using the amazon/dynamodb-local image.

Create docker-compose.yml

Let's get started by creating a docker-compose.yml file.

nano docker-compose.yml
version: '3.7'

services:
  dev:
    container_name: dynamodb
    image: amazon/dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - dynamodata:/home/dynamodblocal
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ."
volumes:
  dynamodata: {}

A couple of notes:

  • version - This is the version of docker-compose to use. Newer versions add improved capabilities.
  • services - You can have docker start multiple containers for full-stack applications. A good use case for multiple services would be if you want to start up an Express server and a DynamoDb instance for your local API to interact with. Here we are just starting up a database.
  • ports - Here we are simply mapping port 8000 on our host system to port 8000 inside the container.
  • volumes - Volumes are created and managed by Docker. We are creating a volume named dynamodata and initializing it to an empty object. This allows the data in your database to persist after you stop and restart your container!
  • command - This is the command that is run when we start the container. We are using Java's -jar command to execute the DynamoDBLocal jar file. The -sharedDb option simply creates a shared database inside the container. -dbPath . is used to tell the jar to use the current directory for the shared database. You can read about the jar options here.

Start Your Database!

docker-compose up dev

Conclusion

That's it! You now have an instance of DynamoDb running locally! You can use this to persist data for your latest application or just spin it up to toy around with it. This can also be useful if you just want to dump JSON into a database and do some aggregation. Here is the fulling working code on my Github Enjoy -- Cheers!

Pro Tip

If you are new to DynamoDb I highly recommend using NoSQL Workbench for DynamoDB to model, visualize, and query your new database. Read my latest post to find out how to get started!

Resources

Manage data in Docker

DynamoDB Local Usage Notes

Did you find this article valuable?

Support Phillip Ninan by becoming a sponsor. Any amount is appreciated!