2 분 소요


I think Docker is the best tool for setting up infrastructure elements as code. I’m going to use Docker to set up databases that are annoying to install directly on my computer. Looking for something I could install all at once and cleanly remove without wanting to mess up my computer, I ended up installing via docker-compose. I’ll need to learn more about codifying additional features and options as I use it. This post uses an M1 MacBook, so the Docker image versions might be slightly different. Use images appropriate for your environment.

MySQL

MySQL is one of the most widely used relational databases. I wrote a docker-compose that manages MySQL default settings and variables from 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 arm64v8/mariadb for the image because at the time of writing, there was no M1 version image for MySQL
  • Opened ports with the ports directive
  • Wrote authentication-related environment variables for the database in environment
  • Also added the same content in env_file as in environment, but you only need one of them
  • Used volumes to sync the virtual container’s internal storage with my local storage
  • Run command is docker-compose -f docker-compose.mysql.yml up -d
  • Either env_file or environment is sufficient.
  • 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 image provided by MongoDB
  • Enabled data sync with local using volumes configuration
  • Run command is docker-compose -f docker-compose.mongo.yml up -d
  • Here, all environment variables are fetched 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 image
  • Used command to start the Redis server. There are various execution options for Redis that can be written here
  • Run command is docker-compose -f docker-compose.redis.yml up -d

Using All at Once

You can bring up each one individually as shown above, but since docker-compose allows bringing up multiple containers at once, I wrote 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 location as docker-compose
  • Run command is docker-compose -f docker-compose.yml up -d
  • I’ve heard Redis and MongoDB can also be set up as clusters, which I need to research more

댓글남기기