Docker精华 ,超全文档!

我们的口号是:人生不设限!!   


 

学习规划:继续上篇 《Docker入门》https://www.cnblogs.com/dk1024/p/13121389.html 

继续讲解:

 

一、容器数据卷

1、什么是容器数据卷:Docker是将应用和环境打包成一个镜像,发布到服务器,这个跑起来的容器如果不小心删除掉,呢么数据就会全部丢失,尤其是Mysql之类的容器,name现在就有一个需求,数据持久化到本地,这就是卷技术,即:目录的挂载,将我们的容器内的目录挂载到Linux某个目录下,说白了就是一种同步的机制,就是容器的持久化和同步操作,而且容器之间也是可以共享数据的。

 

 

 2、体验卷技术

实战:直接使用命令挂载  -v

#目的:启动centos,把容器内 /home 挂载到 /home/ceshi 下

#检查Linux /home目录下是否干净

[root@ldk ~]# cd /home/
[root@ldk home]#

[root@ldk home]# ls


#启动centos 容器

[root@ldk /]# docker run -it -v /home/ceshi:/home centos /bin/bash

 

#进入 容器 查看/home目录

[root@2ce40ff3194b /]# cd /home/
[root@2ce40ff3194b home]# ls
[root@2ce40ff3194b home]#

 

#查看 宿主机目录 /home目录  发现多了个  ceshi文件夹

[root@ldk ~]# cd /home/
[root@ldk home]# ls
 ceshi
[root@ldk home]#

 

#在 宿主机,确认卷是否挂载成功了

