gendosu流、dockerの使い方(Docker composeを使う)
dockerを開発環境で使うに当たって
前回「gendosu流、dockerの使い方」という内容を書きました。
その内容だと、
通常Dockerコンテナを手動で起動するには
まず、Dockerfileを作って自分好みにイメージを作成
作成したイメージからdocker runコマンドでコンテナ起動
たとえば、memcachedとmysqlとredisを使った一通りの構成を手動で起動しようとした場合
それぞれにイメージ作成やdocker run(start)をして起動していかないといけないです。
コンテナ間にリンクを張っている場合は起動順序も考慮する必要があります。
面倒ですね。
そこで、今回はDocker-composeを使ってみたいと思います。
こちらのツールはDockerのオフィシャルで配布されているツールになります。
簡単に言うと、複数コンテナの情報を1ファイルに集約して、そのファイルを元に
1コマンドで全コンテナを起動できるようにするツール
という事になると思います。
早速
インストール
Docker composeのインストールは
https://docs.docker.com/compose/install/
をみて作業を進めます。
curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
これでdocker-composeというコマンドが入りました。
docker-composeを使うには
コマンドを実行するディレクトリ配下にdocker-compose.ymlというファイルを作成します。
※ディレクトリ構成
example/ Dockerfile supervisor/ sshd.conf docker-compose.yml
今回は前回の構成
webとmysql
という2つのコンテナを起動するような構成でファイルを作ってみたいと思います。
Dockerfile(supervisordのディレクトリも)は前回の物を流用します。
# Dockerfile
# sshd # # VERSION 0.0.1 FROM ubuntu:14.04.1 MAINTAINER gendosu "gendosu@hoge.hoge" RUN apt-get update RUN apt-get install -y openssh-server supervisor vim build-essential git RUN mkdir -p /var/run/sshd RUN sed -ibak -e "s/^PermitRootLogin.*$/PermitRootLogin yes/" /etc/ssh/sshd_config ADD supervisord/sshd.conf /etc/supervisor/conf.d/sshd.conf RUN echo 'root:root' |chpasswd
EXPOSEとCMDは、次のdocker-compose.ymlで設定するので、ここでは設定してません。
# docker-compose.yml
web: build: . expose: - "22" ports: - "2200:22" - "8000:8000" links: - mysql command: "/usr/bin/supervisord" mysql: image: mysql:latest environment: - "MYSQL_ROOT_PASSWORD=root" expose: - "3306"
これで設定ファイルの準備が完了しました。
起動はdocker-composeにオプションを指定して実行します。
docker-composeのオプションで、代表的な物は
- up
コンテナをイメージから作り直して起動する - stop
すでに起動しているコンテナを終了する - start
停止しているコンテナを起動させる
あたりかと思います。
起動してみます。
docker-compose up
upというオプションを指定して起動させました。
Dockerfileのビルドが走った後に
各イメージからコンテナが生成されて起動された事が確認できると思います。
[hoge@ubuntu14 example]$ docker-compose up Creating blogexample_mysql_1... Creating blogexample_web_1... Building web... Step 0 : FROM ubuntu:14.04.1 ---> 5ba9dab47459 Step 1 : MAINTAINER gendosu "gendosu@hoge.hoge" ---> Running in 85ba9b557f33 ---> 22226c38f3e4 Removing intermediate container 85ba9b557f33 Step 2 : RUN apt-get update ---> Running in 8c0ae015cd66 Ign http://archive.ubuntu.com trusty InRelease Ign http://archive.ubuntu.com trusty-updates InRelease Ign http://archive.ubuntu.com trusty-security InRelease Hit http://archive.ubuntu.com trusty Release.gpg Get:1 http://archive.ubuntu.com trusty-updates Release.gpg [933 B] Get:2 http://archive.ubuntu.com trusty-security Release.gpg [933 B] Hit http://archive.ubuntu.com trusty Release Get:3 http://archive.ubuntu.com trusty-updates Release [63.5 kB] Get:4 http://archive.ubuntu.com trusty-security Release [63.5 kB] Get:5 http://archive.ubuntu.com trusty/main Sources [1335 kB] Get:6 http://archive.ubuntu.com trusty/restricted Sources [5335 B] Get:7 http://archive.ubuntu.com trusty/universe Sources [7926 kB] Get:8 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB] Get:9 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB] Get:10 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB] Get:11 http://archive.ubuntu.com trusty-updates/main Sources [248 kB] Get:12 http://archive.ubuntu.com trusty-updates/restricted Sources [2310 B] Get:13 http://archive.ubuntu.com trusty-updates/universe Sources [141 kB] Get:14 http://archive.ubuntu.com trusty-updates/main amd64 Packages [636 kB] Get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [15.1 kB] Get:16 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [356 kB] Get:17 http://archive.ubuntu.com trusty-security/main Sources [97.7 kB] Get:18 http://archive.ubuntu.com trusty-security/restricted Sources [1874 B] Get:19 http://archive.ubuntu.com trusty-security/universe Sources [23.3 kB] Get:20 http://archive.ubuntu.com trusty-security/main amd64 Packages [329 kB] Get:21 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB] Get:22 http://archive.ubuntu.com trusty-security/universe amd64 Packages [128 kB] Fetched 20.7 MB in 44s (468 kB/s) ・・・・・・省略・・・・・・ Processing triggers for ureadahead (0.100.0-16) ... ---> 743fff369ae2 Removing intermediate container 571a02d59862 Step 4 : RUN mkdir -p /var/run/sshd ---> Running in 0cfacb2f5003 ---> 17d915718b45 Removing intermediate container 0cfacb2f5003 Step 5 : RUN sed -ibak -e "s/^PermitRootLogin.*$/PermitRootLogin yes/" /etc/ssh/sshd_config ---> Running in 286e7bec5f6a ---> cd1a199636fd Removing intermediate container 286e7bec5f6a Step 6 : ADD supervisord/supervisor.conf /etc/supervisor/conf.d/supervisor.conf ---> c6e7a05e7617 Removing intermediate container 4d3479c14506 Step 7 : ADD supervisord/sshd.conf /etc/supervisor/conf.d/sshd.conf ---> 28a0965a679f Removing intermediate container 3929e4530c19 Step 8 : RUN echo 'root:root' |chpasswd ---> Running in a4ae8fb235a9 ---> e398da4db568 Removing intermediate container a4ae8fb235a9 Successfully built e398da4db568 Attaching to blogexample_mysql_1, blogexample_web_1 mysql_1 | Running mysql_install_db ... mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Using atomics to ref count buffer pool pages mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: The InnoDB memory heap is disabled mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Memory barrier is not used mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Compressed tables use zlib 1.2.7 mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Using Linux native AIO mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Using CPU crc32 instructions mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Initializing buffer pool, size = 128.0M mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Completed initialization of buffer pool mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created! mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Database physically writes the file full: wait... mysql_1 | 2015-04-25 15:39:22 13 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB mysql_1 | 2015-04-25 15:39:23 13 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 mysql_1 | 2015-04-25 15:39:24 13 [Warning] InnoDB: New log files created, LSN=45781 mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Doublewrite buffer not found: creating new mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Doublewrite buffer created mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: 128 rollback segment(s) are active. mysql_1 | 2015-04-25 15:39:24 13 [Warning] InnoDB: Creating foreign key constraint system tables. mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Foreign key constraint system tables created mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Creating tablespace and datafile system tables. mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Tablespace and datafile system tables created. mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Waiting for purge to start mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: 5.6.23 started; log sequence number 0 mysql_1 | 2015-04-25 15:39:24 13 [Note] Binlog end mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: FTS optimize thread exiting. mysql_1 | 2015-04-25 15:39:24 13 [Note] InnoDB: Starting shutdown... mysql_1 | 2015-04-25 15:39:26 13 [Note] InnoDB: Shutdown completed; log sequence number 1625977 mysql_1 | Installing MySQL system tables...OK mysql_1 | mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Using atomics to ref count buffer pool pages mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: The InnoDB memory heap is disabled mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Memory barrier is not used mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Compressed tables use zlib 1.2.7 mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Using Linux native AIO mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Using CPU crc32 instructions mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Initializing buffer pool, size = 128.0M mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Completed initialization of buffer pool mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Highest supported file format is Barracuda. mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: 128 rollback segment(s) are active. mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Waiting for purge to start mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: 5.6.23 started; log sequence number 1625977 mysql_1 | 2015-04-25 15:39:26 36 [Note] Binlog end mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: FTS optimize thread exiting. mysql_1 | 2015-04-25 15:39:26 36 [Note] InnoDB: Starting shutdown... mysql_1 | 2015-04-25 15:39:28 36 [Note] InnoDB: Shutdown completed; log sequence number 1625987 mysql_1 | Filling help tables...OK mysql_1 | mysql_1 | To start mysqld at boot time you have to copy mysql_1 | support-files/mysql.server to the right place for your system mysql_1 | mysql_1 | PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! mysql_1 | To do so, start the server, then issue the following commands: mysql_1 | mysql_1 | /usr/bin/mysqladmin -u root password 'new-password' mysql_1 | /usr/bin/mysqladmin -u root -h 2686e18932b3 password 'new-password' mysql_1 | mysql_1 | Alternatively you can run: mysql_1 | mysql_1 | /usr/bin/mysql_secure_installation mysql_1 | mysql_1 | which will also give you the option of removing the test mysql_1 | databases and anonymous user created by default. This is mysql_1 | strongly recommended for production servers. mysql_1 | mysql_1 | See the manual for more instructions. mysql_1 | mysql_1 | You can start the MySQL daemon with: mysql_1 | mysql_1 | cd /usr ; /usr/bin/mysqld_safe & mysql_1 | mysql_1 | You can test the MySQL daemon with mysql-test-run.pl mysql_1 | mysql_1 | cd mysql-test ; perl mysql-test-run.pl mysql_1 | mysql_1 | Please report any problems at http://bugs.mysql.com/ mysql_1 | mysql_1 | The latest information about MySQL is available on the web at mysql_1 | mysql_1 | http://www.mysql.com mysql_1 | mysql_1 | Support MySQL by buying support/licenses at http://shop.mysql.com mysql_1 | mysql_1 | WARNING: Found existing config file /usr/my.cnf on the system. mysql_1 | Because this file might be in use, it was not replaced, mysql_1 | but was used in bootstrap (unless you used --defaults-file) mysql_1 | and when you later start the server. mysql_1 | The new default config file was created as /usr/my-new.cnf, mysql_1 | please compare it with your file and take the changes you need. mysql_1 | mysql_1 | WARNING: Default config file /etc/mysql/my.cnf exists on the system mysql_1 | This file will be read by default by the MySQL server mysql_1 | If you do not want to use this, either remove it, or use the mysql_1 | --defaults-file argument to mysqld_safe when starting the server mysql_1 | mysql_1 | Finished mysql_install_db mysql_1 | 2015-04-25 15:39:29 1 [Note] Plugin 'FEDERATED' is disabled. mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Using atomics to ref count buffer pool pages mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: The InnoDB memory heap is disabled mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Memory barrier is not used mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Compressed tables use zlib 1.2.7 mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Using Linux native AIO mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Using CPU crc32 instructions mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Initializing buffer pool, size = 128.0M mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Completed initialization of buffer pool mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Highest supported file format is Barracuda. mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: 128 rollback segment(s) are active. mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: Waiting for purge to start mysql_1 | 2015-04-25 15:39:29 1 [Note] InnoDB: 5.6.23 started; log sequence number 1625987 mysql_1 | 2015-04-25 15:39:29 1 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 43127239-eb61-11e4-8711-0242ac110002. mysql_1 | 2015-04-25 15:39:29 1 [Note] Server hostname (bind-address): '*'; port: 3306 mysql_1 | 2015-04-25 15:39:29 1 [Note] IPv6 is available. mysql_1 | 2015-04-25 15:39:29 1 [Note] - '::' resolves to '::'; mysql_1 | 2015-04-25 15:39:29 1 [Note] Server socket created on IP: '::'. mysql_1 | 2015-04-25 15:39:29 1 [Note] Event Scheduler: Loaded 0 events mysql_1 | 2015-04-25 15:39:29 1 [Note] Execution of init_file '/tmp/mysql-first-time.sql' started. mysql_1 | 2015-04-25 15:39:29 1 [Note] Execution of init_file '/tmp/mysql-first-time.sql' ended. mysql_1 | 2015-04-25 15:39:29 1 [Note] mysqld: ready for connections. mysql_1 | Version: '5.6.23' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) web_1 | /usr/lib/python2.7/dist-packages/supervisor/options.py:295: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security. web_1 | 'Supervisord is running as root and it is searching ' web_1 | 2015-04-25 15:43:01,143 CRIT Supervisor running as root (no user in config file) web_1 | 2015-04-25 15:43:01,143 WARN Included extra file "/etc/supervisor/conf.d/sshd.conf" during parsing web_1 | 2015-04-25 15:43:01,143 WARN Included extra file "/etc/supervisor/conf.d/supervisor.conf" during parsing web_1 | 2015-04-25 15:43:01,172 INFO RPC interface 'supervisor' initialized web_1 | 2015-04-25 15:43:01,172 CRIT Server 'unix_http_server' running without any HTTP authentication checking web_1 | 2015-04-25 15:43:01,173 INFO supervisord started with pid 1 web_1 | 2015-04-25 15:43:02,180 INFO spawned: 'sshd' with pid 7 web_1 | 2015-04-25 15:43:03,185 INFO success: sshd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
あとは、前回同様、ポート2200を指定してsshすることでwebコンテナへログインする事が出来ます。