Preface
(1) Written earlier, since there is little experience in installing jdk+tomcat and other environments in linux or unix-like systems, we chose to install it in the docker container to prevent installation failure and failure to restore the system.
(2) We need to download the docker image of the corresponding system, such as centos, Ubuntu, etc.
(3) This article does not have the relevant environment instructions required for docker installation. You can view the official website and install it by yourself. Here is a summary.
1. Download the docker image of CENTOS
Mirror Download Address DaoCloud
[root@izwz99jhv6u546yu27nrwdz downloads]# docker pull centos
//slightly
- 1
- 2
When the download is complete, look at the downloaded image as follows: centos
[root@izwz99jhv6u546yu27nrwdz downloads]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
richarvey/nginx-php-fpm latest b45e0c814c3f 4 days ago 269 MB
golang latest 7afbc2b03b9e 2 weeks ago 675 MB
daocloud.io/library/nginx latest cc1b61406712 3 weeks ago 182 MB
nginx latest cc1b61406712 3 weeks ago 182 MB
php 7-fpm 65ec79b1c89d 3 weeks ago 377 MB
php latest 608e59384e11 3 weeks ago 364 MB
daocloud.io/library/ubuntu latest f49eec89601e 3 weeks ago 129 MB
centos latest 67591570dd29 2 months ago 192 MB
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2. Enter mirror centos for related operations
(1) Some necessary preparations, such as installation of vim, new directories needed, etc.
[root@izwz99jhv6u546yu27nrwdz downloads]# docker run -it centos /bin/bash [root@92896fb7e091 /]# yum install vim* ... (n lines are omitted here)... Complete! # Explains that vim installation was successful [root@92896fb7e091 /]# mkdir -p /home/wangjie # Description - Make a directory to be mounted on the left side Note: At this point, you need to remember that the container ID at this time is 92896fb7e091, 2 backup needs
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
(2) Backup the changes to the mirror above
[root@92896fb7e091 bin]# exit
exit
[root@izwz99jhv6u546yu27nrwdz ~]# docker commit -m "Added vim_lrzsz_wangjie" -a "Docker Newbee" 92896fb7e091 centos_vim_lrzsz
//Among them:
-m To specify the submission instructions, just like the version control tools we use;
-a You can specify updated user information;
92896fb7e091 It's a container for creating mirrors. ID;
centos_vim_lrzsz Is the name of the warehouse that mirrors the specified target(centos_vim_lrzsz)
//When created successfully, the ID of the new image is returned:
sha256:6ddbdb552b7db7f6cffdba3612664dc9bd25715e079aecefd98d5451ffb8ca35
[root@izwz99jhv6u546yu27nrwdz ~]# docker images
[root@izwz99jhv6u546yu27nrwdz webapps]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_vim_lrzsz latest 89796a5c0215 2 hours ago 362 MB
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
3. Make a directory on the host for mounting, that is, the directory of the container in the corresponding 1
[root@izwz99jhv6u546yu27nrwdz ~]# mkdir -p /home/wangjie/tomcat8 [root@izwz99jhv6u546yu27nrwdz ~]# cd /home/wangjie/tomcat8 Upload the downloaded JDK files and Tomcat to this directory through rz. Here I use tomcat-8.0.24 and jdk-8u91-linux-x64. You can change to other versions you want. [root@izwz99jhv6u546yu27nrwdz tomcat8]# ll total 186028 -rw-r--r-- 1 root root 9106353 Aug 25 14:47 apache-tomcat-8.0.24.tar.gz -rw-r--r-- 1 root root 181367942 May 5 2016 jdk-8u91-linux-x64.gz
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4. Re-enter the new container for related operations
(1) Start and mount the host directory / home/wangjie/tomcat8 under the container directory / home/wangjie
[root@izwz99jhv6u546yu27nrwdz tomcat8]# docker run -i -t -v /home/wangjie/tomcat8:/home/wangjie 89796a /bin/bash
//Among them:
-i Represented by"Interactive mode"Running container
-t Represents that the container will enter its command line when it starts
-v Represents which local directory needs to be mounted into the container in the format:-v <Host Absolute Directory>:<Container Absolute Catalogue>
89796a Namely, the new container ID,It can also be used."Container name:TAG"Only then
/bin/bash That is, after entering the container. bash shell Command Line Correspondence-t
[root@0bfc23706742 /]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
(2) Installing jdk and tomcat in containers
[root@0bfc23706742 /]# mkdir -p /opt/tomcat/
//Create a new directory to install tomcat
[root@0bfc23706742 /]# mkdir -p /opt/jdk/
//Create a new directory to install jdk
[root@0bfc23706742 /]# cd /home/wangjie
//Cut into the mount directory and view the files
[root@0bfc23706742 wangjie]# ls
apache-tomcat-8.0.24.tar.gz jdk-8u91-linux-x64.gz
//That is, the files in the host directory / home/wangjie/tomcat8
[root@0bfc23706742 wangjie]# tar -zxf apache-tomcat-8.0.24.tar.gz -C .
//Move tomcat after decompression
[root@0bfc23706742 wangjie]# mv apache-tomcat-8.0.24/ /opt/tomcat/
[root@0bfc23706742 wangjie]# cd /opt/tomcat/
[root@000308aec801 tomcat]# ll
total 4
drwxr-xr-x 9 root root 4096 Feb 22 02:18 apache-tomcat-8.0.24
[root@0bfc23706742 wangjie]# tar -zxf jdk-8u91-linux-x64.gz -C .
//jdk is moved after decompression
[root@0bfc23706742 wangjie]# mv jdk-8u91-linux-x64/ /opt/jdk/
[root@0bfc23706742 wangjie]# cd /opt/jdk/
[root@000308aec801 jdk]# ll
total 4
drwxr-xr-x 8 10 143 4096 Apr 1 2016 jdk1.8.0_91
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
(3) Writing running scripts
When the container runs, run the script and start tomcat [root@0bfc23706742 wangjie]# mkdir -p /root/run.sh [root@0bfc23706742 wangjie]# vim /root/run.sh #!/bin/bash export JAVA_HOME=/opt/jdk/jdk1.8.0_91 export PATH=$JAVA_HOME</span>/bin:<span class="hljs-variable">$PATH sh /opt/tomcat/apache-tomcat-8.0.24/bin/catalina.sh run Modify script execution permissions [root@0bfc23706742 wangjie]# chmod u+x /root/run.sh
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
(4) After the relevant operation. exit. Refer to the backup in 2 and create a new container to run tomcat's container
[root@0bfc23706742 wangjie]# exit
//[root@izwz99jhv6u546yu27nrwdz tomcat8]# docker commit -m "Added jdk_tomcat" -a "Docker Newbee" 0bfc23706742 centos_jdk_tomcat
//Here, for demonstration, add a new container for testing
[root@izwz99jhv6u546yu27nrwdz tomcat8]# docker commit 0bfc23706742 test_tomcat:1.0
[root@izwz99jhv6u546yu27nrwdz tomcat8]# docker images
//You can view all new images
REPOSITORY TAG IMAGE ID CREATED SIZE
test_tomcat 1.0 80f2fd55c5a0 2 hours ago 740 MB
centos_jdk_tomcat latest 38ac50abc54b 2 hours ago 740 MB
[root@izwz99jhv6u546yu27nrwdz tomcat8]# docker ps
//You can see that there are no running containers -- becauseexitAll containers stop.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@izwz99jhv6u546yu27nrwdz tomcat8]# docker ps -a
//You can view all newly added containers (running or stopped)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b44d32861981 test_tomcat:1.0 "/root/run.sh" 2 hours ago Exited (137) 51 minutes ago test_tomcat_1
0bfc23706742 89796a "/bin/bash" 2 hours ago Exited (0) 2 hours ago quirky_shaw
92896fb7e091 centos_vim_lrzsz:v1 "/bin/bash" 3 hours ago Exited (0) 3 hours ago practical_meitner
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
5. Prepare project files on the host and mount them in the docker environment and start
[root@izwz99jhv6u546yu27nrwdz tomcat8]# mkdir webapps
[root@izwz99jhv6u546yu27nrwdz tomcat8]# cd webapps
//rz uploads project code, taking jenkins.war as an example
[root@izwz99jhv6u546yu27nrwdz webapps]# ll
total 81140
-rw-r--r-- 1 root root 69874457 Feb 22 10:57 jenkins.war
- 1
- 2
- 3
- 4
- 5
- 6
Everything is ready to start running tomcat service
[root@izwz99jhv6u546yu27nrwdz webapps]# docker run -d -p 58080:8080 -v /home/wangjie/tomcat8/webapps/:/opt/tomcat/apache-tomcat-8.0.24/webapps/ --name test_tomcat_1 test_tomcat:1.0 /root/run.sh / / among them: - v slightly, as mentioned earlier - d means to execute the / root/run.sh script in "daemon mode" when the Tomcat console does not appear on the output terminal - p represents the port mapping between the host and the container. At this time, the 8080 port inside the container is mapped to the 58080 port of the host, which exposes the 58080 port to the outside world. The 8080 port inside the container can be accessed through the Docker bridge. Name means the name of the container. Name it with a name that you think makes sense. test_tomcat:1.0 is the new container name: TAG /root/run.sh is the script that needs to be executed [root@izwz99jhv6u546yu27nrwdz tomcat8]# docker ps You can see the running container.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Of course, you might ask if tomcat in the container started properly. What do you think of the log?
[root@izwz99jhv6u546yu27nrwdz webapps]# docker logs e25a7d9f4506 Description: docker logs [running container ID] (The tomcat run log in the n-line container is omitted here)...
- 1
- 2
- 3
6. Open Browser Access
- (1) Access- http://120.77.207.178:58080/jenkins
You can enter the initial page normally.
- (2) At this point, the first page may need to initialize admin's password (as shown above). Where does the password come from?
As you can see from the log in Step 5, just copy it and put it on the page. - (3) Jenkins is no longer mentioned here.
7. Other:
Question: "The docker image above only contains jdk and tomcat. What if there is a project (war package) that needs to connect to the database (such as mysql), or PHP, python, apache services?"
Answer: Connect the newly built container above and continue to add the relevant servers, of course, can not all be installed in a mirror, you can add a separate mirror, such as: mysql image, and then assemble with the mirror above...
To try...