DynamoDB is cheap and fast, isn't it? Did you know that there is a Docker Image for developing such DynamoDB locally? We will inquire about an example of building a development environment using such Docker Image.
The version of the image to be used according to the production environment is as follows. Use dynamodb-admin for GUI operation of DynamoDB. Node is used when building the environment together.
environment | version |
---|---|
Python | 3.7.4 |
MySQL | 5.7 |
Node | 10.16.3-alpine |
We're building from scratch, so let's start the Django project first. The official Django image is an older version so I'll make it myself.
$ django-admin startproject dynamodb_example
$ cd dynamodb_example/
$ touch docker-compose.yml
$ touch Dockerfile
FROM python:3.7.4
RUN apt-get update
RUN apt-get install -y --no-install-recommends apt-utils gettext
RUN mkdir /app; mkdir /app/dynamodb_example
WORKDIR /app
COPY dynamodb_example /app/dynamodb_example
COPY requirements.txt /app/
COPY manage.py /app/
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]
docker-compose.yml
version: "3"
services:
mysql:
container_name: example-mysql
ports:
- 53306:3306
image: mysql:5.7
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
volumes:
- ./persist/mysql:/var/lib/mysql
restart: always
environment:
MYSQL_USER: example
MYSQL_PASSWORD: example
MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD: example
django:
container_name: example-django
build: .
volumes:
- .:/app
working_dir: /app
command: sh -c "./wait-for-it.sh db:3306; python3 manage.py runserver 0.0.0.0:8000"
env_file: .env
ports:
- 58080:8000
depends_on:
- mysql
There are 50,000 ports because I definitely don't want to wear them. There is no particular meaning. The library for connecting to DynamoDB contains ** boto3 ** and ** pynamodb ** that wraps it.
requirements.txt
boto3
pynamodb
Django==2.2.4
djangorestframework==3.10.3
django-filter
mysqlclient==1.3.13
I don't really make sense of environment variables, but ...
.env
DB_ENGINE=django.db.backends.mysql
DB_HOST=mysql
DB_DATABASE=example
DB_USERNAME=root
DB_PASSWORD=example
DB_PORT=3306
For the time being, I want to check the communication between Django and MySQL, so I will rewrite Django's DATABASES.
setting.py
DATABASES = {
'default': {
'ENGINE': os.getenv('DB_ENGINE'),
'NAME': os.getenv('DB_DATABASE'),
'USER': os.getenv('DB_USERNAME'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'OPTIONS': {
'init_command': 'SET foreign_key_checks = 0;',
'charset': 'utf8mb4',
},
}
}
If you come to this point, check if it starts for the time being.
$ docker-compose up -d
$ docker-compose exec django bash
$ python manage.py migrate
If all goes well, then add ** DynamoDB Local ** and ** dynamodb-admin **.
DynamoDB Local Put dynamodb in docker-compose.yml. Specify dbPath at the end of the command to make it persistent.
docker-compose.yml
dynamodb:
container_name: example-dynamodb
image: amazon/dynamodb-local
command: -jar DynamoDBLocal.jar -dbPath /home/dynamodblocal/data
volumes:
- ./persist/dynamodb:/home/dynamodblocal/data
ports:
- 50706:8000
dynamodb-admin I had an official image, but I made it, so I will use that.
$ mkdir dynamodb-admin
$ touch dynamodb-admin/Dockerfile
$ touch dynamodb-admin/.env
FROM node:10.16.3-alpine
RUN ["apk", "update"]
RUN ["npm", "install", "dynamodb-admin", "-g"]
EXPOSE 50727
CMD ["dynamodb-admin", "-p", "50727"]
In the environment variable DYNAMO_ENDPOINT
, specify the container service name of dynamodb and the port on the container side.
.env
DYNAMO_ENDPOINT=http://dynamodb:8000
AWS_REGION=ap-northeast-1
AWS_ACCESS_KEY_ID=ACCESS_ID
AWS_SECRET_ACCESS_KEY=ACCESS_KEY
The final docker-compose.yml file with this dyanamodb-admin looks like this:
docker-compose.yml
version: "3"
services:
dynamodb:
container_name: example-dynamodb
image: amazon/dynamodb-local
command: -jar DynamoDBLocal.jar -dbPath /home/dynamodblocal/data
volumes:
- ./persist/dynamodb:/home/dynamodblocal/data
ports:
- 50706:8000
dynamodb-admin:
container_name: example-dynamodb-admin
build: dynamodb-admin/
command: dynamodb-admin -p 8000
env_file: dynamodb-admin/.env
ports:
- 50727:8000
depends_on:
- dynamodb
mysql:
container_name: example-mysql
ports:
- 53306:3306
image: mysql:5.7
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
volumes:
- ./persist/mysql:/var/lib/mysql
restart: always
environment:
MYSQL_USER: example
MYSQL_PASSWORD: example
MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD: example
django:
container_name: example-django
build: .
volumes:
- .:/app
working_dir: /app
command: sh -c "./wait-for-it.sh db:3306; python3 manage.py runserver 0.0.0.0:8000"
env_file: .env
ports:
- 58080:8000
depends_on:
- mysql
- dynamodb
If [http: // localhost: 50727](http: // localhost: 50727) is displayed in the browser and the following is displayed, it is successful. You can do whatever you want with Create Table etc.
I will make a table appropriately. It was created.
Delete the container and restart it.
$ docker-compose down
$ docker-compose up -d
If you open [http: // localhost: 50727](http: // localhost: 50727) and the previous table remains, the persistence is also successful.
This repository will be published below. The tag is ** 1.0.0 ** as it may be updated in the future. https://github.com/Cohey0727/example_dynamodb Please use it if you like.