Table Of Contents
- Docker Compose
- Accessing DynamoDB Local Container From Another Container
- Creating Tables
DynamoDB local Docker image enables you to get started with DynamoDB local quickly by using a docker image with all the DynamoDB local dependencies and necessary configuration built in. The new Docker image also enables you to include DynamoDB local in your containerized builds and as part of your continuous integration testing.
Image is available at: https://hub.docker.com/r/amazon/dynamodb-local
We’ll create a docker compose file to launch dynamo db local as a container which can then be accessed by other containers.
We pull the latest version of amazon/dynamodb-local image
Expose the container port 8000 to local port 8000 as the local dynamo db runs on this port
By default the local dynamo db starts with inMemory setting. We override the command here with an additional argument -sharedDb. This is to ensure that the dynamo db uses a single database file instead of separate files for each credential and region. If you didn’t enable this setting then if in case your app container and local CLI use different AWS creds then they won’t be accessing the same dynamo db state. Table created via your CLI won’t be visible to your app container.
Run docker compose and access your local dynamo db by using the endpoint url option in your CLI commands.
If you’re not using the default network mode and instead using bridge network mode, this is how your docker compose will look like:
Accessing DynamoDB Local Container From Another Container
Let’s say you have a webapp written in Java running in a tomcat container and you want to access the local dynamo db container for your integration tests.
You can make use of the hostname identical to the container name to access the local dynamodb from your webapp container, in this case, the endpoint url will be http://dynamodb:8000.
In your webapp, when you want to access the local dynamo db container, construct the client by setting the endpoint url to http://dynamodb:8000:
Then in your docker compose file, you need to set the AWS creds and region for your webapp container.
Docker image of your web app running in tomcat
Set AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY to any dummy value otherwise the credential chain will fail mentioning that it couldn’t detect any credentials.
Before your application can read/write any data to the local dynamodb, you would have to create the required tables.
Either you can create the tables as part of your webapp code (or) you can bundle a script to your web app image which will create the tables using AWS CLI and start up your application.
Your webapp docker build file can be something like below:
And in your start up script, create the tables and run catalina.