docker-composeで開発用のredisとmysqlを構築する
今回は、docker-composeを使って開発用にredisとmysqlを使います。
最近は、docker-composeをサービスごとに作って利用しています。
docker-composeをインストール方法
brew caskでinstallできます。
brew install caskroom/cask/brew-cask brew cask install docker-compose
docker-composeの前にredisとmysqlのdockerfileの内容です。
Dockerfileの内容
とりあえず、自分が昔から使っているやつを貼っときます。 今は、docker公式のやつがあるのでそちらを使ってもいいかもしれません。
RedisのDockerfile
公式のやつでもいいです。 https://hub.docker.com/_/redis/
FROM centos:centos6 MAINTAINER oomatomo ooma0301@gmail.com # install package RUN rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm RUN yum update -y RUN yum install -y wget tar make gcc RUN cd /tmp && \ wget http://download.redis.io/redis-stable.tar.gz && \ tar xvzf redis-stable.tar.gz && \ cd redis-stable && \ make && \ make install && \ cp -f src/redis-sentinel /usr/local/bin && \ mkdir -p /etc/redis && \ cp -f *.conf /etc/redis # どこからもアクセスできるようにする RUN sed -e "s/# bind 127.0.0.1/bind 0.0.0.0/" -i /etc/redis/redis.conf EXPOSE 6379 CMD ["redis-server", "/etc/redis/redis.conf"]
mysqlのDockerfile
公式のやつでもいいです。 https://hub.docker.com/_/mysql/
FROM centos:centos6 MAINTAINER oomatomo ooma0301@gmail.com # install package RUN rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm RUN yum update -y RUN yum install -y wget tar # install mysql RUN wget http://dev.mysql.com/get/Downloads/MySQL-5.5/MySQL-5.5.46-1.el6.x86_64.rpm-bundle.tar RUN tar xvf MySQL-5.5.46-1.el6.x86_64.rpm-bundle.tar RUN yum localinstall -y MySQL-shared-compat-5.5.46-1.el6.x86_64.rpm RUN yum localinstall -y MySQL-shared-5.5.46-1.el6.x86_64.rpm RUN yum localinstall -y MySQL-server-5.5.46-1.el6.x86_64.rpm RUN yum localinstall -y MySQL-client-5.5.46-1.el6.x86_64.rpm RUN yum localinstall -y MySQL-devel-5.5.46-1.el6.x86_64.rpm # create mysql ADD privilege.development.sql /tmp/ RUN /etc/init.d/mysql start \ && mysql -u root < /tmp/privilege.development.sql EXPOSE 3306 CMD ["/usr/bin/mysqld_safe"]
privilege.development.sqlの内容
ユーザの作成を行っているだけです。
もしくは、サービスごとのSQLだったりします。
CREATE USER 'ooma'@'%' IDENTIFIED BY 'tomo'; GRANT ALL PRIVILEGES ON *.* TO 'ooma'@'%' WITH GRANT OPTION;
docker-composeの設定方法
docker-compose は、docker-compose.yml
というyml形式の設定ファイルを作成します。
とりあえず、最低限の設定の説明です。
[キー]: build: [dockerfileの場所] ports: - "[hostのポート]:[dockerのポート]"
実際の設定ファイル。
mysql: build: ./mysql ports: - "3306:3306" redis: build: ./redis ports: - "6379:6379"
docker-composeの使い方
docker build
# ビルド
docker-compose build
docker-compose build [キー] docker-compose build redis
# バックエンドでは、動かない
docker-compose up
redisのみ実行すると。。
$ docker-compose up redis Creating docker_redis_1... Attaching to docker_redis_1 redis_1 | _._ redis_1 | _.-``__ ''-._ redis_1 | _.-`` `. `_. ''-._ Redis 3.0.5 (00000000/0) 64 bit redis_1 | .-`` .-```. ```\/ _.,_ ''-._ redis_1 | ( ' , .-` | `, ) Running in standalone mode redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 redis_1 | | `-._ `._ / _.-' | PID: 1 redis_1 | `-._ `-._ `-./ _.-' _.-' redis_1 | |`-._`-._ `-.__.-' _.-'_.-'| redis_1 | | `-._`-._ _.-'_.-' | http://redis.io redis_1 | `-._ `-._`-.__.-'_.-' _.-' redis_1 | |`-._`-._ `-.__.-' _.-'_.-'| redis_1 | | `-._`-._ _.-'_.-' | redis_1 | `-._ `-._`-.__.-'_.-' _.-' redis_1 | `-._ `-.__.-' _.-' redis_1 | `-._ _.-' redis_1 | `-.__.-' redis_1 | redis_1 | 1:M 20 Oct 22:49:23.033 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. redis_1 | 1:M 20 Oct 22:49:23.033 # Server started, Redis version 3.0.5 redis_1 | 1:M 20 Oct 22:49:23.033 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. redis_1 | 1:M 20 Oct 22:49:23.033 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. redis_1 | 1:M 20 Oct 22:49:23.034 * The server is now ready to accept connections on port 6379 ←ここでCtrl+Cをやる ^CGracefully stopping... (press Ctrl+C again to force) Stopping docker_redis_1... done
docker run
# 全体をデーモンで実行 docker-compose up -d # mysqlのみデーモンとして実行 docker-compose up -d mysql # redisのみデーモンとして実行 docker-compose up -d redis
あと各ポートにアクセスします。
今まで、毎回 docker run ..
でやっていたのが、簡単に調整出来るのがいいです。