[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose

Build a DynamoDB and Django development environment with docker-compose

Introduction

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.

environment

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

Environment

Django and MySQL

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. スクリーンショット 2019-09-29 23.11.48.png You can do whatever you want with Create Table etc.

Confirmation of persistence

I will make a table appropriately. スクリーンショット 2019-09-29 23.18.51.png It was created. スクリーンショット 2019-09-29 23.19.02.png

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.

Finally

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.

Recommended Posts

[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Python] Build a Django development environment with Docker
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Build a development environment with Poetry Django Docker Pycharm
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Build Django + NGINX + PostgreSQL development environment with Docker
Build the fastest Django development environment with docker-compose
Build a Django development environment with Doker Toolbox
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Create a Todo app with Django ① Build an environment with Docker
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
[Linux] Build a jenkins environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
Create a simple Python development environment with VS Code and Docker
Build a C language development environment with a container
Go (Echo) Go Modules × Build development environment with Docker
Build a Django environment with Vagrant in 5 minutes
Build a virtual environment with pyenv and venv
Build a Kubernetes environment for development on Ubuntu
Quickly build a Python Django environment with IntelliJ
Build PyPy and Python execution environment with Docker
Build a mruby development environment for ESP32 (Linux)
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Building a Docker working environment for R and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a local development environment for Laravel6.X on Mac
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Build a python environment for each directory with pyenv-virtualenv
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
How to build a Django (python) environment on docker
Build a machine learning application development environment with Python
Build a python virtual environment with virtualenv and virtualenvwrapper
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to run a Django application on a Docker container (development and production environment)
Launch Django on a Docker container with docker-compose up
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a lightweight Fast API development environment using Docker
Build a numerical calculation environment with pyenv and miniconda3
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Build a Docker environment that can use PyTorch and JupyterLab
Build a machine learning scikit-learn environment with VirtualBox and Ubuntu
Build a Go development environment with VS Code's Remote Containers
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
Build GPU environment with GCP and kaggle official image (docker)
Build a Flask development environment at low cost using Docker
Build a go environment using Docker
Build a deb file with Docker
Build Mysql + Python environment with docker
Deploy a Django application with Docker
Build PyPy execution environment with Docker
Build a web application with Django
Rebuild Django's development environment with Docker! !! !! !!
Building a Docker working environment for R and Python 2: Japanese support
Build and test a CI environment for multiple versions of Python
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a local development environment for Lambda + Python using Serverless Framework
Build a Python environment on your Mac with Anaconda and PyCharm