본문으로 건너뛰기

데이터베이스를 docker-compose로 구축하기(mysql, mongodb, redis)

Docker는 인프라적인 요소들을 코드로 세팅 할 수 있는 가장 좋은 도구라고 생각한다. Docker를 사용해서 컴퓨터 내에 설치 하기 귀찮은(?) 데이터베이스들을 세팅 해 보려고 한다. 내 컴퓨터를 뭔가 더럽히고싶지 않은 생각에서 한번에 설치하고 깔끔하게 지울 수 있는 것을 찾다보니까 docker-compose로 설치를 하게 되었는데, 좀 더 많은 기능들이나 옵션들을 코드화 하는것은 사용 해 보면서 배워야겠다. 이 글은 M1 맥북을 사용하기 때문에 Docker에서 사용 하는 이미지의 버전이 조금 다를 수 있는데, 각자 환경에 알맞는 이미지를 사용 하면 될 것 같다.

I believe Docker is the best tool for setting up infrastructure components as code. I'm going to set up databases that are a hassle to install directly on my computer using Docker. Since I didn't want to clutter my computer, I looked for something that could be installed and cleanly removed at once, which led me to docker-compose. I'll need to learn more about additional features and options through hands-on experience. This article uses an M1 MacBook, so the Docker image versions might be slightly different. Please use images that suit your own environment.

Mysql

MySQL은 RDB 중 많이 사용하는 데이터베이스 중 하나이다. MySQL 기본설정과 변수들은 .env 파일에서 관리하는 docker-compose 를 작성 해 보았다.

MySQL is one of the most commonly used relational databases. I wrote a docker-compose file that manages MySQL 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

  • 서비스 이름을 mysql-docker로 지었다
  • image는 arm64v8/mariadb를 사용했는데, 코드를 쓴 시점에서 mysql의 m1버전 이미지가 없었던 것으로 기억한다
  • ports로 포트를 열어주었다
  • environment라는 곳에 데이터베이스에서 인증과 관련 된 환경변수들을 작성했다
  • env_file 도 동일하게 environment 안에 있는 내용들을 추가했는데, 둘중 하나만 작성 해 주어도 된다
  • volumes을 사용해서 가상화컨테이너 내부와 나의 로컬의 저장소를 연동 해 주었다
  • 실행 명령어는 docker-compose -f docker-compose.mysql.yml up -d
  • env_file 혹은 environment 둘 중 하나만 있어도 된다.
  • 아래는 .mysql_env 파일 내애 작성 된 내용이다
  • Named the service mysql-docker
  • Used arm64v8/mariadb for the image because, as I recall, there was no M1 version image for MySQL at the time of writing
  • Opened ports using the ports directive
  • Wrote authentication-related environment variables for the database in the environment section
  • Also added the same contents in env_file as in environment, but you only need to write one of them
  • Used volumes to sync storage between the virtual container and my local machine
  • The run command is 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

  • 마찬가지로, service 이름을 mongo-docker로 지었다
  • image는 mongo에서 제공하는 arm64v8버전을 사용했다
  • volumes 설정으로 로컬과 데이터를 연동할 수 있게 했다
  • 실행 명령어는 docker-compose -f docker-compose.mongo.yml up -d
  • 여기서는 모든 환경변수를 .mongo_env에서 가져와서 사용하도록 했다
  • Similarly, named the service mongo-docker
  • Used the arm64v8 version image provided by mongo
  • Enabled data synchronization with local storage through volumes configuration
  • The run command is docker-compose -f docker-compose.mongo.yml up -d
  • Here, all environment variables are sourced 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는 기본적으로 image에서 arm64v8를 지원한다
  • command로, redis서버를 시작하게 해주었다. 레디스에 대한 여러가지 실행옵션이 있는데 여기에 적어주면 된다
  • 실행 명령어는 docker-compose -f docker-compose.redis.yml up -d
  • Redis natively supports arm64v8 in its image
  • Used command to start the redis server. There are various execution options for Redis that can be specified here
  • The run command is docker-compose -f docker-compose.redis.yml up -d

한번에 모두 사용하기

Using All at Once

위와같이 하나씩만 올려도 되지만, docker-compose으로 여러가지 컨테이너를 한번에 올릴 수 있기 때문에 같이 모아서 작성 해 보았다

While you can run them one at a time as shown above, since docker-compose can launch multiple containers at once, 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
  • 각각 .env 파일들은 docker-compose와 동일한 위치에 놓고 사용했다
  • 실행 명령어는 docker-compose -f docker-compose.yml up -d
  • 레디스와 몽고디비는 클러스터 구성을 해서 올리기도 하던데 요거는 조금 더 연구 해 봐야곘다
  • Each .env file was placed in the same directory as docker-compose
  • The run command is docker-compose -f docker-compose.yml up -d
  • I've seen Redis and MongoDB deployed in cluster configurations, which I'll need to research more