Transferred from: http://m.oschina.net/blog/616526, copyright belongs to the original author.
1 Docker vs. virtual machines
2 Building process
2.1 Preparing the host system
Prepare a CentOS 7 operating system with the following requirements:
View your CentOS kernel using the following command:
# Uname-r
2.2 Installing Docker
# Yum Install Docker
You can use the following command to see if Docker is installed successfully:
# Docker version
If the version number of the Docker is output, the installation is successful and the Docker service can be started with the following command:
# Systemctl Start Docker.service
Once the Docker service is up and ready, you can start using Docker.
2.3 Download Image
Take CentOS, for example, to download a CentOS image:
# Docker Pull centos:7.2.1511
When the download is complete, use the command to view the local mirror list:
# docker Imagesrepository TAG IMAGE ID CREATED VIRTUAL sizedocker.io/centos 7.2.1511 83ee614b83 4e 9 weeks ago 194.6 MB
2.4 Starting the container
Containers are run on the basis of mirroring, and once the container is started, we can log in to the container and install the software or application we need.
Use the following command to start the container:
# docker Run-i-t-v/root/software/:/mnt/software/83ee/bin/bash
The command consists of the following three sections:
Docker run < related parameters > < mirroring id> < initial commands >
Among them, the relevant parameters include:
-I: Indicates that the container is running in "interactive mode"
-T: Indicates that the container will enter its command line when it is started
-V: Indicates which directory needs to be mounted to the container, format:-v < host directory >:< container directory >
In this example, all the installers are placed in the/root/software/directory of the host and now need to be mounted to the container's/mnt/software/directory.
# pwd/root/software# lsapache-tomcat-7.0.67.tar.gz jdk1.7.0_79.tar.gz
The initial command indicates the command to run once the container is started, using "/bin/bash" at this point, which means that the bash shell is entered directly after launch.
2.5 Installing the Software
In order to build the Java Web environment, the JDK and Tomcat need to be installed, and the following procedures are performed inside the container. In this example, selecting the/opt/directory as the installation directory requires first entering the directory through the cd/opt/command.
2.5.1 Installing the 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
Write a run script that, when the container is started, runs the script and starts Tomcat.
First, create a run script:
# touch/root/run.sh# Vi/root/run.sh
Then, edit the script content as follows:
#!/bin/bashexport java_home=/opt/jdk/export path= $JAVA _home/bin: $PATHsh/opt/tomcat/bin/catalina.sh Run
Finally, add execute permissions to run the script:
# chmod u+x/root/run.sh
2.6 Exiting the container
When all of the above steps are complete, you can use the Exit command to exit the container.
You can then use the following command to view the running container:
# docker PS
At this point, you should not see any programs that are running, because the container that you just exited with the Exit command is in a stopped state, and you can view all the containers by using the following command:
# docker Ps-acontainer ID IMAGE COMMAND CREATED STATUS 02bebc3f546a 83ee "/bin/bash" 12 Minutes ago Exited (0) 7 seconds ago
Remember the above container ID (container ID), which will then be used to create a running Tomcat image.
2.7 Creating a Tomcat image
Use the following command to create a new "mirror" based on a "container ID":
# Docker commit 02be mytomcat:1.065c88ec597e04812ec3b06b7749578bebcae3aa3d735b565ed25db6818d9d7f3# Docker Imagesrepository TAG IMAGE ID CREATED VIRTUAL Sizemytomcat 1.0 65c88ec597e0 About a minute ago 514.4 Mbdocker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 MB
The container's ID is 02be, the image name created is "mytomcat:1.0", and then the Tomcat container can be started using mirroring.
2.8 Launching the Tomcat container
First, create a new/root/webapps/root directory and a index.html file in that directory with the following file contents:
As described above, the container can be started with a "mirror name" or "Mirror ID", unlike the last boot container, which now no longer enters the container's command line, but instead directly launches the TOMCAT service inside the container. At this point, you need to use the following command:
# Docker run-d-P 58080:8080-v/root/webapps/:/opt/tomcat/webapps/--name mytomcat_1 mytomcat:1.0/root/run.sh
Among them, the relevant parameters include:
-D: The/root/run.sh script is executed in "daemon mode", at which point the Tomcat console does not appear on the output terminal.
-P: Represents the port mapping of the host to the container, at which point the 8080 port inside the container is mapped to the 58080 port of the host, exposing 58080 ports to the outside, and can access the 8080 ports inside the container via the Docker bridge.
-V: Indicates which directory needs to be mounted to the container, format:-v < host directory >:< container directory >
--name: Represents the container name and is named with a meaningful name.
In the browser, you can access Tomcat by entering the host IP and port number:
2.9 Final:
2.10 Stop Tomcat Container
# docker Ps-acontainer ID IMAGE COMMAND CREATED STATUS f23598b6544d mytomcat:1.0 "/root /run.sh "6 minutes ago up 6 minutes # Docker stop f235
2.11 Removing a container
# docker Ps-acontainer ID IMAGE COMMAND CREATED STATUS f23598b6544d Myto mcat:1.0 "/root/run.sh" 8 minutes ago Exited (137) # docker RM f235f235# Docker Ps-acontainer ID IMAGE COMMAND CREATED STATUS
2.12 Removing a mirror
# docker imagesrepository tag IMAGE ID CREATED VIRTUAL SIZEmytomcat 1.0 65c88ec597e0 31 minutes ago 514.4 MBdocker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 mb# docker rmi 65c8Untagged: mytomcat:1.0Deleted: 65c88ec597e04812ec3b06b7749578bebcae3aa3d735b565ed25db6818d9d7f3# docker imagesrepository tag image ID CREATED VIRTUAL SIZEdocker.io/centos 7.2.1511 83ee614b834e 9 weeks ago 194.6 mb
PS: This article refers to the Great God Blog: http://my.oschina.net/huangyong/blog/372491?fromerr=kHrZPM01 , and on the basis of the original text, some additions and modifications were made.
Use Docker to build TOMCAT runtime environment