使用Docker创建两个容器的应用

  本教程展示如何使用两个Docker容器分别装载WordPress博客系统和MySQL数据库,Wordpress是来自于Tutum维护的可信容器。Docker的安装可见:在VirtualBox和Vagrant中安装Docker,或者直接下载boot2docker

首先,运行:

$ docker run -d -p 80 tutum/wordpress /run.sh

这将会下载wordpress的img,通过命令:

$ docker ps

会查询当前运行的Image中有tutum/wordpress:latest 。

这是WordPress 和 MySQL在一个容器中运行。

现在我们有了一个Image,下面看看如何修改它,让MySQL存在在另外一个Docker容器中,

以交互式运行wordpress:

$ docker run -i -t tutum/wordpress bash

查看run.sh脚本:

root@dfa24049c264:/# cat run.sh
#!/bin/bash
if [ ! -f /.mysql_admin_created ]; then
/create_mysql_admin_user.sh
fi
exec supervisord -n

 

现在我们不希望在这个容器中运行MySQL,通过下面方式修改:

root@dfa24049c264:/# rm /etc/supervisor/conf.d/supervisord-mysqld.conf
root@dfa24049c264:/# touch /.mysql_admin_created

在另外Docker窗口,运行docker ps会发现你的交互方式容器的ID;比如dfa24049c264

这样我们可以确认commit给文件系统我们刚才修改:

$ docker commit dfa24049c264 myuser/wordpress
852e9ccd1194f5abbcbfb13c61e54a31d5b716b20bec3d114437e824a39e168a

现在我们已经有了我们自己的tutum的 WordPress,不过没有激活MySQL ,我们来运行它,停止其他容器,启动这个新的image容器: myuser/wordpress.

$ docker stop dfa24049c264
$ docker stop 110631a70e76
$ docker run -d -p 80 myuser/wordpress /run.sh
f1bc5c3bd28aac9a9452fdaeafb527b9616e5167797d56654dee625479c8d70c
$ docker ps

当docker ps时,3306端口并没有像之前出现,说明mySQL未启动。

从Docker index获得我们自己的mySQL容器:

$ docker run -d -p 3306 tutum/mysql /run.sh
63a974062480cdcb93db788dc2ef72cb1f5cbfe2c180b9fdfb1b13c62753c106
$ docker ps

然后同样进入交互方式:

$ docker run -i -t tutum/mysql bash
root@a6fd073d27bb:/# cat run.sh
#!/bin/bash
if [ ! -f /.mysql_admin_created ]; then
/create_mysql_admin_user.sh
fi
exec supervisord -n

这里我们不需要任何改变,而是要获得数据库root用户名和密码,退出Docker会看到用户名和密码:

docker run -d -p 3306 tutum/mysql /run.sh
63a974062480cdcb93db788dc2ef72cb1f5cbfe2c180b9fdfb1b13c62753c106

运行docker ps找到mysql的容器id:63a974062480

$ docker logs 63a974062480
=> Creating MySQL admin user with random password
=> Done!
========================================================================
You can now connect to this MySQL Server using:

mysql -uadmin -pqa1N76pWAri9 -h -P

Please remember to change the above password as soon as possible!
MySQL user 'root' has no password but only allows local connections
========================================================================
2014-01-20 21:56:10,100 CRIT Supervisor running as root (no user in config file)
2014-01-20 21:56:10,111 WARN Included extra file "/etc/supervisor/conf.d/supervisord-mysqld.conf" during parsing
2014-01-20 21:56:10,330 INFO RPC interface 'supervisor' initialized
2014-01-20 21:56:10,330 WARN cElementTree not installed, using slower XML parser for XML-RPC
2014-01-20 21:56:10,330 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2014-01-20 21:56:10,330 INFO supervisord started with pid 1
2014-01-20 21:56:11,333 INFO spawned: 'mysqld' with pid 385
2014-01-20 21:56:12,536 INFO success: mysqld entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

进入mysql管理界面创建wordpress数据库
$ mysql -uadmin -pqa1N76pWAri9 -h 0.0.0.0 -P 49165 -e 'create database wordpress;'

确认
$ docker commit 63a974062480 myuser/mysql
c1c5e63d427cc533325e3870ab9ed439e8bfe2e95c333ec54657210708816bbd

现在我们可以停止之前的通用mysql 的Docker,运行我们自己的mysql的Docker:

$ docker stop 63a974062480
$ docker run -d -p 3306 myuser/mysql /run.sh

最后一步,绑定wordpress和mysql两个容器

$ docker run -i -t myuser/wordpress bash
root@f74cc171d7d5:/# apt-get install curl
root@f74cc171d7d5:/# curl -L https://gist.github.com/cardmagic/8530589/raw/21c5d2f0a4f661531502a705a797e0357d998c86/gistfile1.txt > /app/wp-config.php

开启另外一个Docker窗口,运行:

$ docker commit f74cc171d7d5 myuser/wordpress
$ docker stop f74cc171d7d5

最后,将两者连接一起:

$ export DB_PASSWORD=qa1N76pWAri9
$ docker run -e="DB_PASSWORD=$DB_PASSWORD" -link angry_bardeen:db -d -p 80 myuser/wordpress /run.sh
$ docker ps出现:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fb827d3c3a7 myuser/wordpress:latest /run.sh 2 seconds ago Up 1 seconds 0.0.0.0:49167->80/tcp ecstatic_thompson
5a840e255282 myuser/mysql:latest /run.sh 22 minutes ago Up 22 minutes 0.0.0.0:49166->3306/tcp angry_bardeen,ecstatic_thompson/db

通过浏览器或$ curl http://0.0.0.0:49167/wp-admin/install.php访问确认。

 

在VirtualBox和Vagrant中安装Docker

使用gradle生成Spring Boot应用的Docker Image

 

 

猜你喜欢