⽤docker从零开始部署web服务器并打包成镜像(⽅案1),
讲解过程超级详细
我们使⽤docker的初衷⽆外乎这么⼏个:
1、简化部署,开发环境通过镜像打包到服务器上,加载即可运⾏,免去很多的环境配置,缩短部署时长,减少部署差错;
2、快速复制,实现分布式部署,⼀个应⽤可以快速拷贝到N台服务器,也可以搭建镜像管理服务器⾃动维护;
3、增强沟通,开发⼈员、测试⼈员、业务⼈员之间可以更好的交流。
今天,使⽤最原始的⽅式,从零开始带您⼀步步搭建⾃⼰的基于java和tomcat的web服务器。总体步骤如下:
1、拉取ubuntu操作系统;
2、在ubuntu上安装和配置java的jdk,⽤的1.8版本,商业免费的哦
3、在ubuntu上安装和配置tomcat,⽤的9版
4、测试服务器是否正常,并把⾃⼰的web应⽤拷贝的容器中
5、把配置好的web服务器和⾃⼰的web应⽤打包成镜像
6、把镜像复制到其它机器上运⾏
======================================
步骤1:拉取ubuntu操作系统
qkm@qkm-X61:~/软件/AutoBrowser$ sudo docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
qkm@qkm-X61:~/软件/AutoBrowser$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 20.04 ba6acccedd29 2 months ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
qkm@qkm-X61:/$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
==============================================
qkm@qkm-X61:/$ sudo docker run -dit ubuntu:20.04
bc4374e42e58d33862e71bfc4dd01ec7dfcc1319912a2e2c92cb07f3ed5f2c2a
qkm@qkm-X61:/$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc4374e42e58 ubuntu:20.04 "bash" 5 seconds ago Up 4 seconds gallant_carver
qkm@qkm-X61:/$ docker attach bc4374e42e58
//指令执⾏后进⼊了容器,是⼀个ubuntu linux操作系统,相当于虚拟机
root@bc4374e42e58:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
root@bc4374e42e58:/# pwd
docker打包镜像/
root@bc4374e42e58:/# cd usr
root@bc4374e42e58:/usr# ls
bin games include lib lib32 lib64 libx32 local sbin share src
root@bc4374e42e58:/usr# cd ..
root@bc4374e42e58:/# cd home
root@bc4374e42e58:/home# ls
root@bc4374e42e58:/home# exit
//使⽤attach⽅式进⼊容器,退出时发现运⾏中的容器也被关闭了
=========================
qkm@qkm-X61:/home$ sudo docker run -dit ubuntu:20.04
0bf158ef0c6aad1ea17016b79839dd914ffe649dcce90f924093c0ad56d8503e
//
qkm@qkm-X61:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0bf158ef0c6a ubuntu:20.04 "bash" About a minute ago Up About a minute hungry_shtern qkm@qkm-X61:~$ docker exec -i -t 0bf158ef0c6a /bin/bash
root@0bf158ef0c6a:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
root@0bf158ef0c6a:/# cd home
root@0bf158ef0c6a:/home# ls
root@0bf158ef0c6a:/home# exit
exit
qkm@qkm-X61:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0bf158ef0c6a ubuntu:20.04 "bash" 4 minutes ago Up 4 minutes hungry_shtern
//使⽤exec⽅式进⼊容器,退出时可以看出运⾏中的容器仍在运⾏
==========================================================
步骤2:在ubuntu上安装和配置java的jdk
qkm@qkm-X61:/home$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0bf158ef0c6a ubuntu:20.04 "bash" 9 seconds ago Up 8 seconds hungry_shtern
//把java的jdk拷贝到容器中
qkm@qkm-X61:/home$ docker cp /home/qkm/下载/java/jdk/ 0bf158ef0c6a:/home //新开⼀个终端
qkm@qkm-X61:~$ docker exec -i -t 0bf158ef0c6a /bin/bash
root@0bf158ef0c6a:/# cd home
root@0bf158ef0c6a:/home# ls
root@0bf158ef0c6a:/home# cd /usr/local
root@0bf158ef0c6a:/usr/local# ls
bin etc games include lib man sbin share src
root@0bf158ef0c6a:/usr/local# mkdir java
root@0bf158ef0c6a:/usr/local# ls
bin etc games include java lib man sbin share src
root@0bf158ef0c6a:/usr/local# cd /home
root@0bf158ef0c6a:/home# ls
root@0bf158ef0c6a:/home# tar -zxvf -C /usr/local/java
jdk1.8.0_202/
jdk1.8.0_202/javafx-src.zip
..............
root@0bf158ef0c6a:/# apt-get update
.....................
//不做更新,装不了vim编辑器
root@0bf158ef0c6a:/# apt-get install vim
Reading Done
Building dependency tree
Reading Done
The following additional packages will be installed:
.....................
root@0bf158ef0c6a:/# vim /etc/profile
按i进⼊编辑模式,拷贝以下内容到⽂件中
export JAVA_HOME=/usr/local/java/jdk1.8.0_202 #这⾥与⾃⼰的jdk路经⼀致
export JRE_HOME=${JAVA_HOME}/jre #jdk路经⾥的jre⽂件夹
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib #jdk路经⾥的lib⽂件夹
export PATH=${JAVA_HOME}/bin:$PATH #jdk⾥的bin⽂件夹
按照顺序按键ESC,:,wq(x也可以)保存退出
输⼊下⾯命令使得变量⽣效
root@0bf158ef0c6a:/# source /etc/profile
root@0bf158ef0c6a:/# java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
root@0bf158ef0c6a:/#
安装和配置jdk可参考我之前发的⽂章:
==============================
步骤3:在ubuntu上安装和配置tomcat
在宿主机器终端窗⼝,把已下载好的tomcat9压缩包复制到运⾏的容器中
qkm@qkm-X61:/home$ docker cp /home/qkm/下载/apache-tomcat-9.0. 0bf158ef0c6a:/home 在容器中的终端窗⼝
root@0bf158ef0c6a:/# cd /usr/local
root@0bf158ef0c6a:/usr/local# mkdir tomcat
root@0bf158ef0c6a:/usr/local# cd /home
root@0bf158ef0c6a:/home# tar -zxvf apache-tomcat-9.0. -C /usr/local/tomcat
apache-tomcat-9.0.54/conf/
apache-tomcat-9.0.54/conf/catalina.policy
.............
root@0bf158ef0c6a:/home# cd /usr/local/tomcat
root@0bf158ef0c6a:/usr/local/tomcat# chmod 777 -R apache-tomcat-9.0.54
root@0bf158ef0c6a:/usr/local/tomcat# cd apache-tomcat-9.0.54
root@0bf158ef0c6a:/usr/local/tomcat/apache-tomcat-9.0.54# chmod +x ./bin/startup.sh
root@0bf158ef0c6a:/usr/local/tomcat/apache-tomcat-9.0.54# vim /etc/profile
//添加以下内容
export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.54
root@0bf158ef0c6a:/usr/local/tomcat/apache-tomcat-9.0.54# source /etc/profile
root@0bf158ef0c6a:/usr/local/tomcat/apache-tomcat-9.0.54# ./bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.54
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.54
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.54/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_202/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.54/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-
9.0.54/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
//下⾯步骤把服务加到⾃启动。经实测,还是需要在启动容器时运⾏⼀个启动脚本sh。
root@0bf158ef0c6a:/usr/local/tomcat/apache-tomcat-9.0.54# cp /usr/local/tomcat/apache-tomcat-9.0.54/bin/catalina.sh /etc/init.d/
root@0bf158ef0c6a:/usr/local/tomcat/apache-tomcat-9.0.54# cd /etc/init.d
root@0bf158ef0c6a:/etc/init.d# mv catalina.sh tomcat
root@0bf158ef0c6a:/etc/init.d# service tomcat start
Cannot find /etc/bin/setclasspath.sh
This file is needed to run this program
root@0bf158ef0c6a:/etc/init.d# chmod +x tomcat
root@0bf158ef0c6a:/etc/init.d# vim tomcat
root@0bf158ef0c6a:/etc/init.d# systemctl daemon-reload
bash: systemctl: command not found
root@0bf158ef0c6a:/etc/init.d# sudo systemctl daemon-reload
bash: sudo: command not found
root@0bf158ef0c6a:/etc/init.d# service tomcat start
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.54
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.54
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.54/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_202
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.54/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-
9.0.54/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
root@0bf158ef0c6a:/etc/init.d# cd ..
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论