一款产品从开发到上线,从操作系统,到运行环境,再到应用配置。作为开发+运维之间的协作我们需要关心很多东西,这也是很多互联网公司都不得不面对的问题,特别是各种版本的迭代之后,不同版本环境的兼容,对运维人员都是考验。
Docker之所以发展如此迅速,也是因为它对此给出了一个标准化的解决方案。
环境配置如此麻烦,换一台机器,就要重来一次,费力费时。很多人想到,能不能从根本上解决问题,软件可以带环境安装?也就是说,安装的时候,把原始环境一模一样地复制过来。开发人员利用 Docker 可以消除协作编码时“在我的机器上可正常工作”的问题。
Docker镜像的设计,使得Docker得以打破过去「程序即应用」的观念。透过镜像(images)将作业系统核心除外,运作应用程式所需要的系统环境,由下而上打包,达到应用程式跨平台间的无缝接轨运作。
Docker是基于Go语言实现的云开源项目。
Docker的主要目标是“Build,Ship and Run Any App,Anywhere”,也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的APP(可以是一个WEB应用或数据库应用等等)及其运行环境能够做到“一次封装,到处运行”。
一句话:解决了运行环境和配置问题软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术。
官网:https://www.docker.com/
中文版:https://www.docker-cn.com/
Docker 公司位于旧金山,由法裔美籍开发者和企业家 Solumon Hykes 创立,其标志如下图所示。
有意思的是,Docker 公司起初是一家名为 dotCloud 的平台即服务(Platform-as-a-Service, PaaS)提供商。
底层技术上,dotCloud 平台利用了 Linux 容器技术。为了方便创建和管理这些容器,dotCloud 开发了一套内部工具,之后被命名为“Docker”。Docker就是这样诞生的!
如今 Docker 公司被普遍认为是一家创新型科技公司,据说其市场价值约为 10 亿美元。Docker 公司已经通过多轮融资,吸纳了来自硅谷的几家风投公司的累计超过 2.4 亿美元的投资。
几乎所有的融资都发生在公司更名为“Docker”之后。
提示:“Docker”一词来自英国口语,意为码头工人(Dock Worker),即从船上装卸货物的人。
docker官网:http://www.docker.com
docker中文网站:https://www.docker-cn.com/
Docker Hub官网: https://hub.docker.com/
国内镜像:https://hub.daocloud.io/
GitHub 代码仓库 GitEE码云
Docker官方建议在Ubuntu中安装,因为Docker是基于Ubuntu发布的,而且一般Docker出现的问题Ubuntu是最先更新或者打补丁的。在很多版本的CentOS中是不支持更新最新的一些补丁包的。
由于我们学习的环境都使用的是CentOS,因此这里我们将Docker安装到CentOS上。Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。
注意:这里建议安装在CentOS7.x以上的版本,在CentOS6.x的版本中,安装前需要安装其他很多的环境而且Docker很多补丁不支持更新。
使用yum命令在线安装
[root@localhost ~]# yum install docker
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.aliyun.com
* updates: mirrors.163.com
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.5 MB 00:00:01
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker.x86_64.2.1.13.1-94.gitb2f74b2.el7.centos 将被 升级
---> 软件包 docker.x86_64.2.1.13.1-96.gitb2f74b2.el7.centos 将被 更新
--> 正在处理依赖关系 docker-common = 2:1.13.1-96.gitb2f74b2.el7.centos,它被软件包 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 需要
--> 正在处理依赖关系 docker-client = 2:1.13.1-96.gitb2f74b2.el7.centos,它被软件包 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 需要
--> 正在检查事务
---> 软件包 docker-client.x86_64.2.1.13.1-94.gitb2f74b2.el7.centos 将被 升级
---> 软件包 docker-client.x86_64.2.1.13.1-96.gitb2f74b2.el7.centos 将被 更新
---> 软件包 docker-common.x86_64.2.1.13.1-94.gitb2f74b2.el7.centos 将被 升级
---> 软件包 docker-common.x86_64.2.1.13.1-96.gitb2f74b2.el7.centos 将被 更新
--> 解决依赖关系完成
依赖关系解决
===========================================================================================================================================================================
Package 架构 版本 源 大小
===========================================================================================================================================================================
正在更新:
docker x86_64 2:1.13.1-96.gitb2f74b2.el7.centos extras 18 M
为依赖而更新:
docker-client x86_64 2:1.13.1-96.gitb2f74b2.el7.centos extras 3.9 M
docker-common x86_64 2:1.13.1-96.gitb2f74b2.el7.centos extras 96 k
事务概要
===========================================================================================================================================================================
升级 1 软件包 (+2 依赖软件包)
总下载量:22 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/3): docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64.rpm | 96 kB 00:00:00
(2/3): docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64.rpm | 3.9 MB 00:00:01
(3/3): docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64.rpm | 18 MB 00:00:06
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计 3.1 MB/s | 22 MB 00:00:07
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在更新 : 2:docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64 1/6
正在更新 : 2:docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64 2/6
正在更新 : 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 3/6
清理 : 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64 4/6
清理 : 2:docker-client-1.13.1-94.gitb2f74b2.el7.centos.x86_64 5/6
清理 : 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64 6/6
验证中 : 2:docker-1.13.1-96.gitb2f74b2.el7.centos.x86_64 1/6
验证中 : 2:docker-client-1.13.1-96.gitb2f74b2.el7.centos.x86_64 2/6
验证中 : 2:docker-common-1.13.1-96.gitb2f74b2.el7.centos.x86_64 3/6
验证中 : 2:docker-client-1.13.1-94.gitb2f74b2.el7.centos.x86_64 4/6
验证中 : 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64 5/6
验证中 : 2:docker-common-1.13.1-94.gitb2f74b2.el7.centos.x86_64 6/6
更新完毕:
docker.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos
作为依赖被升级:
docker-client.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos docker-common.x86_64 2:1.13.1-96.gitb2f74b2.el7.centos
完毕!
[root@localhost ~]# docker -v
Docker version 1.13.1, build b2f74b2/1.13.1
启动docker:service docker start systemctl start docker.service
停止docker:service docker stop systemctl stop docker.service
重启docker:service docker restart systemctl restart docker.service
查看docker状态:service docker status systemctl status docker.service
开机启动:chkconfig docker on
查看docker概要信息:docker info
查看docker帮助文档:docker --help
[root@localhost ~]# service docker status
Redirecting to /bin/systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2019-06-17 15:17:51 CST; 48s ago
Docs: http://docs.docker.com
Main PID: 15257 (dockerd-current)
CGroup: /system.slice/docker.service
├─15257 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docke...
└─15262 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libconta...
6月 17 15:17:49 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:49...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:50 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:51 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:51 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:50...
6月 17 15:17:51 localhost.localdomain systemd[1]: Started Docker Application Container....
6月 17 15:17:51 localhost.localdomain dockerd-current[15257]: time="2019-06-17T15:17:51...
Hint: Some lines were ellipsized, use -l to show in full.
Docker 镜像(Image)就是一个只读的模板。镜像可以用来创建 Docker 容器,一个镜像可以创建很多容器。
镜像只是一个静态文件,把镜像运行起来,形成容器!一个镜像可以运行无数个容器
Docker 利用容器(Container)独立运行的一个或一组应用。容器是用镜像创建的运行实例。
它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全的平台。可以把容器看做是一个简易版的 Linux 环境(包括root用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。
容器的定义和镜像几乎一模一样,也是一堆层的统一视角,唯一区别在于容器的最上面那一层是可读可写的。
仓库(Repository)是集中存放镜像文件的场所。
仓库(Repository)和仓库注册服务器(Registry)是有区别的。仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)。
仓库分为**公开仓库(Public)和私有仓库(Private)**两种形式。
最大的公开仓库是 Docker Hub(https://hub.docker.com/),
存放了数量庞大的镜像供用户下载。国内的公开仓库包括阿里云 、网易云等.
需要正确的理解仓储/镜像/容器这几个概念:
Docker 本身是一个容器运行载体或称之为管理引擎。我们把应用程序和配置依赖打包好形成一个可交付的运行环境,这个打包好的运行环境就似乎 image镜像文件。只有通过这个镜像文件才能生成 Docker 容器。image 文件可以看作是容器的模板。Docker 根据 image 文件生成容器的实例。同一个 image 文件,可以生成多个同时运行的容器实例。
Docker镜像是由文件系统叠加而成(是一种文件的存储形式)。最底端是一个文件引导系统,即bootfs,这很像典型的Linux/Unix的引导文件系统。Docker用户几乎永远不会和引导系统有什么交互。实际上,当一个容器启动后,它将会被移动到内存中,而引导文件系统则会被卸载,以留出更多的内存供磁盘镜像使用。Docker容器启动是需要的一些文件,而这些文件就可以称为Docker镜像。
列出docker下的所有镜像:docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
参数说明:
REPOSITORY:镜像所在的仓库名称
TAG:镜像标签
IMAGE ID:镜像ID
CREATED:镜像的创建日期(不是获取该镜像的日期)
SIZE:镜像大小
OPTIONS说明:
-a :列出本地所有的镜像(含中间映像层)
-q :只显示镜像ID。
–digests :显示镜像的摘要信息
–no-trunc :显示完整的镜像信息
这些镜像都是存储在Docker宿主机的/var/lib/docker目录下
[root@localhost docker]# cd /var/lib/docker
[root@localhost docker]# ll
总用量 4
drwx------. 3 root root 78 2月 17 22:06 containers
drwx------. 3 root root 22 2月 17 21:32 image
drwxr-x---. 3 root root 19 2月 17 21:32 network
drwx------. 17 root root 4096 6月 17 16:21 overlay2
drwx------. 4 root root 32 2月 17 21:32 plugins
drwx------. 2 root root 6 2月 17 21:32 swarm
drwx------. 2 root root 6 6月 17 16:31 tmp
drwx------. 2 root root 6 2月 17 21:32 trust
drwx------. 4 root root 169 2月 17 22:06 volumes
如果你需要从网络中查找需要的镜像,可以通过以下命令搜索
命令:docker search [OPTIONS] 镜像名字
[root@localhost docker]# docker search tomcat
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 2464 [OK]
docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 66 [OK]
docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 53 [OK]
docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 29 [OK]
docker.io docker.io/kubeguide/tomcat-app Tomcat image for Chapter 1 26
docker.io docker.io/consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
docker.io docker.io/cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
docker.io docker.io/aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 11 [OK]
docker.io docker.io/tutum/tomcat Base docker image to run a Tomcat applicat... 11
docker.io docker.io/arm32v7/tomcat Apache Tomcat is an open source implementa... 6
docker.io docker.io/rightctrl/tomcat CentOS , Oracle Java, tomcat application s... 5 [OK]
docker.io docker.io/maluuba/tomcat7-java8 Tomcat7 with java8. 3
docker.io docker.io/amd64/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/arm64v8/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/99taxis/tomcat7 Tomcat7 1 [OK]
docker.io docker.io/camptocamp/tomcat-logback Docker image for tomcat with logback integ... 1 [OK]
docker.io docker.io/i386/tomcat Apache Tomcat is an open source implementa... 1
docker.io docker.io/ppc64le/tomcat Apache Tomcat is an open source implementa... 1
docker.io docker.io/appsvc/tomcat 0
docker.io docker.io/cfje/tomcat-resource Tomcat Concourse Resource 0
docker.io docker.io/jelastic/tomcat An image of the Tomcat Java application se... 0
docker.io docker.io/oobsri/tomcat8 Testing CI Jobs with different names. 0
docker.io docker.io/picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAG... 0 [OK]
docker.io docker.io/s390x/tomcat Apache Tomcat is an open source implementa... 0
docker.io docker.io/secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK]
Docker Hub:https://hub.docker.com
Docker镜像首页,包括官方镜像和其它公开镜像。Docker Hub上最受欢迎的10大镜像(通过Docker registry API获取不了镜像被pull的个数,只能通过镜像的stars数量,来衡量镜像的流行度。毫无疑问,拥有最高stars数量的库都是官方库).
使用命令拉取:
docker pull 镜像名字[:TAG]
目前国内访问docker hub速度上有点尴尬,使用docker Mirror势在必行。现有国内提供docker镜像加速服务的商家有不少,下面重点ustc镜像。
ustc是老牌的linux镜像服务提供者了,还在遥远的ubuntu 5.04版本的时候就在用。ustc的docker镜像加速器速度很快。ustc docker mirror的优势之一就是不需要注册,是真正的公共服务。
https://lug.ustc.edu.cn/wiki/mirrors/help/docker
步骤:
(1)编辑该文件:vi /etc/docker/daemon.json
// 如果该文件不存在就手动创建;说明:在centos7.x下,通过vi。
(2)在该文件中输入如下内容:
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
------------------------------------------------------------------------
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
3)注意:一定要重启docker服务,如果重启docker后无法加速,可以重新启动OS
[root@localhost docker]# service docker restart
Redirecting to /bin/systemctl restart docker.service
4)示例
[root@localhost docker]# docker pull zookeeper
Using default tag: latest
Trying to pull repository docker.io/library/zookeeper ...
latest: Pulling from docker.io/library/zookeeper
f5d23c7fed46: Pull complete
eaa7ca9a16a1: Pull complete
57f21f30136f: Pull complete
6e7c1070bcbc: Pull complete
f4d70619b3de: Pull complete
e4d5dc43f630: Pull complete
c9be43341e93: Pull complete
135ab2963089: Pull complete
Digest: sha256:4a70c5b011d94298f62c62d7341a374b6a3a5d15a246f76d4cba3584ae60b8f6
Status: Downloaded newer image for docker.io/zookeeper:latest
作用:删除镜像
删除单个
docker rmi -f 镜像ID
删除多个
docker rmi -f 镜像名1:TAG 镜像名2:TAG
删除全部
docker rmi -f $(docker images -qa)
示例:
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/zookeeper latest a873528df41f Less than a second ago 225 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
[root@localhost docker]# docker rmi -f a873528df41f
Untagged: docker.io/zookeeper:latest
Untagged: docker.io/zookeeper@sha256:4a70c5b011d94298f62c62d7341a374b6a3a5d15a246f76d4cba3584ae60b8f6
Deleted: sha256:a873528df41f67308900374218cbcb334bf764074f9daa76482d86e53f058589
Deleted: sha256:6112743ce75a12d3c5db9692c88cb4fd8f26bdacbee3fd6b2801a8a11d0cafa0
Deleted: sha256:474612b9b8b92623226ef0100c04598113a74ff9b34f3c47e9fc39e8cfcf1953
Deleted: sha256:136c24ea1ecb39f9070a8e2e7b6cedbc6e31a019d171910b7fb9f6efaa1f45d1
Deleted: sha256:af0ddb7072be87b262a98e8d9a30bb01e207ae44dfa7039beb09a4f7e85dfc19
Deleted: sha256:f60a1b0a8bdfbb66a0856863d4339802eeb7f593c4c4a6bddba425c278349fa1
Deleted: sha256:d4ba2b7e7a81205a5e6a7d35bc89b55dc5802fec6b5d35d93e4965e908b46d32
Deleted: sha256:4cfe7f0f8661eef6a97f30742474149c45ce22e7ca8f93e9e069abe333d2e470
Deleted: sha256:d8a33133e477d367977987129313d9072e0ec80894ed4c52c2d88186f354c29a
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
容器(Container)——镜像运行时的实体
镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等 。
容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的命名空间。前面讲过镜像使用的是分层存储,容器也是如此。
容器存储层的生存周期和容器一样,容器消亡时,容器存储层也随之消亡。因此,任何保存于容器存储层的信息都会随容器删除而丢失。
语法:
docker ps [OPTIONS]
OPTIONS说明(常用):
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
–no-trunc :不截断输出
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b3e9c904970 mysql "docker-entrypoint..." 3 months ago Exited (255) 4 hours ago 0.0.0.0:3306->3306/tcp, 33060/tcp my1024_mysql
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b3e9c904970 mysql "docker-entrypoint..." 3 months ago Exited (255) 4 hours ago 0.0.0.0:3306->3306/tcp, 33060/tcp my1024_mysql
[root@localhost ~]# docker ps -f status=exited
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b3e9c904970 mysql "docker-entrypoint..." 3 months ago Exited (255) 4 hours ago 0.0.0.0:3306->3306/tcp, 33060/tcp my1024_mysql
语法:
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
创建容器常用的参数说明:
-i:表示运行容器
-t:表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪终端。
--
name :为创建的容器命名。
-v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
-d:在run后面加上-d参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器,如果只加-i -t两个参数,创建后就会自动进去容器)。
-p:表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射
使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令,并取名为mycentos
docker run -it --name=mycentos centos:7 /bin/bash
[root@localhost ~]# docker run -it --name=mycentos centos:7 /bin/bash
Unable to find image 'centos:7' locally
Trying to pull repository docker.io/library/centos ...
7: Pulling from docker.io/library/centos
8ba884070f61: Pull complete
Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Status: Downloaded newer image for docker.io/centos:7
这时我们通过ps命令查看,发现可以看到启动的容器,状态为启动状态
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68af7641543c centos:7 "/bin/bash" 2 minutes ago Up 2 minutes mycentos
使用exit命令 退出当前容器
[root@68af7641543c /]# exit
exit
[root@localhost ~]#
然后用ps -a 命令查看发现该容器也随之停止:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68af7641543c centos:7 "/bin/bash" 3 minutes ago Exited (0) 28 seconds ago mycentos
8b3e9c904970 mysql "docker-entrypoint..." 3 months ago Exited (255) 4 hours ago 0.0.0.0:3306->3306/tcp, 33060/tcp my1024_mysql
创建一个守护式容器:如果对于一个需要长期运行的容器来说,我们可以创建一个守护式容器。命令如下(容器名称不能重复):
docker run -di --name=mycentos2 centos:7
[root@localhost ~]# docker run -di --name=mycentos2 centos:7
bf9248203458805ff33b997c50b89b58791ac1af21b22e2ab243a8bee2a0bf66
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf9248203458 centos:7 "/bin/bash" 19 seconds ago Up 18 seconds mycentos2
登录守护式容器方式:
docker exec -it container_name (或者 container_id) /bin/bash(exit退出时,容器不会停止)
[root@localhost ~]# docker exec -it mycentos2 /bin/bash
[root@bf9248203458 /]# ll
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 5 root root 340 Jun 17 11:30 dev
drwxr-xr-x. 1 root root 66 Jun 17 11:30 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 125 root root 0 Jun 17 11:30 proc
dr-xr-x---. 2 root root 114 Mar 5 17:36 root
drwxr-xr-x. 1 root root 21 Jun 17 11:30 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
[root@bf9248203458 /]#
docker stop $CONTAINER_NAME/ID
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf9248203458 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes mycentos2
[root@localhost ~]# docker stop mycentos2
mycentos2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker start $CONTAINER_NAME/ID
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker start mycentos2
mycentos2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf9248203458 centos:7 "/bin/bash" 5 minutes ago Up 3 seconds mycentos2
docker restart 容器ID或者容器名
docker kill 容器ID或者容器名
如果我们需要将文件拷贝到容器内可以使用cp命令
docker cp 需要拷贝的文件或目录 容器名称:容器目录
[root@localhost ~]# touch 1.txt
[root@localhost ~]# docker cp 1.txt mycentos2:/root/
[root@localhost ~]# docker exec -it mycentos2 /bin/bash
[root@bf9248203458 /]# cd /root/
[root@bf9248203458 ~]# ll
total 4
-rw-r--r--. 1 root root 0 Jun 17 11:42 1.txt
-rw-------. 1 root root 3415 Mar 5 17:36 anaconda-ks.cfg
也可以将文件从容器内拷贝出来
docker cp 容器名称:容器目录 需要拷贝的文件或目录
[root@localhost ~]# docker cp mycentos2:/root/111.txt /root/
[root@localhost ~]# cd /root/
[root@localhost ~]# ll
总用量 4
-rw-r--r--. 1 root root 0 6月 17 19:42 111.txt
-rw-r--r--. 1 root root 0 6月 17 19:42 1.txt
-rw-------. 1 root root 1257 2月 17 21:33 anaconda-ks.cfg
我们可以在创建容器的时候,将宿主机的目录与容器内的目录进行映射,这样我们就可以通过修改宿主机某个目录的文件从而去影响容器。
创建容器 添加-v参数 后边为 宿主机目录:容器目录
docker run -di --name=mycentos2 -v /usr/local/myhtml:/usr/local/myhtml centos:7
[root@localhost myhtml]# docker run -di --name=mycentos3 -v /usr/local/myhtml:/usr/local/myhtml centos:7
d6e67c10f59d2169ba73436d51253a6280aac1ca47ed5223c4591a90b8e2a31e
[root@localhost myhtml]# docker exec -it mycentos3 /bin/bash
[root@d6e67c10f59d /]# cd /usr/local/
[root@d6e67c10f59d local]# ll
total 0
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. 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 Jun 17 11:48 myhtml
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Mar 5 17:34 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
如果你共享的是多级的目录,可能会出现权限不足的提示。
这是因为CentOS7中的安全模块selinux把权限禁掉了,我们需要添加参数 --privileged=true 来解决挂载的目录没有权限的问题
docker run -di --name=mycentos3 -v /usr/local/myhtml:/usr/local/myhtml --privileged=true centos:7
我们可以通过以下命令查看容器运行的各种数据
[root@localhost myhtml]# docker inspect mycentos3
也可以直接执行下面的命令直接输出IP地址
[root@localhost myhtml]# docker inspect --format='{{.NetworkSettings.IPAddress}}' mycentos3
172.17.0.2
测试宿主机连接容器
[root@localhost myhtml]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.082 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.040 ms
64 bytes from 172.17.0.2: icmp_seq=5 ttl=64 time=0.061 ms
64 bytes from 172.17.0.2: icmp_seq=6 ttl=64 time=0.039 ms
64 bytes from 172.17.0.2: icmp_seq=7 ttl=64 time=0.040 ms
^C
--- 172.17.0.2 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6004ms
rtt min/avg/max/mdev = 0.039/0.057/0.100/0.023 ms
docker rm $CONTAINER_ID/NAME
注意,只能删除停止的容器
docker rm
docker ps -a -q
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码、运行时、库、环境变量和配置文件。
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等等。
平时我们安装进虚拟机的CentOS都是好几个G,为什么docker这里才200M??
对于一个精简的OS,rootfs可以很小,只需要包括最基本的命令、工具和程序库就可以了,因为底层直接用Host的kernel,自己只需要提供 rootfs 就行了。由此可见对于不同的linux发行版, bootfs基本是一致的, rootfs会有差别, 因此不同的发行版可以公用bootfs。
以我们的pull为例,在下载的过程中我们可以看到docker的镜像好像是在一层一层的在下载。
[root@localhost ~]# docker pull mongo
Using default tag: latest
Trying to pull repository docker.io/library/mongo ...
latest: Pulling from docker.io/library/mongo
f7277927d38a: Extracting [=> ] 1.376 MB/43.92 MB
8d3eac894db4: Download complete
edf72af6d627: Download complete
3e4f86211d23: Download complete
5747135f14d2: Download complete
f56f2c3793f6: Download complete
f8b941527f3a: Download complete
4000e5ef59f4: Download complete
ad518e2379cf: Download complete
919225fc3685: Download complete
45ff8d51e53a: Downloading [=====> ] 12.43 MB/104.8 MB
4d3342ddfd7b: Download complete
26002f176fca: Download complete
最大的一个好处就是 - 共享资源
比如:有多个镜像都从相同的 base 镜像构建而来,那么宿主机只需在磁盘上保存一份base镜像,
同时内存中也只需加载一份 base 镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。
Docker镜像都是只读的 当容器启动时,一个新的可写层被加载到镜像的顶部。 这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。
docker commit提交容器副本使之成为一个新的镜像
docker commit -m=“提交的描述信息” -a=“作者” 容器ID 要创建的目标镜像名:[标签名]
案例演示:
docker pull tomcat:7-jre7
[root@localhost ~]# docker pull tomcat:7-jre7
Trying to pull repository docker.io/library/tomcat ...
7-jre7: Pulling from docker.io/library/tomcat
db0035920883: Pull complete
a9ebd83b4a47: Pull complete
02ef9e65a664: Pull complete
b2786dccb0cc: Pull complete
1b809e89f352: Pull complete
bf313a79ccc8: Pull complete
e03e64e40a06: Pull complete
aec2911e85ab: Pull complete
b7b5b93b2e01: Pull complete
e1a8ee44b3b9: Pull complete
5f6f66788a60: Pull complete
Digest: sha256:d5bad6dde416e3214c88850ff1184de32d41afb10735b2594dd9e2d8f001fdcd
Status: Downloaded newer image for docker.io/tomcat:7-jre7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat 7-jre7 47c156f4d4e3 4 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
[root@localhost ~]# docker run -it -d -p 8888:8080 tomcat:7-jre7
2a0288c8cf76305ac9268073f4e7cc30d47fee4033d8944675fa864ddb0f2b02
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a0288c8cf76 tomcat:7-jre7 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:8888->8080/tcp optimistic_nobel
[root@localhost ~]# docker exec -it 2a0288c8cf76 /bin/bash
root@2a0288c8cf76:/usr/local/tomcat# ls -l
total 132
-rw-r--r--. 1 root root 18099 Apr 10 16:57 BUILDING.txt
-rw-r--r--. 1 root root 6090 Apr 10 16:57 CONTRIBUTING.md
-rw-r--r--. 1 root root 56846 Apr 10 16:57 LICENSE
-rw-r--r--. 1 root root 1241 Apr 10 16:57 NOTICE
-rw-r--r--. 1 root root 3255 Apr 10 16:57 README.md
-rw-r--r--. 1 root root 9365 Apr 10 16:57 RELEASE-NOTES
-rw-r--r--. 1 root root 16978 Apr 10 16:57 RUNNING.txt
drwxr-xr-x. 2 root root 4096 May 14 01:12 bin
drwxr-xr-x. 1 root root 22 Jun 17 14:10 conf
drwxr-sr-x. 2 root staff 78 May 14 01:11 include
drwxr-xr-x. 2 root root 4096 May 14 01:10 lib
drwxrwxrwx. 1 root root 177 Jun 17 14:10 logs
drwxr-sr-x. 3 root staff 151 May 14 01:11 native-jni-lib
drwxr-xr-x. 2 root root 30 May 14 01:10 temp
drwxr-xr-x. 7 root root 81 Apr 10 16:57 webapps
drwxrwxrwx. 1 root root 22 Jun 17 14:10 work
root@2a0288c8cf76:/usr/local/tomcat# cd webapps/
root@2a0288c8cf76:/usr/local/tomcat/webapps# ls -l
total 8
drwxr-xr-x. 3 root root 4096 May 14 01:10 ROOT
drwxr-xr-x. 14 root root 4096 May 14 01:10 docs
drwxr-xr-x. 7 root root 111 May 14 01:10 examples
drwxr-xr-x. 5 root root 87 May 14 01:10 host-manager
drwxr-xr-x. 5 root root 103 May 14 01:10 manager
root@2a0288c8cf76:/usr/local/tomcat/webapps# rm -rf docs/
root@2a0288c8cf76:/usr/local/tomcat/webapps#
也即当前的tomcat运行实例是一个没有文档内容的容器,
以它为模板commit一个没有doc的tomcat新镜像bruceliu/tomcat02
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a0288c8cf76 tomcat:7-jre7 "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:8888->8080/tcp optimistic_nobel
[root@localhost ~]# docker commit -a="bruce" -m="delete the tomcat docs" 2a0288c8cf76 bruceliu/tomcat:7-jre7
sha256:a66f9aba334d3a7252d0b9114aa14eff31f3a1641946b5b08cbcd86b5632d84e
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bruceliu/tomcat 7-jre7 a66f9aba334d 9 seconds ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 4 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
启动我们的新镜像并和原来的对比,启动bruceliu/tomcat02,它没有docs
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bruceliu/tomcat 7-jre7 a66f9aba334d 2 minutes ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 4 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
[root@localhost ~]# docker run -it -p 7777:8080 bruceliu/tomcat
Unable to find image 'bruceliu/tomcat:latest' locally
Trying to pull repository docker.io/bruceliu/tomcat ...
^C
[root@localhost ~]# ^C
[root@localhost ~]# docker run -it -p -d 7777:8080 bruceliu/tomcat:7-jre7
/usr/bin/docker-current: Invalid containerPort: -d.
See '/usr/bin/docker-current run --help'.
[root@localhost ~]# docker run -it -d -p 7777:8080 bruceliu/tomcat:7-jre7
9dfea93d6a81136e1b8b517313aeec129fe973008af07d4d6dd30de9e829775f
新启动原来的tomcat,它有docs
[root@localhost ~]# docker run -it -d -p 8888:8080 docker.io/tomcat:7-jre7
51a4fdbd61d412200917b54a247f983e311ac8a5b498b8f9821b9c2f706e4d69
先来看看Docker的理念:
Docker容器产生的数据,如果不通过docker commit生成新的镜像,使得数据做为镜像的一部分保存下来,那么当容器删除后,数据自然也就没有了。为了能保存数据在docker中我们使用卷。
卷就是目录或文件,存在于一个或多个容器中,由docker挂载到容器,但不属于联合文件系统,因此能够绕过Union File System提供一些用于持续存储或共享数据的特性:
卷的设计目的就是数据的持久化,完全独立于容器的生存周期,因此Docker不会在容器删除时删除其挂载的数据卷
特点:
1:数据卷可在容器之间共享或重用数据
2:卷中的更改可以直接生效
3:数据卷中的更改不会包含在镜像的更新中
4:数据卷的生命周期一直持续到没有容器使用它为止
容器的持久化
容器间继承+共享数据
语法:
docker run -it -v /宿主机绝对路径目录:/容器内目录 镜像名
示例:
[root@localhost ~]# docker run -di --name=mycentos0727 -v /localdata:/containerdata centos:7
c54a48d9e8479c7518d9aa4edd6cae5c9f453ceea878cd6d71423896f4f37724
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c54a48d9e847 centos:7 "/bin/bash" 15 seconds ago Up 14 seconds mycentos0727
[root@localhost ~]# docker exec -it mycentos0727 /bin/bash
[root@c54a48d9e847 /]# ls -l
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Jun 17 19:55 containerdata
drwxr-xr-x. 5 root root 340 Jun 17 19:55 dev
drwxr-xr-x. 1 root root 66 Jun 17 19:55 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 119 root root 0 Jun 17 19:55 proc
dr-xr-x---. 2 root root 114 Mar 5 17:36 root
drwxr-xr-x. 1 root root 21 Jun 17 19:55 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
[root@c54a48d9e847 /]# cd containerdata/
[root@c54a48d9e847 containerdata]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 19:58 1.txt
[root@c54a48d9e847 containerdata]# cat 1.txt
[root@c54a48d9e847 containerdata]# cat 1.txt
23423432
234234234
[root@c54a48d9e847 containerdata]#
查看数据卷是否挂载成功
docker inspect 容器ID
"HostConfig": {
"Binds": [
"/localdata:/containerdata"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
容器和宿主机之间数据共享
[root@localhost ~]# docker run -di --name=mycentos0727 -v /localdata:/containerdata centos:7
c54a48d9e8479c7518d9aa4edd6cae5c9f453ceea878cd6d71423896f4f37724
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c54a48d9e847 centos:7 "/bin/bash" 15 seconds ago Up 14 seconds mycentos0727
[root@localhost ~]# docker exec -it mycentos0727 /bin/bash
[root@c54a48d9e847 /]# ls -l
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Jun 17 19:55 containerdata
drwxr-xr-x. 5 root root 340 Jun 17 19:55 dev
drwxr-xr-x. 1 root root 66 Jun 17 19:55 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 119 root root 0 Jun 17 19:55 proc
dr-xr-x---. 2 root root 114 Mar 5 17:36 root
drwxr-xr-x. 1 root root 21 Jun 17 19:55 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
[root@c54a48d9e847 /]# cd containerdata/
[root@c54a48d9e847 containerdata]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 19:58 1.txt
[root@c54a48d9e847 containerdata]# cat 1.txt
[root@c54a48d9e847 containerdata]# cat 1.txt
23423432
234234234
[root@c54a48d9e847 containerdata]
[root@localhost localdata]# pwd
/localdata
[root@localhost localdata]# ll
总用量 4
-rw-r--r--. 1 root root 19 6月 18 03:59 1.txt
[root@localhost localdata]# cat 1.txt
23423432
234234234
[root@localhost localdata]#
容器停止退出后,主机修改后数据是否同步
容器先停止退出
docker stop c54a48d9e847
主机修改文件
容器重新启动
docker start c54a48d9e847
docker exec -it mycentos0727 /bin/bash
查看挂载的目录文件
[root@c54a48d9e847 /]# cd containerdata/
[root@c54a48d9e847 containerdata]# ll
total 8
-rw-r--r--. 1 root root 19 Jun 17 19:59 1.txt
-rw-r--r--. 1 root root 1555 Jun 17 20:14 2.txt
[root@c54a48d9e847 containerdata]# cat 2.txt
发现内容还是同步的.
备注:
根目录下新建mydocker文件夹并进入
可在Dockerfile中使用VOLUME指令来给镜像添加一个或多个数据卷
VOLUME[“/dataVolumeContainer”,“/dataVolumeContainer2”,“/dataVolumeContainer3”]
说明:
出于可移植和分享的考虑,用-v 主机目录:容器目录这种方法不能够直接在Dockerfile中实现。
由于宿主机目录是依赖于特定宿主机的,并不能够保证在所有的宿主机上都存在这样的特定目录。
[root@localhost /]# mkdir mydocker
[root@localhost /]# cd mydocker
[root@localhost /mydocker]# vi mydockerfile
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------success1"
CMD /bin/bash
[root@localhost mydocker]# docker build -f /mydocker/mydockerfile -t bruceliu/centos9527 .
Sending build context to Docker daemon 2.048 kB
Step 1/4 : FROM centos
---> 9f38484d220f
Step 2/4 : VOLUME /dataVolumeContainer1 /dataVolumeContainer2
---> Running in 1b286372176e
---> 96c20bf65a22
Removing intermediate container 1b286372176e
Step 3/4 : CMD echo "finished,--------success1"
---> Running in 316de82a95a1
---> 763314c45154
Removing intermediate container 316de82a95a1
Step 4/4 : CMD /bin/bash
---> Running in 4926a2fb1fa3
---> 071e944a589b
Removing intermediate container 4926a2fb1fa3
Successfully built 071e944a589b
[root@localhost mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bruceliu/centos9527 latest 071e944a589b 41 seconds ago 202 MB
bruceliu/tomcat 7-jre7 a66f9aba334d 4 hours ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 4 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
docker.io/centos latest 9f38484d220f 3 months ago 202 MB
获得一个新镜像bruceliu/centos9527
[root@localhost mydocker]# docker run -it 071e944a589b /bin/bash
[root@84e0c3a6dcdf /]# ls -l
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Jun 17 21:17 dataVolumeContainer1
drwxr-xr-x. 2 root root 6 Jun 17 21:18 dataVolumeContainer2
drwxr-xr-x. 5 root root 360 Jun 17 21:18 dev
drwxr-xr-x. 1 root root 66 Jun 17 21:17 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 119 root root 0 Jun 17 21:18 proc
dr-xr-x---. 2 root root 114 Mar 5 17:36 root
drwxr-xr-x. 1 root root 21 Jun 17 21:18 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
通过上述步骤,容器内的卷目录地址已经知道
对应的主机目录地址哪??
[root@84e0c3a6dcdf /]# cd dataVolumeContainer
bash: cd: dataVolumeContainer: No such file or directory
[root@84e0c3a6dcdf /]#
[root@84e0c3a6dcdf /]# ls -l
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Jun 17 21:17 dataVolumeContainer1
drwxr-xr-x. 2 root root 6 Jun 17 21:18 dataVolumeContainer2
drwxr-xr-x. 5 root root 360 Jun 17 21:18 dev
drwxr-xr-x. 1 root root 66 Jun 17 21:17 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 119 root root 0 Jun 17 21:18 proc
dr-xr-x---. 2 root root 114 Mar 5 17:36 root
drwxr-xr-x. 1 root root 21 Jun 17 21:18 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
[root@84e0c3a6dcdf /]# cd dataVolumeContainer1
[root@84e0c3a6dcdf dataVolumeContainer1]# pwd
/dataVolumeContainer1
[root@84e0c3a6dcdf dataVolumeContainer1]# touch 1.txt
[root@84e0c3a6dcdf dataVolumeContainer1]#
[root@localhost _data]# pwd
/var/lib/docker/volumes/47f4c73f4f71d3376b7f3df75e2e6877c50f7c1446a1cc2ebe07d82387b6372b/_data
[root@localhost _data]# ll
总用量 0
-rw-r--r--. 1 root root 0 6月 18 05:19 1.txt
-rw-r--r--. 1 root root 0 6月 18 05:22 2.txt
备注:
Docker挂载主机目录Docker访问出现cannot open directory .: Permission denied
解决办法:在挂载目录后多加一个–privileged=true参数即可
命名的容器挂载数据卷,其它容器通过挂载这个(父容器)实现数据共享,挂载数据卷的容器,称之为数据卷容器.
以上一步新建的镜像bruceliu/cento9527为模板并运行容器dc01/dc02/dc03
它们已经具有容器卷
/dataVolumeContainer1
/dataVolumeContainer2
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bruceliu/centos9527 latest 071e944a589b 17 minutes ago 202 MB
bruceliu/tomcat 7-jre7 a66f9aba334d 4 hours ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 4 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
docker.io/centos latest 9f38484d220f 3 months ago 202 MB
[root@localhost /]#
[root@localhost /]# docker run -it --name dc01 bruceliu/centos9527
[root@43266366c5bc /]# ls -l
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Jun 17 21:33 dataVolumeContainer1
drwxr-xr-x. 2 root root 6 Jun 17 21:33 dataVolumeContainer2
drwxr-xr-x. 5 root root 360 Jun 17 21:33 dev
drwxr-xr-x. 1 root root 66 Jun 17 21:33 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 116 root root 0 Jun 17 21:33 proc
dr-xr-x---. 2 root root 114 Mar 5 17:36 root
drwxr-xr-x. 1 root root 21 Jun 17 21:33 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
[root@43266366c5bc /]# cd dataVolumeContainer2
[root@43266366c5bc dataVolumeContainer2]# touch dc01_add.txt
dc02/dc03继承自dc01
命令:–volumes-from
docker run -it --name dc02 --volumes-from dc01 zzyy/centos
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43266366c5bc bruceliu/centos9527 "/bin/sh -c /bin/bash" 2 minutes ago Up 2 minutes dc01
[root@localhost ~]# docker attach 43266366c5bc
[root@43266366c5bc dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
[root@43266366c5bc dataVolumeContainer2]# exit
exit
[root@localhost ~]# docker run -it --name dc02 --volumes-from dc01 bruceliu/centos9527
[root@8b6c85d88968 /]# cd dataVolumeContainer2
[root@8b6c85d88968 dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
[root@8b6c85d88968 dataVolumeContainer2]# touch dc02_add.txt
[root@8b6c85d88968 dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:38 dc02_add.txt
[root@8b6c85d88968 dataVolumeContainer2]# exit
exit
[root@localhost ~]# docker run -it --name dc03 --volumes-from dc01 bruceliu/centos9527
[root@2d3ff41a5f00 /]#
[root@2d3ff41a5f00 /]# cd dataVolumeContainer2
[root@2d3ff41a5f00 dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:38 dc02_add.txt
[root@2d3ff41a5f00 dataVolumeContainer2]# touch dc03_add.txt
[root@2d3ff41a5f00 dataVolumeContainer2]#
回到dc01可以看到02/03各自添加的都能共享了
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d3ff41a5f00 bruceliu/centos9527 "/bin/sh -c /bin/bash" 2 minutes ago Up 2 minutes dc03
8b6c85d88968 bruceliu/centos9527 "/bin/sh -c /bin/bash" 3 minutes ago Up 6 seconds dc02
43266366c5bc bruceliu/centos9527 "/bin/sh -c /bin/bash" 8 minutes ago Up 9 seconds dc01
[root@localhost /]# docker attach 43266366c5bc
[root@43266366c5bc /]# ls -l
total 12
-rw-r--r--. 1 root root 12082 Mar 5 17:36 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x. 2 root root 6 Jun 17 21:33 dataVolumeContainer1
drwxr-xr-x. 2 root root 66 Jun 17 21:39 dataVolumeContainer2
drwxr-xr-x. 5 root root 360 Jun 17 21:41 dev
drwxr-xr-x. 1 root root 66 Jun 17 21:33 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 122 root root 0 Jun 17 21:41 proc
dr-xr-x---. 1 root root 27 Jun 17 21:37 root
drwxr-xr-x. 1 root root 21 Jun 17 21:33 run
lrwxrwxrwx. 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 Jun 10 16:40 sys
drwxrwxrwt. 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x. 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x. 18 root root 238 Mar 5 17:34 var
[root@43266366c5bc /]# cd dataVolumeContainer2
[root@43266366c5bc dataVolumeContainer2]#
[root@43266366c5bc dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:38 dc02_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:39 dc03_add.txt
删除dc01,dc02修改后dc03可否访问
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d3ff41a5f00 bruceliu/centos9527 "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes dc03
8b6c85d88968 bruceliu/centos9527 "/bin/sh -c /bin/bash" 7 minutes ago Up 3 minutes dc02
[root@localhost /]# docker attach dc02
[root@8b6c85d88968 /]# cd dataVolumeContainer2
[root@8b6c85d88968 dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:38 dc02_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:39 dc03_add.txt
[root@8b6c85d88968 dataVolumeContainer2]# touch dc02_add_new.txt
[root@8b6c85d88968 dataVolumeContainer2]# exit
exit
[root@localhost /]# docker attach dc03
[root@2d3ff41a5f00 dataVolumeContainer2]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jun 17 21:34 dc01_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:38 dc02_add.txt
-rw-r--r--. 1 root root 0 Jun 17 21:45 dc02_add_new.txt
-rw-r--r--. 1 root root 0 Jun 17 21:39 dc03_add.txt
结论:
容器之间配置信息的传递,数据卷的生命周期一直持续到没有容器使用它为止
当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像
Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。
Dockerfile面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可,合力充当Docker体系的基石。
Dockerfile,需要定义一个Dockerfile,Dockerfile定义了进程需要的一切东西。Dockerfile涉及的内容包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程(当应用进程需要和系统服务和内核进程打交道,这时需要考虑如何设计namespace的权限控制)等等;
在Docker中创建镜像最常用的方式,就是使用Dockerfile。Dockerfile是一个Docker镜像的描述文件,我们可以理解成火箭发射的A、B、C、D…的步骤。Dockerfile其内部包含了一条条的指令,每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。
Docker镜像,在用Dockerfile定义一个文件之后,docker build时会产生一个Docker镜像,当运行 Docker镜像时,会真正开始提供服务;
Docker容器,容器是直接提供服务的。
构建三步骤
编写Dockerfile文件
docker build
docker run
网站:https://hub.docker.com/_/centos/
#基于centos镜像
FROM centos
#维护人的信息
MAINTAINER The CentOS Project <303323496@qq.com>
#安装httpd软件包
RUN yum -y update
RUN yum -y install httpd
#开启80端口
EXPOSE 80
#复制网站首页文件至镜像中web站点下
ADD index.html /var/www/html/index.html
#复制该脚本至镜像中,并修改其权限
ADD run.sh /run.sh
RUN chmod 775 /run.sh
#当启动容器时执行的脚本文件
CMD ["/run.sh"]
由上可知,Dockerfile结构大致分为四个部分:
(1)基础镜像信息
(2)维护者信息
(3)镜像操作指令
(4)容器启动时执行指令。
Dockerfile每行支持一条指令,每条指令可带多个参数,支持使用以#号开头的注释。下面会对上面使用到的一些常用指令做一些介绍。
Dockerfile内容基础知识
1:每条保留字指令都必须为大写字母且后面要跟随至少一个参数
2:指令按照从上到下,顺序执行
3:#表示注释
4:每条指令都会创建一个新的镜像层,并对镜像进行提交
Dockerfile内容基础知识
1:docker从基础镜像运行一个容器
2:执行一条指令并对容器作出修改
3:执行类似docker commit的操作提交一个新的镜像层
4:docker再基于刚提交的镜像运行一个新容器
5:执行dockerfile中的下一条指令直到所有指令都执行完成
小结
从应用软件的角度来看,Dockerfile、Docker镜像与Docker容器分别代表软件的三个不同阶段:
1:Dockerfile是软件的原材料
2:Docker镜像是软件的交付品
3:Docker容器则可以认为是软件的运行态。
指明构建的新镜像是来自于哪个基础镜像,例如:
FROM centos:6
基础镜像不存在会在Docker Hub上拉去(一般会是文件的第一个指令) 使用格式:
FROM <镜像>:[tag]
指明镜像维护着及其联系方式(一般是邮箱地址),例如
MAINTAINER BRUCELIU
不过,MAINTAINER并不推荐使用,更推荐使用LABEL来指定镜像作者,例如:
LABEL maintainer="bruceliu.cn"
[逐渐废弃] LABLE --替代MAINTANIER 具体使用: LABLE maintainer="作者信息
RUN 指令是用来执行命令行命令的。由于命令行的强大能力,RUN 指令在定制镜像时是最常用的指令之一。其格式有两种:
• shell 格式:RUN <命令>,就像直接在命令行中输入的命令一样。刚才写的 Dockerfile 中的 RUN 指令就是这种格式.
• exec 格式:RUN [“可执行文件”, “参数1”, “参数2”],这更像是函数调用中的格式。
使用格式:
RUN RUN [“”,“”,“”]
构建镜像时运行的Shell命令,例如:
RUN ["yum", "install", "httpd"]
RUN yum install httpd
RUN 就像 Shell 脚本一样可以执行命令,那么我们是否就可以像 Shell 脚本一样把每个命令对应一个 RUN 呢?比如
这样:
RUN apt-get update
RUN apt-get install -y gcc libc6-dev make
RUN wget http://download.redis.io/releases/redis-4.0.1.tar.gz
RUN tar xzf redis-4.0.1.tar.gz
RUN cd redis-4.0.1
Dockerfile 中每一个指令都会建立一层,RUN 也不例外。每一个 RUN 的行为,和刚才我们手工建立镜像的过程一样:新建立一层,在其上执行这些命令,执行结束后,commit 这一层的修改,构成新的镜像。 而上面的这种写法,创建了 多 层镜像。这是完全没有意义的,而且很多运行时不需要的东西,都被装进了镜像里,比如编译环境、更新的软件包等等。结果就是产生非常臃肿、非常多层的镜像,不仅仅增加了构建部署的时间,也很容易出错。 这是很多初学 Docker 的人常犯的一个错误。
Union FS 是有最大层数限制的,比如 AUFS,曾经是最大不得超过 42 层,现在是不得超过 127 层。上面的Dockerfile 正确的写法应该是这样
FROM centos
RUN apt-get update \
&& apt-get install -y gcc libc6-dev make \
&& wget http://download.redis.io/releases/redis-4.0.1.tar.gz \
&& tar xzf redis-4.0.1.tar.gz \
&& cd redis-4.0.1
首先,之前所有的命令只有一个目的,就是编译、安装 redis 可执行文件。因此没有必要建立很多层,这只是一层的事情。因此,这里没有使用很多个 RUN 对一一对应不同的命令,而是仅仅使用一个 RUN 指令,并使用 && 将各个所需命令串联起来。将之前的 7 层,简化为了 1 层。
在撰写 Dockerfile 的时候,要经常提醒自己,这并不是在写Shell 脚本,而是在定义每一层该如何构建且,这里为了格式化还进行了换行。Dockerfile 支持 Shell 类的行尾添加 \ 的命令换行方式,以及行首 # 进行注释的格式。良好的格式,比如换行、缩进、注释等,会让维护、排障更为容易,这是一个比较好的习惯。
启动容器时执行的Shell命令,例如:
CMD ["-C", "/start.sh"]
CMD ["/usr/sbin/sshd", "-D"]
CMD /usr/sbin/sshd -D
声明容器运行的服务端口,为容器打开指定要监听的端口以实现与外部通信,例如:
EXPOSE 80 443
ENV指令可以用于为docker容器设置环境变量 ENV设置的环境变量,可以使用 docker inspect命令来查看。同时还可以使用docker run --env =来修改环境变量。
具体用法:
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin/
类似COPY命令,拷贝文件或目录到镜像中,例如:
ADD ...
ADD html.tar.gz /var/www/html
ADD https://xxx.com/html.tar.gz /var/www/html
拷贝文件或目录到镜像中,用法同ADD,只是不支持自动下载和解压,例如:
COPY ./start.sh /start.sh
启动容器时执行的Shell命令,同CMD类似,只是由ENTRYPOINT启动的程序不会被docker run命令行指定的参数所覆盖,而且,这些命令行参数会被当作参数传递给ENTRYPOINT指定指定的程序,例如:
ENTRYPOINT ["/bin/bash", "-C", "/start.sh"]
ENTRYPOINT /bin/bash -C '/start.sh'
PS:Dockerfile文件中也可以存在多个ENTRYPOINT指令,但仅有最后一个会生效。
创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等。例如:
VOLUME ["/var/lib/mysql"]
只能定义docker管理的卷: VOLUME /data/mysql运行的时候会随机在宿主机的目录下生成一个卷目录!
一般不会在Dockerfile中用到,更常见的还是在docker run的时候指定-v数据卷。
为RUN、CMD和ENTRYPOINT执行Shell命令指定运行用户,例如:
USER [:]
USER [:]
USER bruceliu
Docker 默认的工作目录是/,只有 RUN 能执行 cd 命令切换目录,而且还只作用在当下下的 RUN,也就是说每一个 RUN 都是独立进行的。
如果想让其他指令在指定的目录下执行,就得靠 WORKDIR。WORKDIR 动作的目录改变是持久的,不用每个指令前都使用一次 WORKDIR。
WORKDIR /usr/local/tomcat/
Docker Hub 中 99% 的镜像都是通过在 base 镜像中安装和配置需要的软件构建出来的
[root@localhost myfile]# pwd
/root/myfile
[root@localhost myfile]# vi mycentos_dockerfile
FROM centos
MAINTAINER bruceliu<bruceliu167@126.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 "success--------------ok"
CMD /bin/bash
docker build -t 新镜像名字:TAG .
会看到 docker build 命令最后有一个 .
. 表示当前目录
[root@localhost myfile]# docker build -f /root/myfile/mycentos_dockerfile -t mycentos:1.3 .
Sending build context to Docker daemon 2.048 kB
Step 1/10 : FROM centos
---> 9f38484d220f
Step 2/10 : MAINTAINER bruceliu<bruceliu167@126.com>
---> Using cache
---> 281446cb753d
Step 3/10 : ENV MYPATH /usr/local
---> Using cache
---> 2e09cdb61576
Step 4/10 : WORKDIR $MYPATH
---> Using cache
---> 4aa136d65f26
Step 5/10 : RUN yum -y install vim
---> Using cache
---> 585392f09407
Step 6/10 : RUN yum -y install net-tools
---> Using cache
---> 9c7971b08e59
Step 7/10 : EXPOSE 80
---> Using cache
---> 6741c0b61390
Step 8/10 : CMD echo $MYPATH
---> Using cache
---> fb1b4b72ba02
Step 9/10 : CMD echo "success--------------ok"
---> Using cache
---> 3d636347cc9a
Step 10/10 : CMD /bin/bash
---> Using cache
---> 720d4eb5c5f0
Successfully built 720d4eb5c5f0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mongo latest 9c02a5a12c52 Less than a second ago 413 MB
mycentos 1.3 720d4eb5c5f0 50 minutes ago 480 MB
bruceliu/centos9527 latest 071e944a589b 8 hours ago 202 MB
bruceliu/tomcat 7-jre7 a66f9aba334d 13 hours ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 5 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
docker.io/centos latest 9f38484d220f 3 months ago 202 MB
[root@localhost ~]# docker run -it mycentos:1.3
[root@50784a17d114 local]# pwd
/usr/local
[root@50784a17d114 local]# vi 1.txt
[root@50784a17d114 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:3 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 656 (656.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker history 镜像名
[root@localhost ~]# docker history 720d4eb5c5f0
IMAGE CREATED CREATED BY SIZE COMMENT
720d4eb5c5f0 53 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/b... 0 B
3d636347cc9a 53 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "ec... 0 B
fb1b4b72ba02 53 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "ec... 0 B
6741c0b61390 53 minutes ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B
9c7971b08e59 53 minutes ago /bin/sh -c yum -y install net-tools 112 MB
585392f09407 54 minutes ago /bin/sh -c yum -y install vim 167 MB
4aa136d65f26 About an hour ago /bin/sh -c #(nop) WORKDIR /usr/local 0 B
2e09cdb61576 About an hour ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0 B
281446cb753d About an hour ago /bin/sh -c #(nop) MAINTAINER bruceliu<bru... 0 B
9f38484d220f 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.... 0 B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:074f2c974463ab3... 202 MB
都是指定一个容器启动时要运行的命令
[root@localhost ~]# docker run -it -p 8888:8080 docker.io/tomcat ls -l
total 124
-rw-r--r--. 1 root root 19534 Jul 4 2019 BUILDING.txt
-rw-r--r--. 1 root root 5407 Jul 4 2019 CONTRIBUTING.md
-rw-r--r--. 1 root root 57011 Jul 4 2019 LICENSE
-rw-r--r--. 1 root root 1726 Jul 4 2019 NOTICE
-rw-r--r--. 1 root root 3255 Jul 4 2019 README.md
-rw-r--r--. 1 root root 7139 Jul 4 2019 RELEASE-NOTES
-rw-r--r--. 1 root root 16262 Jul 4 2019 RUNNING.txt
drwxr-xr-x. 2 root root 4096 Jul 18 2019 bin
drwxr-sr-x. 2 root root 238 Jul 4 2019 conf
drwxr-sr-x. 2 root staff 78 Jul 18 2019 include
drwxr-xr-x. 2 root root 4096 Jul 18 2019 lib
drwxrwxrwx. 2 root root 6 Jul 4 2019 logs
drwxr-sr-x. 3 root staff 151 Jul 18 2019 native-jni-lib
drwxr-xr-x. 2 root root 30 Jul 18 2019 temp
drwxr-xr-x. 7 root root 81 Jul 4 2019 webapps
drwxrwxrwx. 2 root root 6 Jul 4 2019 work
curl命令可以用来执行下载、发送各种HTTP请求,指定HTTP头部等操作。
如果系统没有curl可以使用yum install curl安装,也可以下载安装。
curl是将下载文件输出到stdout
使用命令:curl http://www.baidu.com
执行后,www.baidu.com的html就会显示在屏幕上了
这是最简单的使用方法。用这个命令获得了http://curl.haxx.se指向的页面,同样,如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地。如果下载的是HTML文档,那么缺省的将只显示文件头部,即HTML文档的header。要全部显示,请加参数 -i
[root@localhost myfile]# vi myip_dockerfile
FROM centos
RUN yum install -y curl
CMD [ "curl", "-s", "https://www.baidu.com" ]
[root@localhost myfile]# docker build -f myip_dockerfile -t myip_centos .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM centos
---> 9f38484d220f
Step 2/3 : RUN yum install -y curl
---> Running in 617a61151503
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package curl.x86_64 0:7.29.0-51.el7 will be updated
---> Package curl.x86_64 0:7.29.0-51.el7_6.3 will be an update
--> Processing Dependency: libcurl = 7.29.0-51.el7_6.3 for package: curl-7.29.0-51.el7_6.3.x86_64
--> Running transaction check
---> Package libcurl.x86_64 0:7.29.0-51.el7 will be updated
---> Package libcurl.x86_64 0:7.29.0-51.el7_6.3 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
curl x86_64 7.29.0-51.el7_6.3 updates 269 k
Updating for dependencies:
libcurl x86_64 7.29.0-51.el7_6.3 updates 222 k
Transaction Summary
================================================================================
Upgrade 1 Package (+1 Dependent package)
Total download size: 492 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/updates/packages/curl-7.29.0-51.el7_6.3.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for curl-7.29.0-51.el7_6.3.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total 777 kB/s | 492 kB 00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) "
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : libcurl-7.29.0-51.el7_6.3.x86_64 1/4
Updating : curl-7.29.0-51.el7_6.3.x86_64 2/4
Cleanup : curl-7.29.0-51.el7.x86_64 3/4
Cleanup : libcurl-7.29.0-51.el7.x86_64 4/4
Verifying : curl-7.29.0-51.el7_6.3.x86_64 1/4
Verifying : libcurl-7.29.0-51.el7_6.3.x86_64 2/4
Verifying : libcurl-7.29.0-51.el7.x86_64 3/4
Verifying : curl-7.29.0-51.el7.x86_64 4/4
Updated:
curl.x86_64 0:7.29.0-51.el7_6.3
Dependency Updated:
libcurl.x86_64 0:7.29.0-51.el7_6.3
Complete!
---> c0ab6e8f3af7
Removing intermediate container 617a61151503
Step 3/3 : CMD curl -s https://www.baidu.com
---> Running in 9933fbe7593b
---> 41868e4e4fee
Removing intermediate container 9933fbe7593b
Successfully built 41868e4e4fee
查看生成的镜像
[root@localhost myfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mongo latest 9c02a5a12c52 Less than a second ago 413 MB
docker.io/tomcat latest 238e6d7313e3 Less than a second ago 506 MB
myip_centos latest 41868e4e4fee 13 minutes ago 314 MB
mycentos 1.3 720d4eb5c5f0 About an hour ago 480 MB
bruceliu/centos9527 latest 071e944a589b 9 hours ago 202 MB
bruceliu/tomcat 7-jre7 a66f9aba334d 13 hours ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 5 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
docker.io/centos latest 9f38484d220f 3 months ago 202 MB
[root@localhost myfile]# docker run 41868e4e4fee
问题
如果我们希望显示 HTTP 头信息,就需要加上 -i 参数
[root@localhost myfile]# docker run 41868e4e4fee -i
container_linux.go:235: starting container process caused "exec: \"-i\": executable file not found in $PATH"
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "exec: \"-i\": executable file not found in $PATH".
WHY
我们可以看到可执行文件找不到的报错,executable file not found。
之前我们说过,跟在镜像名后面的是 command,运行时会替换 CMD 的默认值。
因此这里的 -i 替换了原来的 CMD,而不是添加在原来的 curl -s https://www.baidu.com 后面。而 -i 根本不是命令,所以自然找不到。
那么如果我们希望加入 -i 这参数,我们就必须重新完整的输入这个命令:
$ docker run myip curl -s http://ip.cn -i
制作ENTROYPOINT版查询IP信息的容器
[root@localhost myfile]# docker build -f myip1_dockerfile -t myip1_centos .
Sending build context to Docker daemon 4.096 kB
Step 1/3 : FROM centos
---> 9f38484d220f
Step 2/3 : RUN yum install -y curl
---> Using cache
---> c0ab6e8f3af7
Step 3/3 : ENTRYPOINT curl -s https://www.baidu.com
---> Running in b31022ed350c
---> 274c58d0c1e4
Removing intermediate container b31022ed350c
Successfully built 274c58d0c1e4
[root@localhost myfile]# docker run 274c58d0c1e4
[root@localhost myfile]# docker run 274c58d0c1e4 -i
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Sun, 29 Dec 2019 03:49:34 GMT
Etag: "588604c8-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
① mkdir -p /bruce/mydockerfile/tomcat9
[root@localhost tomcat9]# pwd
/bruce/mydockerfile/tomcat9
② 在上述目录下touch c.txt
[root@localhost tomcat9]# touch c.txt
[root@localhost tomcat9]# ll
总用量 0
-rw-r--r--. 1 root root 0 6月 18 14:41 c.txt
③ 将jdk和tomcat安装的压缩包拷贝进上一步目录
-rw-r--r--. 1 root root 8900822 12月 18 2016 apache-tomcat-7.0.68.tar.gz
-rw-r--r--. 1 root root 0 6月 18 14:41 c.txt
-rw-r--r--. 1 root root 0 6月 18 14:45 Dockerfile
-rw-r--r--. 1 root root 138094686 12月 13 2016 jdk-7u45-linux-x64.tar.gz
④ 在/bruceliu/mydockerfile/tomcat9目录下新建Dockerfile文件
FROM centos
MAINTAINER bruce<[email protected]>
#把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下
COPY c.txt /usr/local/cincontainer.txt
#把java与tomcat添加到容器中
ADD jdk-7u45-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-7.0.68.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.7.0_45
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.68
ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.68
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat
# ENTRYPOINT ["/usr/local/apache-tomcat-7.0.68/bin/startup.sh" ]
# CMD ["/usr/local/apache-tomcat-7.0.68/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-7.0.68/bin/startup.sh && tail -F /usr/local/apache-tomcat-7.0.68/logs/catalina.out
⑤ 构建
[root@localhost tomcat9]# docker build -t brucrliu_tomcat .
Sending build context to Docker daemon 435.5 MB
Step 1/15 : FROM centos
---> 9f38484d220f
Step 2/15 : MAINTAINER bruce<[email protected]>
---> Running in 87cf2c0bccc2
---> adc219f3a447
Removing intermediate container 87cf2c0bccc2
Step 3/15 : COPY c.txt /usr/local/cincontainer.txt
---> 6e86a4c4315b
Removing intermediate container ef939f65bb42
Step 4/15 : ADD jdk-7u45-linux-x64.tar.gz /usr/local/
---> 9127c8831d40
Removing intermediate container 4b2acf47238b
Step 5/15 : ADD apache-tomcat-7.0.68.tar.gz /usr/local/
---> 17701beb1f0f
Removing intermediate container ca617b790d8c
Step 6/15 : RUN yum -y install vim
---> Running in e7742a8b237e
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.163.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.160-6.el7_6 will be installed
--> Processing Dependency: vim-common = 2:7.4.160-6.el7_6 for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
--> Processing Dependency: which for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.7-5.el7 will be installed
---> Package perl.x86_64 4:5.16.3-294.el7_6 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-294.el7_6.x86_64
---> Package perl-libs.x86_64 4:5.16.3-294.el7_6 will be installed
---> Package vim-common.x86_64 2:7.4.160-6.el7_6 will be installed
--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.160-6.el7_6.x86_64
---> Package which.x86_64 0:2.20-7.el7 will be installed
--> Running transaction check
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch
--> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch
---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
--> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
---> Package perl-Socket.x86_64 0:2.010-4.el7 will be installed
---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
---> Package perl-constant.noarch 0:1.27-2.el7 will be installed
---> Package perl-macros.x86_64 4:5.16.3-294.el7_6 will be installed
---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
---> Package vim-filesystem.x86_64 2:7.4.160-6.el7_6 will be installed
--> Running transaction check
---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
---> Package perl-Pod-Escapes.noarch 1:1.04-294.el7_6 will be installed
---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
--> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
--> Running transaction check
---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
--> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
--> Running transaction check
---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
---> Package perl-parent.noarch 1:0.225-244.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:7.4.160-6.el7_6 updates 1.0 M
Installing for dependencies:
gpm-libs x86_64 1.20.7-5.el7 base 32 k
groff-base x86_64 1.22.2-8.el7 base 942 k
perl x86_64 4:5.16.3-294.el7_6 updates 8.0 M
perl-Carp noarch 1.26-244.el7 base 19 k
perl-Encode x86_64 2.51-7.el7 base 1.5 M
perl-Exporter noarch 5.68-3.el7 base 28 k
perl-File-Path noarch 2.09-2.el7 base 26 k
perl-File-Temp noarch 0.23.01-3.el7 base 56 k
perl-Filter x86_64 1.49-3.el7 base 76 k
perl-Getopt-Long noarch 2.40-3.el7 base 56 k
perl-HTTP-Tiny noarch 0.033-3.el7 base 38 k
perl-PathTools x86_64 3.40-5.el7 base 82 k
perl-Pod-Escapes noarch 1:1.04-294.el7_6 updates 51 k
perl-Pod-Perldoc noarch 3.20-4.el7 base 87 k
perl-Pod-Simple noarch 1:3.28-4.el7 base 216 k
perl-Pod-Usage noarch 1.63-3.el7 base 27 k
perl-Scalar-List-Utils x86_64 1.27-248.el7 base 36 k
perl-Socket x86_64 2.010-4.el7 base 49 k
perl-Storable x86_64 2.45-3.el7 base 77 k
perl-Text-ParseWords noarch 3.29-4.el7 base 14 k
perl-Time-HiRes x86_64 4:1.9725-3.el7 base 45 k
perl-Time-Local noarch 1.2300-2.el7 base 24 k
perl-constant noarch 1.27-2.el7 base 19 k
perl-libs x86_64 4:5.16.3-294.el7_6 updates 688 k
perl-macros x86_64 4:5.16.3-294.el7_6 updates 44 k
perl-parent noarch 1:0.225-244.el7 base 12 k
perl-podlators noarch 2.5.1-3.el7 base 112 k
perl-threads x86_64 1.87-4.el7 base 49 k
perl-threads-shared x86_64 1.43-6.el7 base 39 k
vim-common x86_64 2:7.4.160-6.el7_6 updates 5.9 M
vim-filesystem x86_64 2:7.4.160-6.el7_6 updates 10 k
which x86_64 2.20-7.el7 base 41 k
Transaction Summary
================================================================================
Install 1 Package (+32 Dependent packages)
Total download size: 19 M
Installed size: 63 M
Downloading packages:
Public key for perl-Carp-1.26-244.el7.noarch.rpm is not installed
warning: /var/cache/yum/x86_64/7/base/packages/perl-Carp-1.26-244.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for perl-Pod-Escapes-1.04-294.el7_6.noarch.rpm is not installed
--------------------------------------------------------------------------------
Total 2.2 MB/s | 19 MB 00:08
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) "
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : gpm-libs-1.20.7-5.el7.x86_64 1/33
Installing : groff-base-1.22.2-8.el7.x86_64 2/33
Installing : 1:perl-parent-0.225-244.el7.noarch 3/33
Installing : perl-HTTP-Tiny-0.033-3.el7.noarch 4/33
Installing : perl-podlators-2.5.1-3.el7.noarch 5/33
Installing : perl-Pod-Perldoc-3.20-4.el7.noarch 6/33
Installing : 1:perl-Pod-Escapes-1.04-294.el7_6.noarch 7/33
Installing : perl-Encode-2.51-7.el7.x86_64 8/33
Installing : perl-Text-ParseWords-3.29-4.el7.noarch 9/33
Installing : perl-Pod-Usage-1.63-3.el7.noarch 10/33
Installing : 4:perl-libs-5.16.3-294.el7_6.x86_64 11/33
Installing : 4:perl-macros-5.16.3-294.el7_6.x86_64 12/33
Installing : perl-Storable-2.45-3.el7.x86_64 13/33
Installing : perl-Exporter-5.68-3.el7.noarch 14/33
Installing : perl-constant-1.27-2.el7.noarch 15/33
Installing : perl-Time-Local-1.2300-2.el7.noarch 16/33
Installing : perl-Socket-2.010-4.el7.x86_64 17/33
Installing : perl-Carp-1.26-244.el7.noarch 18/33
Installing : 4:perl-Time-HiRes-1.9725-3.el7.x86_64 19/33
Installing : perl-PathTools-3.40-5.el7.x86_64 20/33
Installing : perl-Scalar-List-Utils-1.27-248.el7.x86_64 21/33
Installing : 1:perl-Pod-Simple-3.28-4.el7.noarch 22/33
Installing : perl-File-Temp-0.23.01-3.el7.noarch 23/33
Installing : perl-File-Path-2.09-2.el7.noarch 24/33
Installing : perl-threads-shared-1.43-6.el7.x86_64 25/33
Installing : perl-threads-1.87-4.el7.x86_64 26/33
Installing : perl-Filter-1.49-3.el7.x86_64 27/33
Installing : perl-Getopt-Long-2.40-3.el7.noarch 28/33
Installing : 4:perl-5.16.3-294.el7_6.x86_64 29/33
Installing : which-2.20-7.el7.x86_64 30/33
install-info: No such file or directory for /usr/share/info/which.info.gz
Installing : 2:vim-filesystem-7.4.160-6.el7_6.x86_64 31/33
Installing : 2:vim-common-7.4.160-6.el7_6.x86_64 32/33
Installing : 2:vim-enhanced-7.4.160-6.el7_6.x86_64 33/33
Verifying : perl-HTTP-Tiny-0.033-3.el7.noarch 1/33
Verifying : perl-threads-shared-1.43-6.el7.x86_64 2/33
Verifying : perl-Storable-2.45-3.el7.x86_64 3/33
Verifying : 1:perl-Pod-Escapes-1.04-294.el7_6.noarch 4/33
Verifying : perl-Exporter-5.68-3.el7.noarch 5/33
Verifying : perl-constant-1.27-2.el7.noarch 6/33
Verifying : perl-PathTools-3.40-5.el7.x86_64 7/33
Verifying : 2:vim-filesystem-7.4.160-6.el7_6.x86_64 8/33
Verifying : 1:perl-parent-0.225-244.el7.noarch 9/33
Verifying : which-2.20-7.el7.x86_64 10/33
Verifying : 4:perl-libs-5.16.3-294.el7_6.x86_64 11/33
Verifying : groff-base-1.22.2-8.el7.x86_64 12/33
Verifying : perl-File-Temp-0.23.01-3.el7.noarch 13/33
Verifying : 1:perl-Pod-Simple-3.28-4.el7.noarch 14/33
Verifying : perl-Time-Local-1.2300-2.el7.noarch 15/33
Verifying : gpm-libs-1.20.7-5.el7.x86_64 16/33
Verifying : 4:perl-macros-5.16.3-294.el7_6.x86_64 17/33
Verifying : perl-Socket-2.010-4.el7.x86_64 18/33
Verifying : perl-Carp-1.26-244.el7.noarch 19/33
Verifying : 4:perl-Time-HiRes-1.9725-3.el7.x86_64 20/33
Verifying : perl-Scalar-List-Utils-1.27-248.el7.x86_64 21/33
Verifying : 2:vim-common-7.4.160-6.el7_6.x86_64 22/33
Verifying : perl-Pod-Usage-1.63-3.el7.noarch 23/33
Verifying : perl-Encode-2.51-7.el7.x86_64 24/33
Verifying : 2:vim-enhanced-7.4.160-6.el7_6.x86_64 25/33
Verifying : perl-Pod-Perldoc-3.20-4.el7.noarch 26/33
Verifying : perl-podlators-2.5.1-3.el7.noarch 27/33
Verifying : perl-File-Path-2.09-2.el7.noarch 28/33
Verifying : perl-threads-1.87-4.el7.x86_64 29/33
Verifying : perl-Filter-1.49-3.el7.x86_64 30/33
Verifying : perl-Getopt-Long-2.40-3.el7.noarch 31/33
Verifying : perl-Text-ParseWords-3.29-4.el7.noarch 32/33
Verifying : 4:perl-5.16.3-294.el7_6.x86_64 33/33
Installed:
vim-enhanced.x86_64 2:7.4.160-6.el7_6
Dependency Installed:
gpm-libs.x86_64 0:1.20.7-5.el7
groff-base.x86_64 0:1.22.2-8.el7
perl.x86_64 4:5.16.3-294.el7_6
perl-Carp.noarch 0:1.26-244.el7
perl-Encode.x86_64 0:2.51-7.el7
perl-Exporter.noarch 0:5.68-3.el7
perl-File-Path.noarch 0:2.09-2.el7
perl-File-Temp.noarch 0:0.23.01-3.el7
perl-Filter.x86_64 0:1.49-3.el7
perl-Getopt-Long.noarch 0:2.40-3.el7
perl-HTTP-Tiny.noarch 0:0.033-3.el7
perl-PathTools.x86_64 0:3.40-5.el7
perl-Pod-Escapes.noarch 1:1.04-294.el7_6
perl-Pod-Perldoc.noarch 0:3.20-4.el7
perl-Pod-Simple.noarch 1:3.28-4.el7
perl-Pod-Usage.noarch 0:1.63-3.el7
perl-Scalar-List-Utils.x86_64 0:1.27-248.el7
perl-Socket.x86_64 0:2.010-4.el7
perl-Storable.x86_64 0:2.45-3.el7
perl-Text-ParseWords.noarch 0:3.29-4.el7
perl-Time-HiRes.x86_64 4:1.9725-3.el7
perl-Time-Local.noarch 0:1.2300-2.el7
perl-constant.noarch 0:1.27-2.el7
perl-libs.x86_64 4:5.16.3-294.el7_6
perl-macros.x86_64 4:5.16.3-294.el7_6
perl-parent.noarch 1:0.225-244.el7
perl-podlators.noarch 0:2.5.1-3.el7
perl-threads.x86_64 0:1.87-4.el7
perl-threads-shared.x86_64 0:1.43-6.el7
vim-common.x86_64 2:7.4.160-6.el7_6
vim-filesystem.x86_64 2:7.4.160-6.el7_6
which.x86_64 0:2.20-7.el7
Complete!
---> 68842422f5bb
Removing intermediate container e7742a8b237e
Step 7/15 : ENV MYPATH /usr/local
---> Running in 378f507c7454
---> 68772160bbba
Removing intermediate container 378f507c7454
Step 8/15 : WORKDIR $MYPATH
---> 8f720fb47d8d
Removing intermediate container 1fef2cdcd39f
Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.7.0_45
---> Running in 031f617faaf8
---> 8858a95269f7
Removing intermediate container 031f617faaf8
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 81fea7bdedde
---> 677ff52ce8bf
Removing intermediate container 81fea7bdedde
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.68
---> Running in d8868d0fbf8a
---> 1337cdd4ae56
Removing intermediate container d8868d0fbf8a
Step 12/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.68
---> Running in 3b1b0926e692
---> d63c27b9e986
Removing intermediate container 3b1b0926e692
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in c1540c867550
---> 363e3b5be57d
Removing intermediate container c1540c867550
Step 14/15 : EXPOSE 8080
---> Running in 0557437d1396
---> bddf5d2259c1
Removing intermediate container 0557437d1396
Step 15/15 : CMD /usr/local/apache-tomcat-7.0.68/bin/startup.sh && tail -F /usr/local/apache-tomcat-7.0.68/bin/logs/catalina.out
---> Running in 54733e8ad7a8
---> caefcd845de4
Removing intermediate container 54733e8ad7a8
Successfully built caefcd845de4
⑥ 构建完成
[root@localhost tomcat9]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mongo latest 9c02a5a12c52 Less than a second ago 413 MB
docker.io/tomcat latest 238e6d7313e3 Less than a second ago 506 MB
brucrliu_tomcat latest caefcd845de4 2 minutes ago 669 MB
myip1_centos latest 274c58d0c1e4 19 minutes ago 314 MB
myip_centos latest 41868e4e4fee 36 minutes ago 314 MB
mycentos 1.3 720d4eb5c5f0 About an hour ago 480 MB
bruceliu/centos9527 latest 071e944a589b 9 hours ago 202 MB
bruceliu/tomcat 7-jre7 a66f9aba334d 14 hours ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 5 weeks ago 359 MB
docker.io/mysql latest 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
docker.io/centos latest 9f38484d220f 3 months ago 202 MB
⑦ run
[root@localhost tomcat9]# docker run -d -p 9080:8080 --name mycat7 -v /brucrliu/mydockerfile/tomcat7/test:/usr/local/apache-tomcat-7.0.68/webapps/test -v /bruceliu/mydockerfile/tomcat7/tomcat9logs/:/usr/local/apache-tomcat-7.0.68/logs --privileged=true brucrliu_tomcat
8da490d0e6c81b0e78c530bd90717e2e7f187266329c9e720819b57e7b2c2466
备注:
Docker挂载主机目录Docker访问出现cannot open directory .: Permission denied
解决办法:在挂载目录后多加一个–privileged=true参数即可
⑧ 验证
[root@localhost tomcat9]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8da490d0e6c8 brucrliu_tomcat "/bin/sh -c '/usr/..." About a minute ago Up About a minute 0.0.0.0:9080->8080/tcp mycat7
⑨ 访问
⑩ 结合前述的容器卷将测试的web服务test发布
[root@localhost test]# pwd
/brucrliu/mydockerfile/tomcat7/test
[root@localhost test]# ll
总用量 4
-rw-r--r--. 1 root root 23 6月 18 15:04 1.html
[root@localhost test]# vi 1.html
<h1>Hello,Test!!!</h1>
Docker是一个开源的应用容器引擎;是一个轻量级容器技术;
Docker支持将软件编译成一个镜像;然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使用这个镜像;
docker hub上面查找tomcat镜像
docker search tomcat
[root@localhost ~]# docker search tomcat
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 2482 [OK]
docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 66 [OK]
docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 53 [OK]
docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 29 [OK]
docker.io docker.io/kubeguide/tomcat-app Tomcat image for Chapter 1 26
docker.io docker.io/consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
docker.io docker.io/cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
docker.io docker.io/aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 11 [OK]
docker.io docker.io/tutum/tomcat Base docker image to run a Tomcat applicat... 11
docker.io docker.io/arm32v7/tomcat Apache Tomcat is an open source implementa... 6
docker.io docker.io/rightctrl/tomcat CentOS , Oracle Java, tomcat application s... 5 [OK]
docker.io docker.io/maluuba/tomcat7-java8 Tomcat7 with java8. 3
docker.io docker.io/amd64/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/arm64v8/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/99taxis/tomcat7 Tomcat7 1 [OK]
docker.io docker.io/camptocamp/tomcat-logback Docker image for tomcat with logback integ... 1 [OK]
docker.io docker.io/i386/tomcat Apache Tomcat is an open source implementa... 1
docker.io docker.io/ppc64le/tomcat Apache Tomcat is an open source implementa... 1
docker.io docker.io/appsvc/tomcat 0
docker.io docker.io/cfje/tomcat-resource Tomcat Concourse Resource 0
docker.io docker.io/jelastic/tomcat An image of the Tomcat Java application se... 0
docker.io docker.io/oobsri/tomcat8 Testing CI Jobs with different names. 0
docker.io docker.io/picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAG... 0 [OK]
docker.io docker.io/s390x/tomcat Apache Tomcat is an open source implementa... 0
docker.io docker.io/secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK]
从docker hub上拉取tomcat镜像到本地
[root@localhost ~]# docker rmi docker.io/tomcat
Error response from daemon: No such image: docker.io/tomcat:latest
[root@localhost ~]# docker pull tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ...
latest: Pulling from docker.io/library/tomcat
a4d8138d0f6b: Pull complete
dbdc36973392: Pull complete
f59d6d019dd5: Pull complete
aaef3e026258: Pull complete
5e86b04a4500: Pull complete
1a6643a2873a: Pull complete
2ad1e30fc17c: Pull complete
16f4e6ee0ca6: Pull complete
928f4d662d23: Pull complete
b8d24294d525: Pull complete
Digest: sha256:2785fac92d1bcd69d98f2461c6799390555a41fd50d3f847b544368d594c637b
Status: Downloaded newer image for docker.io/tomcat:latest
docker images查看是否有拉取到的tomcat
[root@localhost ~]# docker images tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 238e6d7313e3 Less than a second ago 506 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 5 weeks ago 359 MB
使用tomcat镜像创建容器(也叫运行镜像)
docker run -it -p 8080:8080 tomcat
-p 主机端口:docker容器端口
-P 随机分配端口
i:交互
t:终端
[root@localhost ~]# docker run -it -p 8080:8080 docker.io/tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/openjdk-8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
18-Jun-2019 09:18:47.186 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.43
18-Jun-2019 09:18:47.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jul 4 2019 20:53:15 UTC
18-Jun-2019 09:18:47.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.43.0
18-Jun-2019 09:18:47.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
18-Jun-2019 09:18:47.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-957.el7.x86_64
18-Jun-2019 09:18:47.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
18-Jun-2019 09:18:47.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_222-b10
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
18-Jun-2019 09:18:47.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
18-Jun-2019 09:18:47.203 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
18-Jun-2019 09:18:47.203 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
docker hub上面查找mysql镜像
docker search mysql
[root@localhost ~]# docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 8458 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 2914 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 626 [OK]
docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 60 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 59
docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 49
docker.io docker.io/deitch/mysql-backup Automated and scheduled mysql database dum... 40 [OK]
docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 32
docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 29 [OK]
docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 28 [OK]
docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 21
docker.io docker.io/prom/mysqld-exporter 20 [OK]
docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 15
docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 13
docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 12
docker.io docker.io/arey/mysql-client Run a MySQL client from a docker container 10 [OK]
docker.io docker.io/imega/mysql-client Size: 36 MB, alpine:3.5, Mysql client: 10.... 6 [OK]
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/yloeffler/mysql-backup This image runs mysqldump to backup data u... 6 [OK]
docker.io docker.io/fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron t... 4 [OK]
docker.io docker.io/genschsa/mysql-employees MySQL Employee Sample Database 2 [OK]
docker.io docker.io/jelastic/mysql An image of the MySQL database server main... 1
docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 0 [OK]
docker.io docker.io/monasca/mysql-init A minimal decoupled init container for mysql 0
docker.io docker.io/widdpim/mysql-client Dockerized MySQL Client (5.7) including Cu... 0 [OK]
[root@localhost ~]# docker pull docker.io/mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
0a4690c5d889: Pull complete
98aa2fc6cbeb: Pull complete
0777e6eb0e6f: Pull complete
2464189c041c: Pull complete
b45df9dc827d: Pull complete
b42b00086160: Pull complete
bb93567627c7: Pull complete
bda91ab2ec35: Pull complete
50b4c0c50b33: Pull complete
70c2999bc788: Pull complete
3cf3e568e51b: Pull complete
c80d654aa77e: Pull complete
Digest: sha256:b1b2c176a45f4ff6875d0d7461fe1fdd8606deb015b38b69545f72de97714efd
Status: Downloaded newer image for docker.io/mysql:latest
[root@localhost ~]# docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 2151acc12881 Less than a second ago 445 MB
docker.io/mysql <none> 7bb2586065cd 2 months ago 477 MB
使用mysql镜像
docker exec -it MySQL运行成功后的容器ID /bin/bash
docker run -di --name mysql_0803 -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
-p 代表端口映射,格式为 宿主机映射端口:容器运行端口
-e 代表添加环境变量 MYSQL_ROOT_PASSWORD是root用户的登陆密码
进入mysql容器
docker exec -it mysql_0803 /bin/bash
登陆mysql
mysql -u root -p
外部Win10也来连接运行在dokcer上的mysql服务
我们在我们本机的电脑上去连接虚拟机Centos中的Docker容器,这里192.168.247.130是虚拟机操作系统的IP
关闭防火墙
firewall-cmd --state
service firewalld stop
firewall-cmd --state
原因:mysql 8.0 默认使用 caching_sha2_password 身份验证机制;客户端不支持新的加密方式。
解决方案:
修改用户(root)的加密方式
步骤1:进入mysql容器内部
[root@localhost ~]# docker exec -it mysql01 bash ## mysql01是容器的别名,这里也可以用容器的id代替
步骤2:登录mysql
root@e285125c99d6:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.13 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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.
步骤3:设置用户配置项
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | $A$005$HF7;krfwhkKHp5fPenQm4J2dm/RJtbbyjtCUVdDCcboXQw3ALxsif/sS1 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
备注:host为 % 表示不限制ip localhost表示本机使用 plugin非mysql_native_password 则需要修改密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; ### 123456 mysql的登录密码
flush privileges;
然后再查看用户信息
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
[root@localhost ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ...
latest: Pulling from docker.io/library/redis
f5d23c7fed46: Pull complete
831c20fd50cb: Pull complete
bc2a0f25caa5: Pull complete
745ac314a007: Pull complete
6deeca231441: Pull complete
6291e84f5373: Pull complete
Digest: sha256:854715f5cd1b64d2f62ec219a7b7baceae149453e4d29a8f72cecbb5ac51c4ad
Status: Downloaded newer image for docker.io/redis:latest
[root@localhost ~]# docker run -di --name=my_redis -p 6388:6379 redis
f0980b9a56e90a5c3564591f9dfb1c7ecf1f9223d85c011d3aea41aeb14c9d27
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f0980b9a56e9 redis "docker-entrypoint..." 8 seconds ago Up 7 seconds 0.0.0.0:6388->6379/tcp my_redis
在你的本地电脑命令提示符下,用window版本redis测试
[root@localhost ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
f5d23c7fed46: Already exists
918b255d86e5: Pull complete
8c0120a6f561: Pull complete
Digest: sha256:eb3320e2f9ca409b7c0aa71aea3cf7ce7d018f03a372564dbdb023646958770b
Status: Downloaded newer image for docker.io/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/redis latest 857c4ab5f029 Less than a second ago 98.2 MB
docker.io/nginx latest e445ab08b2be Less than a second ago 126 MB
docker.io/mongo latest 9c02a5a12c52 Less than a second ago 413 MB
docker.io/mysql 5.6 7b01f1418bd7 Less than a second ago 256 MB
docker.io/mysql latest 2151acc12881 Less than a second ago 445 MB
docker.io/tomcat latest 238e6d7313e3 Less than a second ago 506 MB
brucrliu_tomcat latest caefcd845de4 3 hours ago 669 MB
myip1_centos latest 274c58d0c1e4 3 hours ago 314 MB
myip_centos latest 41868e4e4fee 3 hours ago 314 MB
mycentos 1.3 720d4eb5c5f0 5 hours ago 480 MB
bruceliu/centos9527 latest 071e944a589b 12 hours ago 202 MB
bruceliu/tomcat 7-jre7 a66f9aba334d 17 hours ago 359 MB
docker.io/tomcat 7-jre7 47c156f4d4e3 5 weeks ago 359 MB
docker.io/mysql <none> 7bb2586065cd 2 months ago 477 MB
docker.io/centos 7 9f38484d220f 3 months ago 202 MB
docker.io/centos latest 9f38484d220f 3 months ago 202 MB
[root@localhost ~]# docker run -di --name=my_nginx_test -p 80:80 nginx /bin/bash
82edb98d02d40e3053e307bab26edd345aa04c5f9dc9e10d14de5ccb41831a70
[root@localhost ~]# docker run -p 8880:80 -d docker.io/nginx
b6a5fce26fb4eb4829aee8e27a673940e83ed6978f99076377e24cb728910643
[root@localhost ~]# netstat -anp | grep 8880
tcp6 0 0 :::8880 :::* LISTEN 30443/docker-proxy-
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6a5fce26fb4 docker.io/nginx "nginx -g 'daemon ..." 26 seconds ago Up 25 seconds 0.0.0.0:8880->80/tcp clever_goldwasser
82edb98d02d4 nginx "/bin/bash" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp my_nginx_test
官方的nginx镜像,nginx配置文件nginx.conf 在/etc/nginx/目录下。
在容器内编辑配置文件不方便,我们可以先将配置文件从容器内拷贝到宿主机,编辑修改后再拷贝回去。
[root@localhost ~]# docker cp clever_goldwasser:/etc/nginx/nginx.conf nginx.conf
[root@localhost ~]# ll
总用量 8
-rw-r--r--. 1 root root 0 6月 17 19:42 111.txt
-rw-r--r--. 1 root root 0 6月 17 19:42 1.txt
-rw-------. 1 root root 1257 2月 17 21:33 anaconda-ks.cfg
drwxr-xr-x. 2 root root 80 6月 18 14:35 myfile
-rw-r--r--. 1 root root 643 7月 23 2019 nginx.conf
upstream tomcat-cas {
server 172.17.0.7:8080;
}
server {
listen 80;
server_name passport.easyshop.com;
location / {
proxy_pass http://tomcat-cas;
index index.html index.htm;
}
}
docker cp nginx.conf clever_goldwasser:/etc/nginx/nginx.conf
docker restart clever_goldwasser
1 首先你得准备一个hub 的帐号, 去 https://hub.docker.com 注册吧!
2 在hub那里新建一个仓库, 这个就类似于github那边的…create ---> create repository
(创建知识库) 然后随便搞个名字, 我这里就叫ubuntu吧 所以路径就是wherein/ubuntu
了
3 运行 docker ps
查看容器, 这里就是要获取到CONTAINER ID
4 commit一个容器
命令: docker commit CONTAINER ID wherein/ubuntu
5 接下来就是登录一个hub帐号了 命令: docker login 如果成功则显示下图
6 将刚才的镜像push 到你的hub 那里去 命令: docker push wherein/ubuntu:latest
这个latest其实就是一个tag name 会在hub的Tags那里显示
注: 这里注意一下, 有时push会超时的, 没关系, 再push一次就好了
7 现在验证一下
命令: docker inspect wherein/ubuntu
yum install docker
docker -v
启动docker:service docker start
停止docker:service docker stop
重启docker:service docker restart
查看docker状态:service docker status
开机启动:chkconfig docker on
开机启动:systemctl enable docker.service
查看docker概要信息:docker info
查看docker帮助文档:docker --help
国外的:https://hub.docker.com
国内的:https://hub.daocloud.io/
docker search [OPTIONS] 镜像名字
OPTIONS说明
--no-trunc : 显示完整的镜像描述
-s : 列出收藏数不小于指定值的镜像。
--automated : 只列出 automated build类型的镜像;
示例:查询所有tomcat镜像,条件是受欢迎度大于等于20并自动构建的镜像
docker search tomcat -s 20 --automated
参数说明
NAME:仓库名称
DESCRIPTION:镜像描述
STARS:用户评价,反应一个镜像的受欢迎程度
OFFICIAL:是否官方
AUTOMATED:自动构建,表示该镜像由Docker Hub自动构建流程创建的
docker pull 镜像名字[:TAG] [:TAG] 镜像的版本号
示例:
docker pull daocloud.io/library/tomcat:8.0.46
拉取镜像的时候不指定版本,默认是最新版镜像
[root@VM-0-8-centos ~]# docker pull daocloud.io/library/tomcat:8.0.46
Trying to pull repository daocloud.io/library/tomcat ...
8.0.46: Pulling from daocloud.io/library/tomcat
d93a2d7cc901: Pull complete
15a33158a136: Pull complete
606c0b57feb0: Pull complete
584c35e01c21: Pull complete
48b2c1657fd0: Pull complete
1b4dd65ad29c: Pull complete
4b104da1b89f: Pull complete
e8b2bd774636: Pull complete
d521e6b892a5: Pull complete
a553e2265165: Pull complete
3acddd1b22fc: Pull complete
879d57723a8c: Pull complete
Digest: sha256:8097c710b1a462471c02ac063cc801f7f3d6e63399b7a556216d5b0cbfaa954b
Status: Downloaded newer image for daocloud.io/library/tomcat:8.0.46
编辑该文件:vim /etc/docker/daemon.json
// 如果该文件不存在就手动创建;说明:在centos7.x下,通过vi。
在该文件中输入如下内容:
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
service docker restart
docker images
参数说明:
REPOSITORY:镜像所在的仓库名称
TAG:镜像标签
IMAGE ID:镜像ID
CREATED:镜像的创建日期(不是获取该镜像的日期)
SIZE:镜像大小
删除单个 remove image
------------------------------------
docker rmi -f 镜像ID
删除多个
docker rmi -f 镜像ID 镜像ID
删除全部
docker rmi -f $(docker images -qa)
镜像被运行起来,被run起来之后形成一个容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
创建容器常用的参数说明:
-i:表示运行容器
-t:表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪终端。
--
name :为创建的容器命名。
-v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
-d:在run后面加上-d参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器,如果只加-i -t两个参数,创建后就会自动进去容器)。
-p:表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射
docker run -it --name=mycentos 镜像ID /bin/bash
/bin/bash : centsos操作系统的命令终端界面
exit 退出交互式容器,特点:交互式容器一旦退出,那么容器自动停止
docker ps [OPTIONS]
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
--no-trunc :不截断输出,显示完成信息
示例:查询所有的容器
docker ps -a
docker start 容器ID
docker exec -it 容器ID /bin/bash
docker stop 容器ID
docker restart 容器ID
docker run -id --name=mycentos 容器ID
守护式容器退出的时候不会容器退出
docker kill 容器ID或者容器名
------------------------------------------------------------
docker pause 暂停容器
docker unpause 恢复暂停容器
单个删除
docker rm 容器ID
多个删除
docker rm 容器ID 容器ID
运行中的容器不能直接删除
-f 强制删除运行中的容器
docker rm -f 容器ID
删除所有容器
docker rm -f `docker ps -a -q`
宿主机------->容器
docker cp 宿主机文件路径 容器名称:容器文件路径
示例:
docker cp 平台.jpg f6458218a246:/opt/pingtai.jpg
容器-------->宿主机
docker cp 容器名称:容器文件路径 宿主机文件路径
示例:
docker cp f6458218a246:/opt/123123123.txt /root
目录挂载:在创建容器的时候可以使用 -v 参数实现目录挂载。容器中的某个目录和宿主机的某个目录进行了映射,这两个目录文件自动同步
docker run -id --name="mycentos-002" -v /root/hehehe:/root/xixixi 7e6257c9f8d8
需要挂载的目录如果不存在,那么可以自动创建
docker inspect 容器ID -- 查看容器详情信息
[root@VM-0-8-centos hehehe]# docker inspect f6458218a246
[
{
"Id": "f6458218a246794156b9db2306b24c7f060fb755cbcf9e2fdff323e6994d0659",
"Created": "2021-03-03T08:03:39.641856558Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 10307,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-03T08:03:39.830497789Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:7e6257c9f8d8d4cdff5e155f196d67150b871bbe8c02761026f803a704acb3e9",
"ResolvConfPath": "/var/lib/docker/containers/f6458218a246794156b9db2306b24c7f060fb755cbcf9e2fdff323e6994d0659/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f6458218a246794156b9db2306b24c7f060fb755cbcf9e2fdff323e6994d0659/hostname",
"HostsPath": "/var/lib/docker/containers/f6458218a246794156b9db2306b24c7f060fb755cbcf9e2fdff323e6994d0659/hosts",
"LogPath": "",
"Name": "/mycentos7-1",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "docker-runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/22e7faab2b144982f5e5a9032a7ee9f3a8feb404ce140ea55af38f9efc9cdafa-init/diff:/var/lib/docker/overlay2/a0954a0a2dd5c3b2ee67389e261278866a2a26cd709e7c11b9f91c4bcc3917c8/diff",
"MergedDir": "/var/lib/docker/overlay2/22e7faab2b144982f5e5a9032a7ee9f3a8feb404ce140ea55af38f9efc9cdafa/merged",
"UpperDir": "/var/lib/docker/overlay2/22e7faab2b144982f5e5a9032a7ee9f3a8feb404ce140ea55af38f9efc9cdafa/diff",
"WorkDir": "/var/lib/docker/overlay2/22e7faab2b144982f5e5a9032a7ee9f3a8feb404ce140ea55af38f9efc9cdafa/work"
}
},
"Mounts": [],
"Config": {
"Hostname": "f6458218a246",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": true,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "7e6257c9f8d8",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"org.opencontainers.image.created": "2020-08-09 00:00:00+01:00",
"org.opencontainers.image.licenses": "GPL-2.0-only",
"org.opencontainers.image.title": "CentOS Base Image",
"org.opencontainers.image.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "881ad4e081688ffba7542c0c98651208ba2804aaa5c6c09e24071d16174dd13a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/881ad4e08168",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5629ea351b3de88734abd3b9c1f0f7316e22348a0f07cdc7054c661fefc12ac8",
"Gateway": "172.18.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:12:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "a2ae13c4596e736ffed373efd03d460f149a8872aef2931bcf4613e69e438661",
"EndpointID": "5629ea351b3de88734abd3b9c1f0f7316e22348a0f07cdc7054c661fefc12ac8",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02"
}
}
}
}
]
也可以直接执行下面的命令直接输出IP地址
docker inspect --format='{{.NetworkSettings.IPAddress}}' f6458218a246
拉取tomcat
docker pull daocloud.io/library/tomcat:8.5.16-jre8-alpine
查看所有镜像
docker images -a
启动tomcat
docker run -id --name=tomcat001 -p 8686:8080 fa6ad595ba45
登录到tomcat服务器
docker exec -it 53a2756c3054 /bin/bash
把项目拷贝到容器中
docker cp JingDong/ 53a2756c3054:/usr/local/tomcat/webapps/
# 表示镜像的父镜像(base镜像) 继承与CentOS:7 指明构建的新镜像是来自于哪个基础镜像
FROM centos:7
# 镜像的作者信息 版权
# MAINTAINER bruceliu
# [逐渐废弃] LABLE --替代MAINTANIER 具体使用: LABLE maintainer="作者信息"
LABEL maintainer="[email protected]"
# 设置环境变量
ENV MYPATH /usr/local
# 设置当前的工作目录
WORKDIR $MYPATH
# 执行指令: yum -y install vim
RUN yum -y install vim
# 执行指令: yum -y install net-tools
RUN yum -y install net-tools
# 对外暴露访问端口,声明容器运行的服务端口,为容器打开指定要监听的端口以实现与外部通信,例如:
EXPOSE 80
# 启动容器时执行的Shell命令,例如:如果有多个CMD,容器启动的时候执行最后一个
CMD echo $MYPATH
CMD echo "--------------success--------------ok"
CMD /bin/bash
# 父镜像是谁
FROM centos:7
# 作者信息
MAINTAINER bruce
#把JDK的安装文件从宿主机拷贝到容器中/usr/local/目录下,并且自动解压
# ADD拷贝命令,会自动解压
# COPY拷贝命令,不会自动解压
ADD jdk-7u45-linux-x64.tar.gz /usr/local/
#安装vim编辑器 vim指令
RUN yum -y install vim
#设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
# 切换工作目录
WORKDIR $MYPATH
#配置java环境变量
ENV JAVA_HOME /usr/local/jdk1.7.0_45
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
CMD /bin/bash
FROM centos:8
MAINTAINER bruce
#把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下[为了演示COPY]
#COPY c.txt /usr/local/cincontainer.txt
#把java与tomcat添加到容器中 ADD会自动解压
ADD jdk-7u45-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-7.0.68.tar.gz /usr/local/
#安装vim编辑器
#RUN yum -y install vim
#设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
# 切换工作目录/usr/local
WORKDIR $MYPATH
#配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.7.0_45
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.68
ENV CATALINA_BASE /usr/local/apache-tomcat-7.0.68
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动tomcat容器的时候,自动执行命令,当tomcat启动之后,顺便打印日志
CMD /usr/local/apache-tomcat-7.0.68/bin/startup.sh && tail -F /usr/local/apache-tomcat-7.0.68/logs/catalina.out
FROM centos:7
MAINTAINER bruce
#把java与tomcat添加到容器中
ADD jdk-8u144-linux-x64.gz /usr/local/
ADD apache-tomcat-8.5.63.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_144
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.63
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.63
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#将项目war包拷贝到webapps,但是此时不会自动解压!ADD只能解压tar.gz,还有tomcat此时还没启动!
ADD BookManagerSystem.war /usr/local/apache-tomcat-8.5.63/webapps/
#设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local/apache-tomcat-8.5.63/webapps/
WORKDIR $MYPATH
# 执行命名(把原来的自带的ROOT更改掉)
RUN mv ROOT/ ROOT123/
# 给服务器安装unzip命令,作用就是解压缩zip包 或者 war包
RUN yum install -y unzip
# 解压缩war包
RUN unzip -oq BookManagerSystem.war -d ROOT/
#启动时运行tomcat
CMD /usr/local/apache-tomcat-8.5.63/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.63/logs/catalina.out