一款产品从开发到上线,从操作系统,到运行环境,再到应用配置。作为开发+运维之间的协作我们需要关心很多东西,这也是很多互联网公司都不得不面对的问题,特别是各种版本的迭代之后,不同版本环境的兼容,对运维人员都是考验
Docker之所以发展如此迅速,也是因为它对此给出了一个标准化的解决方案。
环境配置如此麻烦,换一台机器,就要重来一次,费力费时。很多人想到,能不能从根本上解决问题,软件可以带环境安装?也就是说,安装的时候,把原始环境一模一样地复制过来。开发人员利用 Docker 可以消除协作编码时“在我的机器上可正常工作”的问题。
Docker是基于Go语言实现的云开源项目。
Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的APP(可以是一个WEB应用或数据库应用等等)及其运行环境能够做到“一次封装,到处运行”。
底层分为:
上层分为:
PS:VMWar就是一种全虚拟化的虚拟机
底层分为:
上层分为:
PS:相比于全虚拟化,OS虚拟化技术省去了虚拟硬件,直接共用底层硬件,但是上层操作系统必须与底层操作系统相同,docker虚拟化就是OS虚拟化技术
底层分为:
上层分为:
PS:硬件层虚拟化是直接基于硬件进行虚拟化,上层可以安装多种虚拟操作系统
传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程;
而容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核,而且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便。
每个容器之间互相隔离,每个容器有自己的文件系统 ,容器之间进程不会相互影响,能区分计算资源。
镜像是容器的只读模板和UnionFs文件系统(镜像是分层的,上层镜像基于下层镜像,最底层的是基础镜像),可以形象的理解为java类,定义的java对象的属性和功能
容器是一个精华版的Linux系统包含了运行在其中的应用程序,容器是镜像的实例,一个镜像可以启动多个容器,好比一个java类可以创建多个对象
仓库用于存放镜像文件,类似于Git的代码仓库,提交镜像到仓库,在任意docker客户端连接仓库,可以下载镜像
Docker是一个客户端-服务器(C/S)架构程序。Docker客户端只需要向Docker服务器或者守护进程发出请求,服务器或者守护进程将完成所有工作并返回结果。Docker提供了一个命令行工具Docker以及一整套RESTful API。你可以在同一台宿主机上运行Docker守护进程和客户端,也可以从本地的Docker客户端连接到运行在另一台宿主机上的远程Docker守护进程。
docker images [OPTINS] 查看本地所有镜像
docker search [OPTINS] 查找某个镜像
docker pull 下载镜像
docker rmi 删除某个镜像
docker run [OPTINS] IMAGES [COMMAND] 启动容器(如果容器不存在先新建)
docker ps [OPTINS] 列出所有正在运行的容器
退出正在运行的容器
docker start 容器ID或容器名 启动容器
docker restart 容器ID或容器名 重启容器
docker stop 容器ID或容器名 停止容器
docker kill 容器ID或容器名 强制停止容器
docker rm 容器ID 删除已停止的容器
docker logs -f -t --tail 容器ID 查看容器日志
docker top 容器ID 查看指定容器内运行的进程
docker inspect 容器ID 查看容器的详细信息
进入正在运行的容器并打开交互终端
从容器中拷贝文件到宿主机
注:在docker run -d启动后台守护式容器,如果运行的容器不会自动挂起(必须有前台响应),则会直接退出容器,例如:docker run -d centos;但是如果容器会自动挂起,则不会退出,例如:docker run -d tomcat;针对后台守护式启动没有前台挂起退出的容器,可以使用 docker run -d centos /bin/bash
UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。Union 文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录
docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。
bootfs(boot file system)主要包含bootloader(引导加载程序)和kernel(内核), bootloader主要是引导加载kernel, Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。
rootfs (root file system) ,在bootfs之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
对于一个精简的OS,rootfs可以很小,只需要包括最基本的命令、工具和程序库就可以了,因为底层直接用Host(宿主机)的kernel(系统内核),自己只需要提供 rootfs 就行了。由此可见对于不同的linux发行版, bootfs基本是一致的, rootfs会有差别, 因此不同的发行版可以公用bootfs。
实际上我们在pull镜像时就可以看到,docker镜像是一层层的在下载,这种设计的最大好处就是"共享",比如:有多个镜像都从相同的 base 镜像构建而来,那么宿主机只需在磁盘上保存一份base镜像,
同时内存中也只需加载一份 base 镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。
ps:docker镜像都是只读的,当容器启动时,一个新的可写层被加载到镜像顶部。这一层通常被称为"容器层",“容器层之下都是镜像层”。
例如:在docker pull centos,下载了精简版的linux系统镜像,并docker run -d centos /bin/bash 后台启动了,但是精简版的系统缺少很多组件,我们在容器中下载新增了所需的容器,在通过commit提交容器副本成为一个新的镜像,相比原先的镜像增加了我们自定义的组件。
docker容器运行中产生的数据,是不会持久化保留下来的,当容器停止关闭后,数据随之也会被清空,为了能保证容器数据不被清空,我们就需要使用容器数据卷。
以tomcat容器为例:
docker run -it -v D:\User:\usr\local\tomcat\webapps 容器ID
docker inspect 容器ID
PS D:\> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcatfile 1.0 f5e4f24392ea 2 days ago 529MB
mytomcat 2.0 f0f9376c76c6 4 days ago 529MB
mytomcat 1.0 65a3bdc5c771 4 days ago 529MB
tomcat latest 31a47677561a 6 days ago 529MB
centos latest 470671670cac 3 months ago 237MB
PS D:\> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS D:\> docker run -d -p 8080:8080 -v d:\User:/usr/local/tomcat/webapps/ROOT tomcat
db9f24cc76398960007edaf3b0361b822aa1b0347b233d4b0408c09edeb1518b
PS D:\> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db9f24cc7639 tomcat "catalina.sh run" 5 seconds ago Up 3 seconds 0.0.0.0:8080->8080/tcp ecstatic_matsumoto
PS D:\> docker exec -it db9f24cc7639 /bin/bash
root@db9f24cc7639:/usr/local/tomcat# pwd
/usr/local/tomcat
root@db9f24cc7639:/usr/local/tomcat# cd webapps
root@db9f24cc7639:/usr/local/tomcat/webapps# ls
ROOT
root@db9f24cc7639:/usr/local/tomcat/webapps# cd ROOT/
root@db9f24cc7639:/usr/local/tomcat/webapps/ROOT# ls
1.txt 111.txt aa hello.html
查看docker inspect 容器ID 详细信息
"Mounts": [
{
"Type": "bind",
"Source": "/host_mnt/d/User",
"Destination": "/usr/local/tomcat/webapps/ROOT",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
PS:
DockerFile是用来创建Docker镜像的构建文件,是由一系列命令和脚本组成
#FROM继承于那个镜像(当前镜像基于那个镜像)
FROM tomcat
#MAINTAINER 镜像创建者的名称和邮箱
MAINTAINER zjf<[email protected]>
#EVN 自定义环境变量
ENV MYPATH /usr/local/tomcat
#WORKDIR 指定镜像启动容器后,默认登录的目录
WORKDIR $MYPATH
#VOLUME 容器数据卷,用于持久化和共享数据,通过DockerFile添加的VOLUME无法和-v命令一样指定宿主机的目录,而是随机分配
VOLUME ["/usr/local/tomcat/webapps","/dataVolumeContainer"]
#CMD 指定容器启动要运行的命令,如果多个CMD只有最后一个生效
CMD echo "finished,--------success1"
#EXPOSE 定义容器启动的端口
EXPOSE 8080
#CMD 指定容器启动要运行的命令,如果多个CMD只有最后一个生效
CMD ["catalina.sh", "run"]
ps:处于DockerFile可移植性的考虑,并不能像"docker run -it -v /宿主机目录:/容器目录"一样可以指定宿主机的目录,因为在其他宿主机运行dockerFile,可能并没有特定的数据卷挂载目录。
命令:
docker build -f d:\myDockerFile\tomcatFile\dockerFile -t myfiletomcat:1.0
参数解析:
-f : 指定dokcerFile所在目录;
-t : 指定生成镜像的名称和TAG
执行build命令流程
PS D:\myDockerFile\tomcatFile> docker build -f d:\myDockerFile\tomcatFile\dockerFile -t myfiletomcat:1.0 .
Sending build context to Docker daemon 2.56kB
Step 1/8 : FROM tomcat
---> 31a47677561a
Step 2/8 : MAINTAINER zjf<[email protected]>
---> Using cache
---> 85c368c26268
Step 3/8 : ENV MYPATH /usr/local/tomcat
---> Using cache
---> 7105735a7441
Step 4/8 : WORKDIR $MYPATH
---> Using cache
---> 41d7b21e5535
Step 5/8 : VOLUME ["/usr/local/tomcat/webapps","/dataVolumeContainer"]
---> Using cache
---> 08ca1776a093
Step 6/8 : CMD echo "finished,--------success1"
---> Using cache
---> c577a4be0545
Step 7/8 : EXPOSE 8080
---> Using cache
---> 2031d8427f31
Step 8/8 : CMD ["catalina.sh", "run"]
---> Using cache
---> f5e4f24392ea
Successfully built f5e4f24392ea
Successfully tagged myfiletomcat:1.0
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
PS:通过Step可以清晰看到dockerFile执行流程,从上而下执行一层层加载dockerFile文件,从这里也可以印证镜像是UnionFS文件系统,镜像是一层层叠加,对于用户最终只看到一个最外层镜像。
PS D:\myDockerFile\tomcatFile> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS D:\myDockerFile\tomcatFile> docker run -it -p 8080:8080 myfiletomcat:1.0 /bin/bash
root@e60f1bc85c7c:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps work
CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp webapps.dist
root@e60f1bc85c7c:/usr/local/tomcat#
PS D:\myDockerFile\tomcatFile> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e60f1bc85c7c myfiletomcat:1.0 "/bin/bash" 22 seconds ago Up 20 seconds 0.0.0.0:8080->8080/tcp nervous_driscoll
PS D:\myDockerFile\tomcatFile> docker inspect e60f1bc85c7c
[
{
"Id": "e60f1bc85c7c38b357c3a7df49ef1fa1320a42d40300654e9865cecc04e129fd",
"Created": "2020-04-25T13:20:35.3356501Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3377,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-25T13:20:36.395656Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:f5e4f24392eae5bc745d486ee0639f0323a15039137a69bd8e40eb3b8178b8c0",
"ResolvConfPath": "/var/lib/docker/containers/e60f1bc85c7c38b357c3a7df49ef1fa1320a42d40300654e9865cecc04e129fd/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e60f1bc85c7c38b357c3a7df49ef1fa1320a42d40300654e9865cecc04e129fd/hostname",
"HostsPath": "/var/lib/docker/containers/e60f1bc85c7c38b357c3a7df49ef1fa1320a42d40300654e9865cecc04e129fd/hosts",
"LogPath": "/var/lib/docker/containers/e60f1bc85c7c38b357c3a7df49ef1fa1320a42d40300654e9865cecc04e129fd/e60f1bc85c7c38b357c3a7df49ef1fa1320a42d40300654e9865cecc04e129fd-json.log",
"Name": "/nervous_driscoll",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"Mounts": [
{
"Type": "volume",
"Name": "9041847af8899349879d1a93b5e034221f93384350840674d6e8be5e9967ba80",
"Source": "/var/lib/docker/volumes/9041847af8899349879d1a93b5e034221f93384350840674d6e8be5e9967ba80/_data",
"Destination": "/dataVolumeContainer",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "a5b13b6a44191ba058ba66b5081f6c3bd13bd619825b72f19d53d785275f84f4",
"Source": "/var/lib/docker/volumes/a5b13b6a44191ba058ba66b5081f6c3bd13bd619825b72f19d53d785275f84f4/_data",
"Destination": "/usr/local/tomcat/webapps",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
}
]
PS:在dockerFile文件中指定的容器数据卷:VOLUME ["/usr/local/tomcat/webapps","/dataVolumeContainer"],在windows系统中无法关联到本地目录,因为docker指定宿主机的挂载目录层级:/var/lib/docker/volumes/****,这个目录在windows系统不存在,大家在inspect详情中"Mounts"挂载中"Source"属性中可以看到指定宿主机的目录,所以这里就不在演示数据共享和持久化功能。
子容器挂载父容器数据卷命令:–volumes-from 父容器名
刚刚实现容器数据卷的两种挂载方式:1.通过"docker run -it -v /宿主机目录:/容器目录" 2.通过dockerFile文件构建镜像可以指定多个挂载数据卷,现在我们实现数据卷挂载容器,在运行的容器数据卷中挂载其他的容器,被挂载的容器是父容器。
PS D:\myDockerFile\tomcatFile> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcatfile 1.0 f5e4f24392ea 4 days ago 529MB
myfiletomcat 1.0 f5e4f24392ea 4 days ago 529MB
mytomcat 2.0 f0f9376c76c6 6 days ago 529MB
mytomcat 1.0 65a3bdc5c771 6 days ago 529MB
tomcat latest 31a47677561a 8 days ago 529MB
centos latest 470671670cac 3 months ago 237MB
PS D:\myDockerFile\tomcatFile> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS D:\myDockerFile\tomcatFile> docker run -it --name parentTomcat myfiletomcat:1.0 /bin/bash
root@873bdf86cc8c:/usr/local/tomcat# cd /
root@873bdf86cc8c:/# ls
bin dataVolumeContainer etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
root@873bdf86cc8c:/# cd dataVolumeContainer/
root@873bdf86cc8c:/dataVolumeContainer# touch volums.txt
root@873bdf86cc8c:/dataVolumeContainer# ls
volums.txt
root@873bdf86cc8c:/dataVolumeContainer#
这里启动了刚才挂载两个容器卷的镜像myfiletomcat通过–name定义名称为parentTomcat,/dataVolumeContainer目录就是数据卷挂载目录,并在该目录创建了touch volums.txt文件
root@873bdf86cc8c:/dataVolumeContainer#
PS D:\myDockerFile\tomcatFile> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
873bdf86cc8c myfiletomcat:1.0 "/bin/bash" 5 minutes ago Up 5 minutes 8080/tcp parentTomcat
PS D:\myDockerFile\tomcatFile> docker run -it --name childrenTomcat --volumes-from parentTomcat myfiletomcat:1.0 /bin/ba
sh
root@54a9af506bd5:/usr/local/tomcat# cd /
root@54a9af506bd5:/# ls
bin dataVolumeContainer etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
root@54a9af506bd5:/# cd dataVolumeContainer/
root@54a9af506bd5:/dataVolumeContainer# ls
volums.txt
root@54a9af506bd5:/dataVolumeContainer#
PS D:\myDockerFile\tomcatFile> docker run -it --name childrenTomcat --volumes-from parentTomcat mytomcat:2.0 /bin/bash
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/childrenTomcat" is already in use by container "54a9af506bd5482ae6a07d1b77c56e5334de2e90de8eaa80f5656a2e3f7834cd". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
PS D:\myDockerFile\tomcatFile> docker run -it --name childrenTomcat2 --volumes-from parentTomcat mytomcat:2.0 /bin/bash
root@e0bb6e7c696e:/usr/local/tomcat# cd /
root@e0bb6e7c696e:/# ls
bin dataVolumeContainer etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
root@e0bb6e7c696e:/# cd dataVolumeContainer/
root@e0bb6e7c696e:/dataVolumeContainer# ls
volums.txt
root@e0bb6e7c696e:/dataVolumeContainer#
启动了两个子容器分别为childrenTomcat和childrenTomcat2,通过–volumes-from parentTomcat挂载到父容器数据卷,并且在/dataVolumeContainer数据卷挂载目录,查看到volums.txt文件
PS:数据卷挂载容器的特性,容器之间的数据共享的传递,会一直持续到没有容器使用为止,可以理解为即使父容器被删除,不会影响子容器数据传递,只要有至少两个数据卷挂载容器在运行,容器之间的数据共享传递不会终端
至此Docker容器知识整理完毕,后续更新DOcker高级知识(DockerFile解析【自定义DockerFile通过转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传Build构建镜像】;Docker实际运用;Docker仓库…)
我们刚通过dockerfile构建了一个自定义容器卷的tomcat(myfiletomcat:1.0),dockerfile是用来构建docker镜像的文件,是由一系列命令参数和脚本构成的。
构建步骤也非常简单:1.编写dockerfile文件;2.执行docker build构建命令;3:启动容器 docker run
FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20191001"
CMD ["/bin/bash"]
dockerfile编写要求
从应用软件的角度来看dockerfile、docker镜像、docker容器分别代表三个不同的阶段。
dockerfile自定义tomcat镜像
准备工作
FROM centos
MAINTAINER zjf<349955531.com>
#把宿主机当前上下文的test.txt拷贝到容器/usr/local/路径下
COPY test.txt /usr/local/cincontainer.txt
#把java与tomcat添加到容器中
ADD apache-tomcat-8.5.32.tar.gz /usr/local/
ADD jdk-8u172-linux-x64.tar.gz /usr/local/
#安装vim编辑器
RUN yum -y install vim
#环境变量设置,工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_172
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.32
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.32
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat
# ENTRYPOINT ["/usr/local/apache-tomcat-9.0.8/bin/startup.sh" ]
# CMD ["/usr/local/apache-tomcat-9.0.8/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.8/bin/startup
PS C:\WINDOWS\system32> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myfiletomcat 1.0 f5e4f24392ea 8 days ago 529MB
tomcatfile 1.0 f5e4f24392ea 8 days ago 529MB
mytomcat 2.0 f0f9376c76c6 10 days ago 529MB
mytomcat 1.0 65a3bdc5c771 10 days ago 529MB
tomcat latest 31a47677561a 12 days ago 529MB
centos latest 470671670cac 3 months ago 237MB
PS C:\WINDOWS\system32> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS C:\WINDOWS\system32> cd d:
PS D:\> cd .\myDockerFile\
PS D:\myDockerFile> cd .\tomcat8File\
PS D:\myDockerFile\tomcat8File> docker build -t zjftomcat8:1.0 .
Sending build context to Docker daemon 200.5MB
Step 1/15 : FROM centos
---> 470671670cac
Step 2/15 : MAINTAINER zjf<349955531.com>
---> Running in 80024ff8256d
Removing intermediate container 80024ff8256d
---> f2dd7afcbe7a
Step 3/15 : COPY test.txt /usr/local/cincontainer.txt
---> 7c0b86e9f289
Step 4/15 : ADD apache-tomcat-8.5.32.tar.gz /usr/local/
---> 6191cf3065ac
Step 5/15 : ADD jdk-8u172-linux-x64.tar.gz /usr/local/
---> ad3108bf1f23
Step 6/15 : RUN yum -y install vim
---> Running in 569dd57cf9cb
CentOS-8 - AppStream 432 kB/s | 7.0 MB 00:16
CentOS-8 - Base 414 kB/s | 2.2 MB 00:05
CentOS-8 - Extras 5.3 kB/s | 5.5 kB 00:01
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k
which x86_64 2.21-10.el8 BaseOS 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 31 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 5.7 kB/s | 39 kB 00:06
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 96 kB/s | 48 kB 00:00
(3/5): which-2.21-10.el8.x86_64.rpm 130 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 75 kB/s | 1.4 MB 00:18
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 192 kB/s | 6.3 MB 00:33
--------------------------------------------------------------------------------
Total 227 kB/s | 7.8 MB 00:35
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream 1.4 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) "
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : which-2.21-10.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/5
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/5
Verifying : which-2.21-10.el8.x86_64 5/5
Installed:
vim-enhanced-2:8.0.1763-13.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64
vim-common-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
which-2.21-10.el8.x86_64
Complete!
Removing intermediate container 569dd57cf9cb
---> 4e5383708b27
Step 7/15 : ENV MYPATH /usr/local
---> Running in d59f57ef4f7c
Removing intermediate container d59f57ef4f7c
---> d949714b566a
Step 8/15 : WORKDIR $MYPATH
---> Running in bfbf2fd915dc
Removing intermediate container bfbf2fd915dc
---> a6c2ba9dd4c8
Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_172
---> Running in 88c5c57d93f9
Removing intermediate container 88c5c57d93f9
---> 17a3ee8f5832
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in e5f614145675
Removing intermediate container e5f614145675
---> 62688bf4d3d9
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.32
---> Running in b23df981a8c5
Removing intermediate container b23df981a8c5
---> e89601adc335
Step 12/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.32
---> Running in 588e9e7d33fd
Removing intermediate container 588e9e7d33fd
---> bfdabd5bd7d1
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in 46786fa9cbb4
Removing intermediate container 46786fa9cbb4
---> 4809373695f7
Step 14/15 : EXPOSE 8080
---> Running in c3e69be82ec6
Removing intermediate container c3e69be82ec6
---> b8fb886d5bec
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.8/bin/startup
---> Running in ff14f161df18
Removing intermediate container ff14f161df18
---> eef5384cd048
Successfully built eef5384cd048
Successfully tagged zjftomcat8:1.0
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
PS D:\myDockerFile\tomcat8File> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zjftomcat8 1.0 eef5384cd048 About a minute ago 698MB
myfiletomcat 1.0 f5e4f24392ea 8 days ago 529MB
tomcatfile 1.0 f5e4f24392ea 8 days ago 529MB
mytomcat 2.0 f0f9376c76c6 10 days ago 529MB
mytomcat 1.0 65a3bdc5c771 10 days ago 529MB
tomcat latest 31a47677561a 12 days ago 529MB
centos latest 470671670cac 3 months ago 237MB
PS D:\myDockerFile\tomcat8File>
出现Successfully built eef5384cd048;Successfully tagged zjftomcat8:1.0整个构建过程就成功了,并且在docker images中看到了我们定义的zjftomcat8镜像
docker run -it -p 9080:8080 --name mytom9 -v d:\User\test:/usr/local/apache-tomcat-8.5.32/webapps/test -v d:\User\logs/:/usr/loca zjftomcat8:1.0 /bin/bash
查看/usr/local/目录,已经实现jdk和tomcat8自动解压,并copy宿主机test.txt到cincontainer.txt
[root@36690bde6081 local]# ls -l
total 48
drwxr-xr-x 1 root root 4096 Apr 30 01:13 apache-tomcat-8.5.32
drwxr-xr-x 2 root root 4096 May 11 2019 bin
-rwxr-xr-x 1 root root 0 Apr 30 01:03 cincontainer.txt
drwxr-xr-x 2 root root 4096 May 11 2019 etc
drwxr-xr-x 2 root root 4096 May 11 2019 games
drwxr-xr-x 2 root root 4096 May 11 2019 include
drwxr-xr-x 8 10 143 4096 Mar 29 2018 jdk1.8.0_172
drwxr-xr-x 2 root root 4096 May 11 2019 lib
drwxr-xr-x 2 root root 4096 May 11 2019 lib64
drwxr-xr-x 2 root root 4096 May 11 2019 libexec
drwxr-xr-x 2 root root 4096 May 11 2019 sbin
drwxr-xr-x 5 root root 4096 Jan 13 21:49 share
drwxr-xr-x 2 root root 4096 May 11 2019 src
在宿主机容器卷test目录,放入hello.html文件,容器中查看已经同步
[root@36690bde6081 apache-tomcat-8.5.32]# ls
LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf lib logs temp webapps work
[root@36690bde6081 apache-tomcat-8.5.32]# cd webapps/
[root@36690bde6081 webapps]# cd test/
[root@36690bde6081 test]# ls
hello.html
[root@36690bde6081 test]#
启动tomcat,访问8090端口和test目录下hello.html
[root@36690bde6081 bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-8.5.32
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.32
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.32/temp
Using JRE_HOME: /usr/local/jdk1.8.0_172
Using CLASSPATH: /usr/local/apache-tomcat-8.5.32/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.32/bin/tomcat-juli.jar
Tomcat started.
[root@36690bde6081 bin]# ps -ef | grep tomcat
root 45 0 34 01:56 pts/1 00:00:04 /usr/local/jdk1.8.0_172/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-8.5.32/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/apache-tomcat-8.5.32/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.32/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-8.5.32 -Dcatalina.home=/usr/local/apache-tomcat-8.5.32 -Djava.io.tmpdir=/usr/local/apache-tomcat-8.5.32/temp org.apache.catalina.startup.Bootstrap start
PS C:\WINDOWS\system32> docker search -s 50 mysql
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9436 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3400 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 691 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 75
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 67
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
PS C:\WINDOWS\system32> docker pull mysql:5.6
5.6: Pulling from library/mysql
b248fa9f6d2a: Pull complete
0058702edba7: Pull complete
84e63f6f2297: Pull complete
81ad333a3403: Pull complete
3708a6b9545b: Pull complete
442342ef4996: Pull complete
ecf24ffdea0f: Pull complete
2b7a5158d097: Pull complete
ddf457681dfd: Pull complete
da0a8ff4a16b: Pull complete
161b9a3ea216: Pull complete
Digest: sha256:f77b19ed0467ccb44d54550b9707aec339d61d570dd4d6f648a9d35e1310eafa
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
PS C:\WINDOWS\system32> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zjftomcat8 1.0 eef5384cd048 About an hour ago 698MB
mysql 5.6 afa411733b0c 2 days ago 302MB
tomcatfile 1.0 f5e4f24392ea 8 days ago 529MB
myfiletomcat 1.0 f5e4f24392ea 8 days ago 529MB
mytomcat 2.0 f0f9376c76c6 10 days ago 529MB
mytomcat 1.0 65a3bdc5c771 10 days ago 529MB
tomcat latest 31a47677561a 12 days ago 529MB
centos latest 470671670cac 3 months ago 237MB
docker run -d -p 3306:3306 --name mysql -v d:\User\mysql\conf:/etc/mysql/conf.d -v d:\User\mysql\logs:/logs -v d:\User\mysql\data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
命令说明:
PS D:\User\mysql> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9884e2d7920 mysql:5.6 "docker-entrypoint.s…" 38 seconds ago Up 35 seconds 0.0.0.0:3306->3306/tcp mysql
36690bde6081 zjftomcat8:1.0 "/bin/bash" 42 minutes ago Up 42 minutes 0.0.0.0:9080->8080/tcp mytom9
PS D:\User\mysql> docker exec -it a9884e2d7920 /bin/bash
root@a9884e2d7920:/# mysql -uroot -p123456;
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.48 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
至此Dokcer容器知识整理更新完毕,码字不易希望大家阅览后有所收获,欢迎交流~