docker-composeの設定サンプル

3 min read読了の目安(約2900字

Docker Compose でMySQL(MariaDB)とRedisを構築する

ファイル構成

.
├── docker-compose.yml
├── mariadb
│   ├── Dockerfile
│   └── mariadb.cnf
└── redis
    ├── Dockerfile
    └── redis.conf

docker-compose.yml

mariadb:
    build: docker/mariadb
    ports:
    - "3306:3306"
    expose:
    - 3306
    volumes:
        - "./db/:/docker-entrypoint-initdb.d"
        - "./var/lib/mysql:/var/lib/mysql"
        - "./var/log/mysql:/var/log/mysql"
    environment:
        - MYSQL_ROOT_PASSWORD=root
redis:
    build: docker/redis
    ports:
    - "6379:6379"
    expose:
    - 6379

mariadb/Dockerfile

FROM mariadb:latest
ADD mariadb.cnf /etc/mysql/conf.d/mariadb.cnf

mariadb/mariadb.cnf

# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf
[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8
[mysqld]
#
# * Character sets
#
# Default is Latin1, if you need UTF-8 set all this (also in client section)
#
character-set-server  = utf8
#collation-server      = utf8_general_ci
general_log=1
general_log_file=/var/log/mysql/general-query.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,TRADITIONAL #
max_connections=20
#[mariadb]
server-id=1
binlog_format = row
log-bin = /var/log/mysql/bin-log
log-error = /var/log/mysql/error.log
slow_query_log_file   = /var/log/mysql/slow-queries.log
default_storage_engine = MyISAM
binlog-ignore-db = mysql

redis/Dockerfile

FROM redis:latest
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

redis/redis.conf

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 1gb
maxmemory-policy noeviction
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes