Reprinted from: http://m.oschina.net/blog/616526, the copyright belongs to the original author.
1 Docker and virtual machines
2 Construction process
2.1 Prepare the host system
Prepare a CentOS 7 operating system, the specific requirements are as follows:
-
Must be a 64-bit operating system
-
It is recommended that the kernel be above 3.8
Check out your CentOS kernel with the command:
# uname -r
2.2 Install Docker
# yum install docker
You can use the following command to check if Docker is installed successfully:
# docker version
If the version number of Docker is output, the installation is successful. You can start the Docker service with the following command:
# systemctl start docker.service
Once the Docker service has started, it's time to start using Docker.
2.3 Download mirror
Take CentOS as an example, download a CentOS image:
# docker pull centos:7.2.1511
After the download is complete, use the command to view the list of local mirrors:
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB
2.4 Start the container
The container runs on the basis of the image. Once the container is started, we can log in to the container and install the software or applications we need.
The container can be started with the following command:
# docker run -i -t -v /root/software/:/mnt/software/ 83ee /bin/bash
The command consists of the following three parts:
docker run <相关参数> <镜像 ID> <初始命令>
Among them, the relevant parameters include:
-
-i: means run the container in "interactive mode"
-
-t: Indicates that the container will enter its command line after startup
-
-v: Indicates which local directory needs to be mounted into the container, format: -v <host directory>:<container directory>
In this example, all the installers are placed in the host's /root/software/ directory, and now they need to be mounted in the container's /mnt/software/ directory.
# pwd
/root/software
# ls
apache-tomcat-7.0.67.tar.gz jdk1.7.0_79.tar.gz
The initial command indicates the command that needs to be run once the container is started. In this case, "/bin/bash" is used to directly enter the bash shell after startup.
2.5 Install the software
In order to build the Java Web runtime environment, JDK and Tomcat need to be installed. The following processes are all performed inside the container. In this example, the /opt/ directory is selected as the installation directory. First, you need to enter the directory through the cd /opt/ command.
2.5.1 Install JDK
First, unpack the JDK package:
# tar -zxf /mnt/software/jdk1.7.0_79.tar.gz -C .
Then, move the JDK directory:
# mv jdk1.7.0_79/ /opt/jdk/
2.5.2 Installing Tomcat
First, unpack the Tomcat package:
# tar -zxf /mnt/software/apache-tomcat-7.0.67.tar.gz -C .
Then, move the Tomcat directory:
# mv apache-tomcat-7.0.67/ /opt/tomcat/
2.5.3 Writing run scripts
编写一个运行脚本,当启动容器时,运行该脚本,启动 Tomcat。
首先,创建运行脚本:
# touch /root/run.sh
# vi /root/run.sh
然后,编辑脚本内容如下:
#!/bin/bash
export JAVA_HOME=/opt/jdk/
export PATH=$JAVA_HOME/bin:$PATH
sh /opt/tomcat/bin/catalina.sh run
最后,为运行脚本添加执行权限:
# chmod u+x /root/run.sh
2.6 退出容器
当以上步骤全部完成后,可使用exit命令,退出容器。
随后,可使用如下命令查看正在运行的容器:
# docker ps
此时,应该看不到任何正在运行的程序,因为刚才已经使用exit命令退出的容器,此时容器处于停止状态,可使用如下命令查看所有容器:
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
02bebc3f546a 83ee "/bin/bash" 12 minutes ago Exited (0) 7 seconds ago
记住以上CONTAINER ID(容器 ID),随后将通过该容器,创建一个可运行 Tomcat 镜像。
2.7 创建Tomcat镜像
使用以下命令,根据某个“容器 ID”来创建一个新的“镜像”:
# docker commit 02be mytomcat:1.0
65c88ec597e04812ec3b06b7749578bebcae3aa3d735b565ed25db6818d9d7f3
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mytomcat 1.0 65c88ec597e0 About a minute ago 514.4 MB
docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB
该容器的ID是02be,所创建的镜像名是“mytomcat:1.0”,随后可使用镜像来启动Tomcat容器。
2.8 启动Tomcat容器
首先,新建/root/webapps/ROOT目录,并在该目录下创建一个index.html文件,文件内容如下:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
正如上面所描述的那样,可以通过“镜像名”或“镜像 ID”来启动容器,与上次启动容器不同的是,现在不再进入容器的命令行,而是直接启动容器内部的 Tomcat 服务。此时,需要使用以下命令:
# docker run -d -p 58080:8080 -v /root/webapps/:/opt/tomcat/webapps/ --name mytomcat_1 mytomcat:1.0 /root/run.sh
其中,相关参数包括:
-
-d:表示以“守护模式”执行/root/run.sh脚本,此时 Tomcat 控制台不会出现在输出终端上。
-
-p:表示宿主机与容器的端口映射,此时将容器内部的 8080 端口映射为宿主机的 58080 端口,这样就向外界暴露了 58080 端口,可通过 Docker 网桥来访问容器内部的 8080 端口了。
-
-v:表示需要将本地哪个目录挂载到容器中,格式:-v <宿主机目录>:<容器目录>
-
--name:表示容器名称,用一个有意义的名称命名即可。
在浏览器中,输入宿主IP和端口号,即可访问 Tomcat:
2.9 最终示意图:
2.10 停止Tomcat容器
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
f23598b6544d mytomcat:1.0 "/root/run.sh" 6 minutes ago Up 6 minutes
# docker stop f235
2.11 移除容器
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
f23598b6544d mytomcat:1.0 "/root/run.sh" 8 minutes ago Exited (137)
# docker rm f235
f235
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
2.12 移除镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mytomcat 1.0 65c88ec597e0 31 minutes ago 514.4 MB
docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB
# docker rmi 65c8
Untagged: mytomcat:1.0
Deleted: 65c88ec597e04812ec3b06b7749578bebcae3aa3d735b565ed25db6818d9d7f3
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB
PS:本文参考了大神博文:http://my.oschina.net/huangyong/blog/372491?fromerr=kHrZPM01,并在原文基础上,进行了部分添加修改。