Abracadabra

The first course of the Docker

Docker command:

  • docker images 查看本机所有镜像

  • docker pull NAME 从仓库下载镜像

  • docker run [-d -p 8080:80] or [-P] NAME 启动镜像(-d 后台运行 -p 端口映射 -P 随机映射)

  • docker exec [-i -t] NAME bash 进入容器并执行bash

  • docker ps 查看后台容器

  • docker stop ID 停止docker容器

  • docker restart ID 重启容器

Docker netowrk type: bridge

Docker port map: host(eth0:80) <–> dicker0(bridge) <–> docker container(eth0:80)

Build Docker

  • Dockerfile
  • docker build [-t] 建立Docker,指定TAG

A Dcokerfile example (based tomcat):

1
2
3
4
5
from tomcat
MAINTAINER ewan ewanlee@yeah.net
COPY jpress-web-newest.war /usr/local/tomcat/webapps

搭建第一个Web app

为了介绍方便,所以使用了开源的java实现的wordpress,也就是Jpress

[1]下载相应的war包,并存到工作目录下

[2]下载一个tomcat的Docker镜像docker pull tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Using default tag: latest
latest: Pulling from library/tomcat
cd0a524342ef: Pull complete
e39c3ffe4133: Pull complete
aac3320edf40: Pull complete
4d9e109682f7: Pull complete
0a59efcf9553: Pull complete
43a404e523e0: Pull complete
806f07b1dce8: Pull complete
0cad96dccb4c: Pull complete
04073e2a9145: Pull complete
d9e4bf4be89c: Pull complete
739005fdecc9: Pull complete
8bd03d99f1b2: Pull complete
d586afbd7622: Pull complete
Digest: sha256:88483873b279aaea5ced002c98dde04555584b66de29797a4476d5e94874e6de
Status: Downloaded newer image for tomcat:latest

[3]写一个Dockerfile,也就是之前的example

[4]建立镜像docker build -t jpress:latest .

结果如下:

1
2
3
4
5
6
7
8
9
10
11
Sending build context to Docker daemon 20.8 MB
Step 1 : FROM tomcat
---> d71978506e58
Step 2 : MAINTAINER ewan ewanlee@yeah.net
---> Running in dfa1902d1ea4
---> 956612ba6987
Removing intermediate container dfa1902d1ea4
Step 3 : COPY jpress-web-newest.war /usr/local/tomcat/webapps
---> dd6eecd741e7
Removing intermediate container 1fe7f943071b
Successfully built dd6eecd741e7

[5]下载一个mysql的docker镜像docker pull mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Using default tag: latest
latest: Pulling from library/mysql
cd0a524342ef: Already exists
d9c95f06c17e: Pull complete
46b2d578f59a: Pull complete
10fbc2bcc6e9: Pull complete
91b1a29c3956: Pull complete
5bf9316bd602: Pull complete
69bd23f08b55: Pull complete
4fb778132e94: Pull complete
6913628d7744: Pull complete
a477f36dc2e0: Pull complete
c954124ae935: Pull complete
Digest: sha256:e44b9a3ae88db013a3e8571a89998678ba44676ed4ae9f54714fd31e108f8b58
Status: Downloaded newer image for mysql:latest

[6]运行mysql并创建一个数据库

1
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=000000 -e MYSQL_DATABASE=jpress mysql

[7]运行自己建立的jpress镜像docker run -d -p 8888:8080 jpress

下面进行浏览器页面的配置,在浏览器输入localhost:8888将出现以下界面:

index

在地址栏后加入后缀jpress-web-newest

install

填写配置信息,注意服务器地址是docker0网卡的ip

config

结果

home

安装过程中出现了一个bug,就是在进行配置后我退出了,再次进去重新配置出错,最后发现原因是表前缀需要改一下,因为之前配置成功了,数据库中已经有了一个相同的表前缀​:P

是不是很方便,完全不用手动安装任何东西~

References

  1. http://www.imooc.com/learn/824