2 분 소요


I believe Docker is the best tool for setting up infrastructure components as code. I’m going to use Docker to set up databases that are cumbersome to install directly on your computer. Since I didn’t want to clutter my computer with various installations, I looked for something that could be installed all at once and cleanly removed when needed, which led me to docker-compose. I’ll need to learn more about additional features and options as I continue using it. This post is written using an M1 MacBook, so the Docker image versions might be slightly different. Please use the appropriate images for your environment.

MySQL

MySQL is one of the most widely used relational databases. I’ve written a docker-compose file that manages MySQL’s basic settings and variables through a .env file.

  version: "3"
  services:
    mysql-docker:
      image: arm64v8/mariadb
      ports:
        - "3306:3306"
      environment:
        TZ: Asia/Seoul
        MYSQL_ROOT_PASSWORD: qwerqwer123
        MYSQL_DATABASE: paul
        MYSQL_USER: paul
        MYSQL_PASSWORD: qwerqwer123
      container_name: "docker-mysql"
      env_file: .mysql_env
      volumes:
        - /Users/wool/Database-docker/data/mysql:/var/lib/mysql

docker-compose.mysql.yml

  • Named the service mysql-docker
  • Used the arm64v8/mariadb image because, at the time of writing, there was no M1 version image for MySQL
  • Opened the ports using the ports directive
  • Wrote authentication-related environment variables for the database in the environment section
  • Also added the same content to env_file - you only need to use one of these two methods
  • Used volumes to sync storage between the virtual container and my local machine
  • Run command: docker-compose -f docker-compose.mysql.yml up -d
  • You only need either env_file or environment, not both.
  • Below is the content written in the .mysql_env file
      # .mysql_env
      MYSQL_HOST=localhost
      MYSQL_PORT=3306
      MYSQL_ROOT_PASSWORD=qwerqwer123
      MYSQL_DATABASE=paul
      MYSQL_USER=paul
      MYSQL_PASSWORD=qwerqwer123
    

MongoDB

  version: "3"
  services:
    mongo-docker:
      image: arm64v8/mongo
      ports:
        - "${MONGO_PORT}:27017"
      volumes:
        - /Users/wool/Database-docker/data/mongo:/data/db
      container_name: "docker-mongodb"
      env_file:
        - .mongo_env

docker-compose.mongo.yml

  • Similarly, named the service mongo-docker
  • Used the arm64v8 version provided by MongoDB
  • Configured volumes to sync data with local storage
  • Run command: docker-compose -f docker-compose.mongo.yml up -d
  • Here, all environment variables are loaded from .mongo_env
      MONGO_HOST=localhost
      MONGO_PORT=27017
      MONGO_INITDB_ROOT_USERNAME=root
      MONGO_INITDB_ROOT_PASSWORD=qwerqwer123
      MONGO_INITDB_DATABASE=mongo-test
    

Redis

version: "3"
services:
  redis-docker:
    image: redis:latest
    command: redis-server --port 6379
    container_name: "docker-redis"
    labels:
      - "name=redis"
      - "mode=standalone"
    volumes:
      - /Users/wool/Database-docker/data/redis:/data
    ports:
      - 6379:6379

docker-compose.redis.yml

  • Redis natively supports arm64v8 in its base image
  • Used the command directive to start the Redis server. There are various execution options for Redis that can be specified here
  • Run command: docker-compose -f docker-compose.redis.yml up -d

Using All Services Together

While you can spin up each service individually as shown above, docker-compose allows you to run multiple containers at once, so I’ve combined them all together.

version: "3"
services:
  mysql-docker:
    image: arm64v8/mariadb
    ports:
      - "3306:3306"
    environment:
      TZ: Asia/Seoul
      MYSQL_ROOT_PASSWORD: qwerqwer123
      MYSQL_DATABASE: paul
      MYSQL_USER: paul
      MYSQL_PASSWORD: qwerqwer123
    container_name: "docker-mysql"
    env_file: .mysql_env
    volumes:
      - /Users/wool/Database-docker/data/mysql:/var/lib/mysql

  mongo-docker:
    image: arm64v8/mongo
    ports:
      - "${MONGO_PORT}:27017"
    volumes:
      - /Users/wool/Database-docker/data/mongo:/data/db
    container_name: "docker-mongodb"
    env_file:
      - .mongo_env

  redis-docker:
    image: redis:latest
    command: redis-server --requirepass qwerqwer123 --port 6379
    container_name: "docker-redis"
    volumes:
      - /Users/wool/Database-docker/data/redis:/data
    labels:
      - "name=redis"
      - "mode=standalone"
    ports:
      - 6379:6379
  • Each .env file is placed in the same directory as the docker-compose file
  • Run command: docker-compose -f docker-compose.yml up -d
  • I’ve seen people set up Redis and MongoDB in cluster configurations, which I’ll need to research more

댓글남기기