运行Docker容器

我们已经安装好docker,详情参见:http://blog.csdn.net/dylloveyou/article/details/53582596

下面运行2个docker容器(一个mysql,一个tomcat,并部署jfinal demo),感受一下docker的便捷
主机IP为:11.11.175.55

如下操作:
1) 下载 mysql 5.5 镜像

root@devstack-newton:~# docker pull mysql:5.5
5.5: Pulling from library/mysql
386a066cd84a: Pull complete 
827c8d62b332: Pull complete 
de135f87677c: Pull complete 
05822f26ca6e: Pull complete 
581edb358f3e: Pull complete 
df4ad3824469: Pull complete 
de3fccb307ff: Pull complete 
98bc471865b4: Pull complete 
0d19ce23241a: Pull complete 
5a3a76908528: Pull complete 
068c2a1c3d8e: Pull complete 
Digest: sha256:2aa365becf97739d924798b4304bbf6146467efd8b252b555f13bec28a12f8f9
Status: Downloaded newer image for mysql:5.5

2) 启动一个mysql容器,映射主机3306端口,root账户密码root

root@devstack-newton:~# docker run -d -p 3306:3306 --name test_mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.5
c2f2164554ace80264f047e7bb6cff590fe06afbee25bb47a9792f8a5bd44651

3)用mysql客户端连接测试

运行Docker容器_第1张图片

4)初始化jfinal demo数据库
demo中有一张数据库表

运行Docker容器_第2张图片

5) 下载 tomcat 7 镜像

root@devstack-newton:~# docker pull tomcat:7
7: Pulling from library/tomcat

75ea84187083: Pull complete 
3e2e387eb26a: Pull complete 
7c6c48f4275c: Pull complete 
887691f35f8f: Pull complete 
bdd1cefebf67: Pull complete 
598674c5963f: Pull complete 
cfa880bbc1a2: Pull complete 
d49a2408af9c: Pull complete 
e9a7bd396dd8: Pull complete 
1b3964849a69: Pull complete 
Digest: sha256:06c404f264fe60a62bf0e0485e18453ec81466863ff82439d7399a0437394d79
Status: Downloaded newer image for tomcat:7

6) 启动一个tomcat容器,映射主机8888端口,链接到mysql,并挂载本地目录

root@devstack-newton:~# docker run -d -p 8888:8080 --name test_tomcat  --link test_mysql:db -v /opt/data/tomcat/webapps/ROOT:/usr/local/tomcat/webapps/ROOT tomcat:7
786ef10d3d504f7b685a37c9560b434bb055e60acad975623cd62dd5410d5c09

7) 部署 jfinal demo 到 tomcat 容器,注意数据库配置文件要修改正确
把jfinal demo 文件复制到主机 /opt/data/tomcat/webapps/ROOT 下

root@devstack-newton:~# ls -al /opt/data/tomcat/webapps/ROOT
total 44
drwxr-xr-x 10 root root 4096 Dec 12 16:02 .
drwxr-xr-x  3 root root 4096 Dec 12 15:56 ..
drwxr-xr-x  2 root root 4096 Dec 12 16:02 blog
drwxr-xr-x  2 root root 4096 Dec 12 16:02 common
drwxr-xr-x  2 root root 4096 Dec 12 16:02 css
-rw-r--r--  1 root root 1150 Dec 12 16:02 favicon.ico
drwxr-xr-x  2 root root 4096 Dec 12 16:02 img
drwxr-xr-x  2 root root 4096 Dec 12 16:02 index
drwxr-xr-x  2 root root 4096 Dec 12 16:02 js
drwxr-xr-x  2 root root 4096 Dec 12 16:02 META-INF
drwxr-xr-x  4 root root 4096 Dec 12 16:02 WEB-INF

修改tomcat数据库连接

root@devstack-newton:~# cd /opt/data/tomcat/webapps/ROOT/WEB-INF/classes
root@devstack-newton:/opt/data/tomcat/webapps/ROOT/WEB-INF/classes# vi a_little_config.txt

jdbcUrl = jdbc:mysql://db/jfinal_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
user = root
password = root
devMode = true

我们启动tomcat容器时,加了link mysql数据库,别名为db

docker run -d -p 8888:8080 --name test_tomcat  --link test_mysql:db ...

所以数据库连接改为

jdbcUrl = jdbc:mysql://db/jfinal_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull

6)重启tomcat容器,测试

查看日志(docker logs test_tomcat ),已经显示正常启动

打开页面
测试部署成功
运行Docker容器_第3张图片

并可以进行blog的增删改查操作
运行Docker容器_第4张图片

上面的docker的各种操作,后续文章再详细解释。

初试docker,大家是不是觉得很简单啊。

你可能感兴趣的:(docker,云计算)