[root@ldk home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2ce40ff3194b centos "/bin/bash" 4 minutes ago Up 4 minutes boring_swanson
[root@ldk home]# docker inspect 2ce40ff3194b
[
{
"Id": "2ce40ff3194b192421024cf4b56606c6dfe3b49c504f41698f0b7ec38e49b71c",
"Created": "2020-06-20T06:05:30.539438785Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6971,

...

"Mounts": [
{
"Type": "bind",
"Source": "/home/ceshi",   #宿主机地址
"Destination": "/home",    #docker容器内地址
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],

...

 

#测试挂载效果(在 宿主机 /home/ceshi 下新增ldk.java  检查是否可以映射到 容器 内的/home 目录下)

 

#宿主机

[root@ldk home]# cd ceshi
[root@ldk ceshi]# ll
total 0
[root@ldk ceshi]# touch ldk.java
[root@ldk ceshi]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 20 14:17 ldk.java
[root@ldk ceshi]#

#容器内

[root@2ce40ff3194b /]# cd /home/
[root@2ce40ff3194b home]# ls
ldk.java     
[root@2ce40ff3194b home]#

#查看发现,同步成功! 同理,停止容器,修改 宿主机的ldk.java 文件,再重启容器,查看容器内的ldk.java,同样是可以同步成功的。只要容器存在,就可以同步成功。注:删除容器,宿主机的挂载目录还是存在的。这也间接说明,两份文件占用了两份存储。

 

实战:Mysql 同步数据

 

#获取mysql镜像

[root@ldk /]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 8559a31e96f4: Already exists d51ce1c2e575: Already exists c2344adc4858: Already exists fcf3ceff18fc: Already exists 16da0c38dc5b: Already exists b905d1797e97: Already exists 4b50d1c6b05c: Already exists c75914a65ca2: Pull complete 1ae8042bdd09: Pull complete 453ac13c00a3: Pull complete 9e680cd72f08: Pull complete a6b5dc864b6c: Pull complete Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest

#启动mysql
-d 后台运行
-p 端口映射
-v 卷挂载 这边我们挂载两个目录
-e mysql密码
--name 容器名字 [root@ldk /]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 8126f9c18eecb4581a33c1bbab4b5b829617d25104ed7b9004fe751055a9606a [root@ldk /]#

#启动并且挂载成功,按照centos容器挂载规则,查看是否挂载

 

 

二、具名挂载和匿名挂载

#匿名挂载  Nginx, 只指定容器内路径,不指定容器外路径

[root@ldk ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx 9aef70a2a4a5e91687d639dc65635968c8d86e0c456be4eff9665ce2870ced94

#查看所有挂载情况 [root@ldk ~]# docker volume ls DRIVER VOLUME NAME local 9b560fb95ed9e801600ee4fc84d8566575185ff04eec984bd8b2a4bcff9459f1 local 12c2c93d2d80e10ea8545ff78fc7e3fa2eb03400bcc5a7bf475b36d64ec6f48a local 78f7f7c39370425190946b0a29288043e2999e9f0c18078b045c89895bd57264 [root@ldk ~]#

#具名挂载 格式:-v 卷名:容器内目录(大多数情况都是具名方式)

[root@ldk ~]# docker run -d -P --name nginx02 -v jumign-nginx:/etc/nginx nginx
709e5d31a7adfc79fa491edabfec81048a04be3df22dcbb6edf419bfd16de5fd
[root@ldk ~]# docker volume ls
DRIVER VOLUME NAME
local 9b560fb95ed9e801600ee4fc84d8566575185ff04eec984bd8b2a4bcff9459f1
local 12c2c93d2d80e10ea8545ff78fc7e3fa2eb03400bcc5a7bf475b36d64ec6f48a
local 78f7f7c39370425190946b0a29288043e2999e9f0c18078b045c89895bd57264
local jumign-nginx
[root@ldk ~]#

 

 

#查看inspect,所有的卷都挂载在:/var/lib/docker/volumes  下

[root@ldk ~]# docker volume inspect jumign-nginx
[
{
"CreatedAt": "2020-06-20T16:53:27+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/jumign-nginx/_data",
"Name": "jumign-nginx",
"Options": null,
"Scope": "local"
}
]
[root@ldk ~]#

 

#进入宿主机,验证卷的挂载情况

[root@ldk ~]# cd /var/lib/docker/volumes
[root@ldk volumes]# ll
total 40
drwxr-xr-x 3 root root 4096 Jun 20 16:48 12c2c93d2d80e10ea8545ff78fc7e3fa2eb03400bcc5a7bf475b36d64ec6f48a
drwxr-xr-x 3 root root 4096 Jun 20 16:36 78f7f7c39370425190946b0a29288043e2999e9f0c18078b045c89895bd57264
drwxr-xr-x 3 root root 4096 Jun 20 16:36 9b560fb95ed9e801600ee4fc84d8566575185ff04eec984bd8b2a4bcff9459f1
drwxr-xr-x 3 root root 4096 Jun 20 16:53 jumign-nginx
-rw------- 1 root root 32768 Jun 20 16:53 metadata.db
[root@ldk volumes]#

#继续深入查看jumign-nginx 挂载情况,看到了想要的nginx.conf配置文件

[root@ldk volumes]# cd jumign-nginx/_data/
[root@ldk _data]# ll
total 40
drwxr-xr-x 2 root root 4096 Jun 20 16:53 conf.d
-rw-r--r-- 1 root root 1007 May 26 23:00 fastcgi_params
-rw-r--r-- 1 root root 2837 May 26 23:00 koi-utf
-rw-r--r-- 1 root root 2223 May 26 23:00 koi-win
-rw-r--r-- 1 root root 5231 May 26 23:00 mime.types
lrwxrwxrwx 1 root root 22 May 26 23:01 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root 643 May 26 23:01 nginx.conf
-rw-r--r-- 1 root root 636 May 26 23:00 scgi_params
-rw-r--r-- 1 root root 664 May 26 23:00 uwsgi_params
-rw-r--r-- 1 root root 3610 May 26 23:00 win-utf
[root@ldk _data]#

#拓展:
ro readonly 只读:设置之后,只能通过容器外部进行改写,容器内不禁止操作,且权限不可修改
rw readwrite 可读可写,容器内外均可读写


docker run -d -P --name nginx02 -v jumign-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v jumign-nginx:/etc/nginx:rw nginx
 

 

 

三、数据卷之Dockerfile

  初识Dockerfile:之前我们使用commit命令生成过镜像,我们也可以使用Dockerfile构建镜像,说白了,Dockerfile就是Docker镜像的构建文件,就是一段命令脚本!

 小试一把:

#在宿主机 /home/docker-test-volume/ 下新建一个dockerfile1的文件
[root@ldk /]# cd /home/
[root@ldk home]# mkdir docker-test-volume
[root@ldk home]# cd docker-test-volume/
[root@ldk docker-test-volume]# ll
total 0
[root@ldk docker-test-volume]# vim dockerfile1

#编写脚本(开头大写,分层构建):

FROM centos


VOLUME ["volume01","volume02"]


CMD echo "------build end------"


CMD /bin/bash

#脚本解释:准备一个基础的centos镜像;
      设置具名挂载目录为volum01和volume02,创建的时候就挂载 ;
      构建完打印:------build end------;
      默认bash控制台;

#执行脚本

[root@ldk docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t ldk/centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 470671670cac
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 771370c6dfef
Removing intermediate container 771370c6dfef
---> 1941426d1d91
Step 3/4 : CMD echo "------build end------"
---> Running in 070cbd782e00
Removing intermediate container 070cbd782e00
---> c947d804f177
Step 4/4 : CMD /bin/bash
---> Running in 817099ec4089
Removing intermediate container 817099ec4089
---> 49a84ce17460
Successfully built 49a84ce17460
Successfully tagged ldk/centos:1.0

#创建镜像成功

[root@ldk docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ldk/centos 1.0 49a84ce17460 6 minutes ago 237MB
tomcat latest 2eb5a120304e 10 days ago 647MB
nginx latest 2622e6cca7eb 10 days ago 132MB
mysql 5.7 9cfcce23593a 11 days ago 448MB
mysql latest be0dbf01a0f3 11 days ago 541MB
centos latest 470671670cac 5 months ago 237MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
[root@ldk docker-test-volume]#

 

 

#启动自己构建的容器

[root@ldk /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ldk/centos 1.0 49a84ce17460 8 minutes ago 237MB
tomcat latest 2eb5a120304e 10 days ago 647MB
nginx latest 2622e6cca7eb 10 days ago 132MB
mysql 5.7 9cfcce23593a 11 days ago 448MB
mysql latest be0dbf01a0f3 11 days ago 541MB
centos latest 470671670cac 5 months ago 237MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
[root@ldk /]# docker run -it 49a84ce17460 /bin/bash
[root@7b0a9902dbeb /]# ls -l
total 56
lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jun 20 09:44 dev
drwxr-xr-x 1 root root 4096 Jun 20 09:44 etc
drwxr-xr-x 2 root root 4096 May 11 2019 home
lrwxrwxrwx 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------ 2 root root 4096 Jan 13 21:48 lost+found
drwxr-xr-x 2 root root 4096 May 11 2019 media
drwxr-xr-x 2 root root 4096 May 11 2019 mnt
drwxr-xr-x 2 root root 4096 May 11 2019 opt
dr-xr-xr-x 95 root root 0 Jun 20 09:44 proc
dr-xr-x--- 2 root root 4096 Jan 13 21:49 root
drwxr-xr-x 11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 May 11 2019 srv
dr-xr-xr-x 13 root root 0 Jun 20 09:31 sys
drwxrwxrwt 7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x 12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x 20 root root 4096 Jan 13 21:49 var
drwxr-xr-x 2 root root 4096 Jun 20 09:44 volume01   //这两个就是我们自动挂载的数据卷目录
drwxr-xr-x 2 root root 4096 Jun 20 09:44 volume02
[root@7b0a9902dbeb /]#

 

 

#验证是否挂载成功(在容器内部新建一个 av.txt文件,去宿主机寻找被同步的av.txt)

#进入容器内的挂载目录 /volum01,新建文件 av.txt

[root@7b0a9902dbeb /]# cd volume01
[root@7b0a9902dbeb volume01]# ls
[root@7b0a9902dbeb volume01]# touch av.txt
[root@7b0a9902dbeb volume01]# ls
av.txt
[root@7b0a9902dbeb volume01]#

 

 

#进入宿主机 

[root@ldk ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b0a9902dbeb 49a84ce17460 "/bin/bash" 5 minutes ago Up 5 minutes serene_napier
709e5d31a7ad nginx "/docker-entrypoint.…" 57 minutes ago Up 57 minutes 0.0.0.0:32769->80/tcp nginx02
9aef70a2a4a5 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:32768->80/tcp nginx01
[root@ldk ~]# docker inspect 7b0a9902dbeb
...
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/11bd55866068e52271cf7f50ab7adf3fdb1eef887f0f3560810ce83d36a44fcb-init/diff:/var/lib/docker/overlay2/07e92bfd3de681a8ac6ef84545406794783f828fd4f8f0303be4f18ae4e4e198/diff",
"MergedDir": "/var/lib/docker/overlay2/11bd55866068e52271cf7f50ab7adf3fdb1eef887f0f3560810ce83d36a44fcb/merged",
"UpperDir": "/var/lib/docker/overlay2/11bd55866068e52271cf7f50ab7adf3fdb1eef887f0f3560810ce83d36a44fcb/diff",
"WorkDir": "/var/lib/docker/overlay2/11bd55866068e52271cf7f50ab7adf3fdb1eef887f0f3560810ce83d36a44fcb/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "d4a5330aa541110183f279343a769ec93712ec71f6b71ebb71a76a452e3e21e2",
"Source": "/var/lib/docker/volumes/d4a5330aa541110183f279343a769ec93712ec71f6b71ebb71a76a452e3e21e2/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
...
[root@ldk ~]# cd /var/lib/docker/volumes/d4a5330aa541110183f279343a769ec93712ec71f6b71ebb71a76a452e3e21e2/_data
[root@ldk _data]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 20 17:50 av.txt
[root@ldk _data]#


 

 

四、数据卷容器

多个mysql同步数据!

#启动我们自己制作的镜像(注:启动命名最后一个参数,要么填写镜像id。要么填写 镜像名称+tag,否则会报错)
[root@ldk /]# docker run -it --name docker01 ldk/centos:1.0
[root@8a3e0b293bf9 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01    volume02

#启动成功之后 退出容器(Control + P + Q)

[root@8a3e0b293bf9 /]# [root@ldk /]#

#依赖docker01启动docker02,docker01就叫做数据卷容器 [root@ldk /]# docker run -it --name docker02 --volumes-from docker01 ldk/centos:1.0 [root@ee6fa31cd361 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02 [root@ee6fa31cd361 /]# cd volume01 [root@ee6fa31cd361 volume01]# ls

#进入docker01内部在挂载目录 volumes01下创建一个文件

[root@ldk /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee6fa31cd361 ldk/centos:1.0 "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker02
8a3e0b293bf9 ldk/centos:1.0 "/bin/sh -c /bin/bash" 4 minutes ago Up 4 minutes docker01
[root@ldk /]# docker attach 8a3e0b293bf9
[root@8a3e0b293bf9 /]# cd volume01
[root@8a3e0b293bf9 volume01]# ls
[root@8a3e0b293bf9 volume01]# touch docker01
[root@8a3e0b293bf9 volume01]# ls
docker01
[root@8a3e0b293bf9 volume01]#


#进入docker02内部在挂载目录 volumes01 查看是否有同步更新的文件
[root@ee6fa31cd361 volume01]# ls
docker01
[root@ee6fa31cd361 volume01]# 
#同理:创造一个docker03依赖docker01,效果一样,时拿出docker01,docker02 和docker03 已经 同步拷贝 的文件,依然存在!

 


 

 

 

 

五、DockerFile介绍

1、基础介绍:

  dockerfile就是用来构建docker镜像的文件,定义了一切步骤,就是一段包含命令和参数的可执行脚本。

 

构建步骤:1、编写一个dockerfile文件

        2、docker build 构建成为一个镜像

     3、docker run 运行镜像

     4、docker push 发布镜像(DockerHub 、阿里云镜像仓库)

 

构建过程:

  基础知识:1、每个保留关键字(指令)都必须是大写字母

        2、从上向下执行

        3、# 表示注释

        4、每个指令都会创建一个新的镜像层,并提交

 

 

 小总结:DockerFile是面向开发的,我们要发布项目,就需要制作镜像,就需要编写dockerfile文件,Docker镜像逐渐成为企业交付的标准。

    DockerFile:构建镜像的初始化文件,源代码。

    DockerImages:通过DockerFile构建生成的容器,最终发布和运行的产品。

    Docker容器:容器就是镜像运行起来的服务器。

 

DockerFile指令:

FROM      #基础镜像,一切的开始
MANTAINER   #镜像作者(姓名+邮箱)
RUN       #镜像构建的时候需要运行的命令
ADD       #步骤中添加tomcat镜像,这个tomcat压缩包就是添加内容
WORKDIR     #镜像的挂载目录
VOLUME     #挂载的目录位置
EXPOSE     #指定暴露端口
CMD       #指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代,如果替代之后不是命令,会报错的
ENTRYPOINT   #指定这个容器启动的时候要运行的命令,可以追加执行命令
ONBUILD     #当构建一个被继承的DockerFile,这个时候就会运行ONBUILD 的指令,触发指令。
COPY       #类似AD的,将我们的镜像拷贝到镜像中
WNV       #构建的时候这只环境变量

 

2、实战:构建自己的centos

 

#1、编写DockerFile文件

[root@ldk home]# mkdir dockerfile
[root@ldk home]# cd dockerfile/
[root@ldk dockerfile]# ll
total 0
[root@ldk dockerfile]# vim dockerfile01

DockerFile文件内容:

FROM centos

MAINTAINER ldk<107123456@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、通过这个DockerFile构建镜像

[root@ldk dockerfile]# docker build -f dockerfile01 -t ldkcentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 470671670cac
Step 2/10 : MAINTAINER ldk<107123456@qq.com>

...

---> Running in 58114b729de4
Removing intermediate container 58114b729de4
---> 3c797ceb67f6
Successfully built 3c797ceb67f6
Successfully tagged ldkcentos:0.1
[root@ldk dockerfile]# 

#3、构建成功,测试运行

[root@ldk dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ldkcentos 0.1 3c797ceb67f6 5 minutes ago 317MB
ldk/centos 1.0 49a84ce17460 6 hours ago 237MB
tomcat latest 2eb5a120304e 10 days ago 647MB
nginx latest 2622e6cca7eb 10 days ago 132MB
mysql 5.7 9cfcce23593a 11 days ago 448MB
mysql latest be0dbf01a0f3 11 days ago 541MB
centos latest 470671670cac 5 months ago 237MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
[root@ldk dockerfile]#

#启动自己构建的镜像

[root@ldk dockerfile]# docker run -it ldkcentos:0.1
[root@b334b9f261bf local]#

#我们发现进入镜像,直接进入了刚才DockerFile 设置的工作目录 /usr/local 下。

#4、docker history 镜像id  :查看镜像的构建过程历史信息,可以自己试一下,方便自己研究镜像的构建历史,这里就不在赘述。



 

 

3、实战:DockerFile制作tomcat镜像

#1、准备镜像文件  tomcat压缩包,jdk压缩包

#2、编写dockerfile文件,官方命名:DockerFile,build会自动寻找这个文件,就不需要-f指定了

[root@ldk tomcat]# vim Dockerfile

  #文件内容(特别注意:加粗部分的参数切勿写错,因为写错也不报错,最后执行也能成功,比较难检查错误):

FROM centos
MAINTAINER ldk<107123456@qq.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u65-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.0.28.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_65
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_MOHE/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.0.28
ENV CATLINA_BASH /usr/local/apache-tomcat-8.0.28
ENV PATH $PATH:$JAVA_HOME/bin:$CATALIN_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-8.0.28/bin/startup.sh && tail -F /url/local/apache-tomcat-8.0.28/bin/logs/catalina.out


3、构建镜像(不要忘记后面的 .)

[root@ldk tomcat]# docker build -t diytomcat .

[root@ldk tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
diytomcat latest 2835818b0158 15 minutes ago 672MB
ldkcentos 0.1 3c797ceb67f6 About an hour ago 317MB
ldk/centos 1.0 49a84ce17460 7 hours ago 237MB
tomcat latest 2eb5a120304e 10 days ago 647MB
nginx latest 2622e6cca7eb 11 days ago 132MB
mysql 5.7 9cfcce23593a 11 days ago 448MB
mysql latest be0dbf01a0f3 11 days ago 541MB
centos latest 470671670cac 5 months ago 237MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
[root@ldk tomcat]#


4、启动镜像(特别注意:第一个容器的挂载目录可以写webapp下,方便修改部署项目)

[root@ldk tomcat]# docker run -d -p 9090:8080 --name ldkdiytomcat -v /home/ldk/build/tomcat/test:/url/local/apache-tomcat-8.0.28/webapps/ -v /home/ldk/build/tomcat/tomcatlogs/:/url/local/apache-tomcat-8.0.28/logs diytomcat
d29fef718ec1b522847d1be13811dcfe6bd64da753c2e887fa54fa982ce2d35f

[root@ldk tomcat]# pwd
/home/ldk/build/tomcat
[root@ldk tomcat]# ll
total 8
drwxr-xr-x 2 root root 4096 Jun 21 00:54 test
drwxr-xr-x 2 root root 4096 Jun 21 00:55 tomcatlogs
[root@ldk tomcat]#ll

total 8
drwxr-xr-x 2 root root 4096 Jun 21 00:54 test
drwxr-xr-x 2 root root 4096 Jun 21 00:55 tomcatlogs
[root@ldk tomcat]#


5、外网访问测试(xxx.xxx.xxx.xxx:9090)

#ok,项目发布成功

 


 

以下为7月5号更新=======================》

 

六、发布自己的镜像到DockerHub 或者 阿里云容器服务

发布到DockerHub 

1、注册自己DockerHub的账号,网址:https://hub.docker.com/

2、登录 自己的 DockerHub

[root@ldk ~]# docker login -u ldkywzq
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
[root@ldk ~]# 

3、PUSH

1、找到想要push的镜像

  [root@ldk ~]# docker images
  REPOSITORY TAG IMAGE ID CREATED SIZE
  gstomcat latest 0aefabe3523b 12 days ago 672MB   //这是我将要push的镜像
  diytomcat01 latest 2835818b0158 2 weeks ago 672MB
  diytomcat latest 2835818b0158 2 weeks ago 672MB
  ldkcentos 0.1 3c797ceb67f6 2 weeks ago 317MB
  ldk/centos 1.0 49a84ce17460 2 weeks ago 237MB
  tomcat latest 2eb5a120304e 3 weeks ago 647MB
  nginx latest 2622e6cca7eb 3 weeks ago 132MB
  mysql 5.7 9cfcce23593a 3 weeks ago 448MB
  mysql latest be0dbf01a0f3 3 weeks ago 541MB
  centos latest 470671670cac 5 months ago 237MB
  hello-world latest bf756fb1ae65 6 months ago 13.3kB
  [root@ldk ~]#

 

2、为这个镜像打一个版本号(注:ldkywzq 要和 DockerHub 用户名保持一致,否则会 报错:denied: requested access to the resource is denied
[root@ldk ~]# docker tag 0aefabe3523b ldkywzq/tomcat:1.0
[root@ldk ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ldk/tomcat          1.0                 0aefabe3523b        12 days ago         672MB
ldkywzq/tomcat      1.0                 0aefabe3523b        12 days ago         672MB
gstomcat            latest              0aefabe3523b        12 days ago         672MB
diytomcat01         latest              2835818b0158        2 weeks ago         672MB
diytomcat           latest              2835818b0158        2 weeks ago         672MB
ldkcentos           0.1                 3c797ceb67f6        2 weeks ago         317MB
ldk/centos          1.0                 49a84ce17460        2 weeks ago         237MB
tomcat              latest              2eb5a120304e        3 weeks ago         647MB
nginx               latest              2622e6cca7eb        3 weeks ago         132MB
mysql               5.7                 9cfcce23593a        3 weeks ago         448MB
mysql               latest              be0dbf01a0f3        3 weeks ago         541MB
centos              latest              470671670cac        5 months ago        237MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
[root@ldk ~]# docker push ldkywzq/tomcat:1.0
The push refers to repository [docker.io/ldkywzq/tomcat]

  a4ffa71a7adb: Pushing [===================================> ] 40.16MB/57.21MB
  5c41923ea7eb: Pushed
  9533cfc71498: Pushing [=====> ] 38.07MB/364.9MB
  0683de282177: Pushing [============> ] 59.68MB/237.1MB



发布到 阿里云容器

1、登录阿里云 - 容器镜像服务 - 创建命名空间

 

 

 

 

 

 

 

 2、创建容器镜像

 

 

 

 

 

阿里云提交步骤(阿里云点击进自己的仓库名称,有特别全的提交步骤,这里就不在赘述)
docker login --username=ldkywzq registry.cn-qingdao.aliyuncs.com
docker push registry.cn-qingdao.aliyuncs.com/bilibili-ldkywzq/ldk-test:[镜像版本号]

 

 


 

关于Docker的笔记暂时记录这些了,常用操作命令和知识点基本都有了,就剩个Docker网络没写,说白了也就Linux安装了Docker之后,会自动配置一个属于Dcoker的内部路由器,这个路由器可以打通任何一个Docker内的容器,与宿主机是直连,也可打通。这些知识偏底层一些原理,我不再做记录,就让它交给时间吧,以后慢慢就理解了。

 

 

 

本文的思路大纲是来自《狂神说》免费课程,它是一个不为任何机构组织站台的程序爱好者,乐于开源,忠于自己,绝对宝藏男孩!

大家有兴趣可以去b站参观学习:https://space.bilibili.com/95256449/video

 

posted @ 2020-06-21 02:03  Dkwestworld  阅读(2704)  评论(14编辑  收藏  举报