什么是容器数据卷
如果数据都放在容器中,那么容器删除,数据就会丢失!需求:数据可以持久化
MySQL,容器删除了,删库跑路!需求:MySQL 数据可以存储在本地
容器之间可以有一个数据共享技术,Docker 容器中产生的数据,同步到本地
目录挂载:将容器内的目录,挂在到 Linux 上面
总结:容器的持久化和同步操作!容器也是可以数据共享的
方式一:直接使用命令来挂载 -v
docker run -it -v [主机目录]:[容器内目录] -p
# 测试
[root@vinjcent home]# docker run -it -v /home/test:/home centos /bin/bash
# 启动起来时可以通过 docker inspect
[root@vinjcent /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
4282d4a6ae1d centos "/bin/bash" 2 minutes
[root@vinjcent /]# docker inspect 4282d4a6ae1d
特点:在本地修改能够同步到容器内,无论容器是否在运行都能自动同步
# 获取 mysql 镜像
[root@vinjcent ~]# dokcer pull mysql:5.7
# 运行容器,需要做数据挂载 安装 mysql,需要配置密码的
# 官方命令
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
# 启动 mysql 容器
-d 后台运行
-p 端口映射
-v 卷挂载
-e 环境配置
--name 容器名字
[root@vinjcent ~]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
# 启动成功后,在本地使用SQLyog测试连接
在本地可视化数据库,创建一个表,同时也能将创建的数据同步到容器中
# 匿名挂载
-v 容器内路径
[root@vinjcent data]# docker run -d -P --name nginx01 -v /etc/nginx nginx
# 查看所有卷的情况
[root@vinjcent data]# docker volume ls
DRIVER VOLUME NAME
local 12c904b5a8bfd00f2720f25fe7d2eaf4f070036b6cda36075b58402c467a8a15
local edc30e3065505aa2f0539bfd0b1a142358f2fb73016d418fc2cbccbc42fae8d6
# 具名挂载
[root@vinjcent data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
7a614b0e53a1e8a79f89c4372142d979e5747cb94f4e6319242477dee70d2db2
[root@vinjcent data]# docker volume ls
DRIVER VOLUME NAME
local 12c904b5a8bfd00f2720f25fe7d2eaf4f070036b6cda36075b58402c467a8a15
local edc30e3065505aa2f0539bfd0b1a142358f2fb73016d418fc2cbccbc42fae8d6
local juming-nginx
# 通过 -v 卷名:容器内路径
[root@vinjcent data]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2022-06-17T11:09:55+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
所有的 docker 容器内的卷,没有指定目录的情况下都是在 /var/lib/docker/volumes/xxx
目录下
[root@vinjcent /]# cd /var/lib/docker/
[root@vinjcent docker]# ls
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
[root@vinjcent docker]# cd volumes/
[root@vinjcent volumes]# ls
12c904b5a8bfd00f2720f25fe7d2eaf4f070036b6cda36075b58402c467a8a15 juming-nginx
backingFsBlockDev metadata.db
edc30e3065505aa2f0539bfd0b1a142358f2fb73016d418fc2cbccbc42fae8d6
[root@vinjcent volumes]# cd juming-nginx/
[root@vinjcent juming-nginx]# ls
_data
[root@vinjcent juming-nginx]# cd _data/
[root@vinjcent _data]# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
我们通过具名挂载可以方便的找到一个卷,大多数情况下在使用具名挂载
# 如何确定是具名挂载还是匿名挂载,区别在于有没有指定路径挂载
-v 容器内路径 # 匿名挂载
-v /宿主机路径:容器内路径 # 指定路径挂载
扩展
# 通过 -v 容器内路径:ro / rw 改变读写权限
# ro readonly
# rw readwrite
# 一旦设置了容器权限,容器对我们挂载出来的内容就有限制了
[root@vinjcent /]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
[root@vinjcent /]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器其内部是无法操作的
Dockerfile 就是用来构建 docker 镜像的构建文件,通过命令脚本可以生成镜像
镜像是一层一层的,脚本一个个的命令
测试
# 创建一个dockerfile文件,名字可以自定义,建议Dockerfile
# 文件中的内容(指令)是大写 参数
[root@vinjcent docker-test-volume]# vim dockerfile1
# dockerfile1文件内容
From centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
# 这里的每一个命令,就是镜像的一层
# 生成自定义挂载卷镜像
[root@vinjcent docker-test-volume]# docker build -f dockerfile1 -t vinjcent/centos .
# 查看自定义数据卷的目录
这个卷和外部一定有一个同步的目录
查看卷挂载的路径
[root@vinjcent /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2ef0408cb446 34f69aa4a6c2 "/bin/bash" 59 seconds ago Up 58 seconds dreamy_aryabhata
7a614b0e53a1 nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx02
942c1ebebb2d nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx01
# 通过查看容器内部详细信息,可以看到容器外的挂载路径
[root@vinjcent /]# docker inspect 2ef0408cb446
测试文件是否同步
这张方式在未来使用十分多,通常会构建自己的镜像
假设构建镜像时候没有挂载卷,需要手动镜像挂载 -v
两个 mysql 数据同步
# 启动3个容器,通过自定义的镜像启动
# 列出所有镜像
[root@vinjcent docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
vinjcent/centos latest 34f69aa4a6c2 51 minutes ago 231MB
tomcat1.0 1.0 42230d410916 20 hours ago 685MB
redis latest 604d80444252 3 days ago 117MB
tomcat 9.0 ae6026892279 7 days ago 680MB
nginx latest 0e901e68141f 2 weeks ago 142MB
mysql 5.7 2a0961b7de03 2 weeks ago 462MB
elasticsearch 7.17.4 d64cccab426e 4 weeks ago 630MB
portainer/portainer latest 12b0b8dced14 5 weeks ago 75.4MB
centos latest 5d0da3dc9764 9 months ago 231MB
# 启动自定义的 centos 容器,命名为docker01
[root@vinjcent docker-test-volume]# docker run -it --name docker01 vinjcent/centos
# 启动自定义的 centos 容器,命名为docker02
[root@vinjcent /]# docker run -it --name docker02 --volumes-from docker01 vinjcent/centos
# =====================================docker01===============================================
[root@vinjcent docker-test-volume]# docker run -it --name docker01 vinjcent/centos
[root@d225885c80e5 /]# ls -l
total 0
lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Jun 17 08:24 dev
drwxr-xr-x. 1 root root 66 Jun 17 08:24 etc
drwxr-xr-x. 2 root root 6 Nov 3 2020 home
lrwxrwxrwx. 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------. 2 root root 6 Sep 15 2021 lost+found
drwxr-xr-x. 2 root root 6 Nov 3 2020 media
drwxr-xr-x. 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x. 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x. 257 root root 0 Jun 17 08:24 proc
dr-xr-x---. 2 root root 162 Sep 15 2021 root
drwxr-xr-x. 11 root root 163 Sep 15 2021 run
lrwxrwxrwx. 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x. 13 root root 0 Jun 17 02:03 sys
drwxrwxrwt. 7 root root 171 Sep 15 2021 tmp
drwxr-xr-x. 12 root root 144 Sep 15 2021 usr
drwxr-xr-x. 20 root root 262 Sep 15 2021 var
drwxr-xr-x. 2 root root 6 Jun 17 08:24 volume01
drwxr-xr-x. 2 root root 6 Jun 17 08:24 volume02
[root@d225885c80e5 /]# cd volume01
[root@d225885c80e5 volume01]# ls
[root@d225885c80e5 volume01]# touch docker01
# =====================================docker02===============================================
[root@vinjcent /]# docker run -it --name docker02 --volumes-from docker01 vinjcent/centos
[root@25d35c3faa4a /]# ls -l
total 0
lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Jun 17 08:27 dev
drwxr-xr-x. 1 root root 66 Jun 17 08:27 etc
drwxr-xr-x. 2 root root 6 Nov 3 2020 home
lrwxrwxrwx. 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------. 2 root root 6 Sep 15 2021 lost+found
drwxr-xr-x. 2 root root 6 Nov 3 2020 media
drwxr-xr-x. 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x. 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x. 260 root root 0 Jun 17 08:27 proc
dr-xr-x---. 2 root root 162 Sep 15 2021 root
drwxr-xr-x. 11 root root 163 Sep 15 2021 run
lrwxrwxrwx. 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x. 13 root root 0 Jun 17 02:03 sys
drwxrwxrwt. 7 root root 171 Sep 15 2021 tmp
drwxr-xr-x. 12 root root 144 Sep 15 2021 usr
drwxr-xr-x. 20 root root 262 Sep 15 2021 var
drwxr-xr-x. 2 root root 6 Jun 17 08:24 volume01
drwxr-xr-x. 2 root root 6 Jun 17 08:24 volume02
[root@25d35c3faa4a /]# cd volume01
[root@25d35c3faa4a volume01]# ls
[root@25d35c3faa4a volume01]# ls
docker01
### --volumes-from ===> 同样的也实现了容器的数据的共享,每个容器都是隔离的,不管是在哪一方都能够看见数据操作后的结果
# 将其中一个容器删除之后,数据不会导致另外一个共享数据容器的数据也随之消失,实现了备份的效果
[root@vinjcent docker-test-volume]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25d35c3faa4a vinjcent/centos "/bin/sh -c /bin/bash" 52 minutes ago Up 52 minutes docker02
d225885c80e5 vinjcent/centos "/bin/sh -c /bin/bash" 55 minutes ago Up 55 minutes docker01
7a614b0e53a1 nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx02
942c1ebebb2d nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx01
[root@vinjcent docker-test-volume]# docker rm -f d225885c80e5
d225885c80e5
多个 mysql 实现数据共享
### mysql01
[root@vinjcent ~]# docker run -d -p 3310:3306 -v /etc/mysql/conf -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
### mysql02
[root@vinjcent ~]# docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volume-from mysql01 mysql:5.7
# 这个时候可以实现两个容器数据同步
结论:
容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有人使用为止
但是一旦持久化到本地,本地的数据是不会删除的
dockerfile 是用来构建docker镜像的文件,命令参数脚本
构建步骤:
很多官方镜像都是基础包,很多功能都不具有,需要我们手动去搭建
基础知识
dockerfile 是面向开发的,以后要发布项目,做镜像,就需要编写 dockerfile 文件,这个文件十分简单
Docker 镜像逐渐成为企业交付的标准
DockerFile:构建文件,定义了一切的步骤,源代码
DockerImages:通过 DockerFile 构建生成的镜像,最终发布和运行的产品
Docker容器:容器就是镜像运行起来提供服务器
FROM # 基础镜像,一切从这里开始构建
MAINTAINER # 镜像是谁写的 姓名 + 邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 添加内容,比如tomcat压缩包
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 暴露端口
CMD # 指定这个容器启动的时候,要运行的命令,只有最后一个会生效,而且可被替代
ENTRYPOINT # 指定这个容器启动的时候,要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 DockerFile 这个时候就会被运行 ONBUILD 的指令,是一种触发指令
COPY # 类似 ADD 命令,将文件拷贝到镜像中
ENV # 构建的时候设置环境变量
Docker Hub 中99%镜像都是从这个基础镜像过来的 FROM sratch,然后配置需要的软件和配置来进行构建
创建一个自己的centos
# 先运行一个centos容器
[root@vinjcent /]# docker run -it centos
# 命令 pwd 是有的
[root@102a65dc08c0 /]# pwd
/
# 而 vim、ifconfig是没有的
[root@102a65dc08c0 /]# vim
bash: vim: command not found
[root@102a65dc08c0 /]# ifconfig
bash: ifconfig: command not found
# 在本地 /home 目录下创建一个dockerfile文件夹
[root@vinjcent home]# mkdir dockerfile
[root@vinjcent home]# ls
dockerfile
[root@vinjcent home]# cd dockerfile/
### 创建一个自定义的centos镜像
# 1.编写DockerFile文件
[root@vinjcent dockerfile]# vim div_centos_dockerfile
# 2022年需要加FROM centos加上版本号7,如下所示
[root@vinjcent dockerfile]# cat div_centos_dockerfile
# ======================文件内容======================
FROM centos:7
MAINTAINER vinjcent<2573552788@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
# ======================文件内容======================
# 2.通过这个文件构建镜像
[root@vinjcent dockerfile]# docker build -f div_centos_dockerfile -t div_centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 5d0da3dc9764
Step 2/10 : MAINTAINER vinjcent<2573552788@qq.com>
---> Using cache
---> 7908e070649b
Step 3/10 : ENV MYPATH /usr/local
---> Using cache
---> 73302161ae59
Step 4/10 : WORKDIR $MYPATH
---> Using cache
---> a75c5e6071c6
Step 5/10 : RUN yum -y install vim
---> Running in baf1e6dabd56
......
Successfully built fa38876b0d2b
Successfully tagged div_centos:1.0
对比原生 centos 镜像
拿到一个镜像可以查看历史的变更
[root@vinjcent dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
div_centos 1.0 fa38876b0d2b 8 minutes ago 601MB
centos 7 eeb6ee3f44bd 9 months ago 204MB
# 查看
[root@vinjcent dockerfile]# docker history fa38876b0d2b
IMAGE CREATED CREATED BY SIZE COMMENT
fa38876b0d2b 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
e8732db7e605 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
7be27facac36 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
dd7e5e952625 8 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
c68d4a79a2b9 8 minutes ago /bin/sh -c yum -y install net-tools 171MB
9a761143d117 8 minutes ago /bin/sh -c yum -y install vim 226MB
ea3c72ece7d5 9 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
4cdb2e908e4f 9 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
094dcbda61e8 9 minutes ago /bin/sh -c #(nop) MAINTAINER vinjcent<25735… 0B
eeb6ee3f44bd 9 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 9 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 9 months ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
CMD 和 ENTRYPOINT 区别
CMD # 指定这个容器启动的时候,要运行的命令,只有最后一个会生效,而且可被替代
ENTRYPOINT # 指定这个容器启动的时候,要运行的命令,可以追加命令
测试 cmd 命令
[root@vinjcent dockerfile]# ls
div_centos_dockerfile
### 编写dockerfile文件
[root@vinjcent dockerfile]# vim div_cmd_dockerfile
# =======================文件内容=================================
FROM centos:7
CMD ["ls","-a"]
# =======================文件内容=================================
[root@vinjcent dockerfile]# docker build -f div_cmd_dockerfile -t div_cmd .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos:7
---> eeb6ee3f44bd
Step 2/2 : CMD ["ls","-a"]
---> Running in 92c338039804
Removing intermediate container 92c338039804
---> 4f9866b5d755
Successfully built 4f9866b5d755
Successfully tagged div_cmd:latest
# 执行了 "ls -a" 命令
[root@vinjcent dockerfile]# docker run 4f9866b5d755
.
..
.dockerenv
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 想要追加一个 -l 命令
[root@vinjcent dockerfile]# docker run 4f9866b5d755 -l
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
# cmd 的情况下,-l 替换了 CMD ["ls","-a"] 命令,-l 不是命令所以报错
docker run 4f9866b5d755 ls -al # 才是完整的命令
测试 entrypoint 命令
### 编写dockerfile文件
[root@vinjcent dockerfile]# vim div_entrypoint_dockerfile
[root@vinjcent dockerfile]# cat div_entrypoint_dockerfile
# =======================文件内容=================================
FROM centos:7
ENTRYPOINT ["ls","-a"]
# =======================文件内容=================================
[root@vinjcent dockerfile]# docker build -f div_entrypoint_dockerfile -t div_entrypoint .
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos:7
---> eeb6ee3f44bd
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in 8e5db219aa97
Removing intermediate container 8e5db219aa97
---> dd11944ea9b8
Successfully built dd11944ea9b8
Successfully tagged div_entrypoint:latest
[root@vinjcent dockerfile]# docker run dd11944ea9b8
.
..
.dockerenv
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
DockerFile 中很多命令都十分相似,我们需要了解它们的区别,需要通过测试区分它们
JDK压缩包下载:https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html
Tomcat压缩包下载:https://tomcat.apache.org/download-90.cgi
使用 ftp 上传
Dockerfile
,build 会自动寻找这个文件,就不需要 -f 指定了# 在本地的 /home/vinjcent 目录下
[root@vinjcent vinjcent]# pwd
/home/vinjcent
# 创建一个 readme.txt 文件
[root@vinjcent vinjcent]# touch readme.txt
# 创建并编辑文件div_tomcat_dockerfile
[root@vinjcent vinjcent]# vim div_tomcat_dockerfile
# =======================文件内容=================================
FROM centos:7
MAINTAINER vinjcent<2573552788@qq.com>
COPY readme.txt /url/local/readme.txt
ADD jdk-8u202-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.62.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_202
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.62
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.62
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.62/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.62/bin/l
ogs/catalina.out
# =======================文件内容=================================
# 构建镜像
[root@vinjcent vinjcent]# docker build -f div_tomcat_dockerfile -t div_tomcat .
# 查看下载的镜像
[root@vinjcent vinjcent]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
div_tomcat latest c0e79ed03e6e 20 seconds ago 849MB
div_entrypoint latest dd11944ea9b8 14 hours ago 204MB
div_cmd latest 4f9866b5d755 14 hours ago 204MB
div_centos 1.0 fa38876b0d2b 14 hours ago 601MB
centos 7 eeb6ee3f44bd 9 months ago 204MB
# 运行自定义 tomcat 容器
[root@vinjcent vinjcent]# docker run -d -p 9090:8080 --name div_tomcat01 -v /home/vinjcent/build/tomcat/test:/usr/local/apache-tomcat-9.0.62/webapps/test -v /home/vinjcent/build/tomcat/tomcat_logs/:/usr/local/apache-tomcat-9.0.62/logs div_tomcat
# 查看正在运行的容器
[root@vinjcent tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6d25b283ea1 div_tomcat "/bin/sh -c '/usr/lo…" About a minute ago Up About a minute 0.0.0.0:9090->8080/tcp, :::9090->8080/tcp div_tomcat01
[root@vinjcent tomcat]# docker exec -it f6d25b283ea1
# 进入我们自定义的容器
[root@vinjcent tomcat]# docker exec -it f6d25b283ea1 /bin/bash
[root@f6d25b283ea1 local]# pwd
/usr/local
# 查看目录
[root@f6d25b283ea1 local]# ls -l
total 0
drwxr-xr-x. 1 root root 45 Jun 18 04:11 apache-tomcat-9.0.62
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 7 10 143 245 Dec 15 2018 jdk1.8.0_202
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Nov 13 2020 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
# 开启另外一个窗口访问
[root@vinjcent vinjcent]# curl localhost:9090
外网(自己电脑)访问
/home/vinjcent/build/tomcat/test/WEB-INF
目录下创建 web.xml
文件和/home/vinjcent/build/tomcat/test
目录下创建 index.jsp
文件<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>scope1.jsp页面</h1>
Hello World!<br />
</body>
</html>
在本地(自己电脑)访问http://192.168.159.100:9090/test/
日志查看
在本地/home/vinjcent/build/tomcat/tomcat_logs
目录下
# 查看输出日志
[root@vinjcent tomcat_logs]# cat catalina.out
小结:需要掌握 dockerfile 的编写,之后的一切都是用 docker 镜像来发布运行
DockerHub
在我们的服务器上提交自己的镜像
[root@vinjcent /]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
登陆完毕之后就可以提交镜像了,就是 docker push
# 登录dockerhub
[root@vinjcent /]# docker login -u vinjcent
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
### push 命令
# 查看镜像
[root@vinjcent /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
div_tomcat latest bedc043e9029 12 hours ago 849MB
div_entrypoint latest dd11944ea9b8 26 hours ago 204MB
div_cmd latest 4f9866b5d755 26 hours ago 204MB
div_centos 1.0 fa38876b0d2b 27 hours ago 601MB
centos 7 eeb6ee3f44bd 9 months ago 204MB
# push 到我们个人的dockerhub
[root@vinjcent /]# docker push div_tomcat
Using default tag: latest
The push refers to repository [docker.io/library/div_tomcat]
fd68039a504a: Preparing
604318eb9f98: Preparing
55aedaec4348: Preparing
d16fd3a2c693: Preparing
174f56854903: Preparing
denied: requested access to the resource is denied # 问题: 拒绝上传
# 不存在镜像
[root@vinjcent /]# docker push vinjcent/div_tomcat:1.0
The push refers to repository [docker.io/vinjcent/div_tomcat]
An image does not exist locally with the tag: vinjcent/div_tomcat
# 解决: 增加一个 tag
[root@vinjcent /]# docker tag bedc043e9029 vinjcent/tomcat:1.0
# docker push 上去即可,发布的镜像尽量带上版本号
[root@vinjcent /]# docker push vinjcent/tomcat:1.0
The push refers to repository [docker.io/vinjcent/tomcat]
fd68039a504a: Pushed
604318eb9f98: Pushed
55aedaec4348: Pushed
d16fd3a2c693: Pushed
174f56854903: Pushed
1.0: digest: sha256:8ced27625d576b5d70fda53c424e073dc2170744923116ee4e1e385b49e45c59 size: 1373
提交的时候也是按照镜像的层级来提交的
阿里云镜像服务上