文章目录
1. Docker概述
(1)基本介绍
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。官网:https://docs.docker.com/
(2)应用场景
-
Web 应用的自动化打包和发布。
-
自动化测试和持续集成、发布。
-
在服务型环境中部署和调整数据库或其他的后台应用。
-
从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。
(3)Docker 的优势
Docker 是一个用于开发,交付和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分开,从而可以快速交付软件。借助 Docker,您可以与管理应用程序相同的方式来管理基础架构。通过利用 Docker 的方法来快速交付,测试和部署代码,您可以大大减少编写代码和在生产环境中运行代码之间的延迟。
1、快速,一致地交付您的应用程序。Docker 允许开发人员使用您提供的应用程序或服务的本地容器在标准化环境中工作,从而简化了开发的生命周期。
容器非常适合持续集成和持续交付(CI / CD)工作流程,请考虑以下示例方案:
您的开发人员在本地编写代码,并使用 Docker 容器与同事共享他们的工作。
他们使用 Docker 将其应用程序推送到测试环境中,并执行自动或手动测试。
当开发人员发现错误时,他们可以在开发环境中对其进行修复,然后将其重新部署到测试环境中,以进行测试和验证。
测试完成后,将修补程序推送给生产环境,就像将更新的镜像推送到生产环境一样简单。
2、响应式部署和扩展
Docker 是基于容器的平台,允许高度可移植的工作负载。Docker 容器可以在开发人员的本机上,数据中心的物理或虚拟机上,云服务上或混合环境中运行。
Docker 的可移植性和轻量级的特性,还可以使您轻松地完成动态管理的工作负担,并根据业务需求指示,实时扩展或拆除应用程序和服务。
3、在同一硬件上运行更多工作负载
Docker 轻巧快速。它为基于虚拟机管理程序的虚拟机提供了可行、经济、高效的替代方案,因此您可以利用更多的计算能力来实现业务目标。Docker 非常适合于高密度环境以及中小型部署,而您可以用更少的资源做更多的事情。
2. 虚拟化技术和容器化技术
虚拟化技术特点:1.资源占用多 2.冗余步骤多 3.启动很慢
容器化技术:容器化技术不是模拟的一个完整的操作系统
比较Docker和虚拟机的不同:
1.传统虚拟机,虚拟出硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件。
2.Docker容器内的应用直接运行在宿主机的内容,容器是没有自己的内核的,也没有虚拟硬件。
3.每个容器都是相互隔离的,每个容器都有属于自己的文件系统,互不影响。
容器化带来的好处:
3. Docker的基本组成
Docker的基本组成图如下:
说明:
4. Docker的安装
查看系统的内核:
uname -r
系统内核版本为3.10.0
[root@iZwz99sm8v95sckz8bd2c4Z ~]# uname -r
3.10.0-957.21.3.el7.x86_64
查看系统配置
cat /etc/os-release
[root@iZwz99sm8v95sckz8bd2c4Z ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT=“CentOS-7”
CENTOS_MANTISBT_PROJECT_VERSION=“7”
REDHAT_SUPPORT_PRODUCT=“centos”
REDHAT_SUPPORT_PRODUCT_VERSION=“7”
Docker的安装步骤:
(1)卸载旧的版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
(2)下载需要的安装包
yum install -y yum-utils
- 1
(3)设置镜像的仓库
yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo #国外的地址
<span class="token comment"># 设置阿里云的Docker镜像仓库</span>
yum-config-manager </span>
–add-repo </span>
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #国外的地址
(4)更新yum软件包索引
yum makecache fast
(5)安装docker相关的配置
docker-ce 是社区版,docker-ee 企业版
yum install docker-ce docker-ce-cli containerd.io
出现了completed即安装成功。
(6)启动Docker
systemctl start docker
# 查看当前版本号,是否启动成功
docker version
# 设置开机自启动
systemctl enable docker
结果:
下载hello-world镜像进行测试
查看下载的hello world镜像
[root@iZwz99sm8v95sckz8bd2c4Z lib]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 11 months ago 13.3kB
5. Docker的卸载
# 1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2. 删除资源 . /var/lib/docker是docker的默认工作路径
rm -rf /var/lib/docker
6. 配置阿里云镜像加速
(1)进入阿里云官网,搜索容器镜像服务
(2)依次执行官方的这四条命令
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://axvfsf7e.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
8. Docker容器运行流程
启动一个容器,Docker的运行流程如下图:
9. 底层原理
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!Docker Server接收到Docker-Client的指令,就会执行这个指令!
Docker为什么比VM Ware快?
1、Docker比虚拟机更少的抽象层
2、docker利用宿主机的内核,VM需要的是Guest OS
Docker新建一个容器的时候,不需要像虚拟机一样重新加载一个操作系统内核,直接利用宿主机的操作系统,而虚拟机是需要加载Guest OS。Docker和VM的对比如下:
10. Docker常用命令
10.1 基础命令
docker version #查看docker的版本信息
docker info #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令(可查看可选的参数)
docker COMMAND --help
命令的帮助文档地址:https://docs.docker.com/engine/reference/commandline/docker/
10.2 镜像命令
1.docker images 查看本地主机的所有镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 11 months ago 13.3kB
#解释:
1.REPOSITORY 镜像的仓库源
2.TAG 镜像的标签
3.IMAGE ID 镜像的id
4.CREATED 镜像的创建时间
5.SIZE 镜像的大小
# 可选参数
-a/–all 列出所有镜像
-q/–quiet 只显示镜像的id
2.docker search 搜索镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10308 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3819 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 754 [OK]
percona Percona Server is a fork of the MySQL relati… 517 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79
centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK]
#可选参数
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
–format string Pretty-print search using a Go template
–limit int Max number of search results (default 25)
–no-trunc Don’t truncate output
#搜索收藏数大于3000的镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10308 [OK]
mariadb MariaDB is a community-developed fordockerk of MyS… 3819 [OK]
3.docker pull 镜像名[:tag] 下载镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql
Using default tag: latest #如果不写tag默认就是latest
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete #分层下载,docker image的核心-联合文件系统
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
ffe10de703ea: Pull complete
657af6d90c83: Pull complete
98bfb480322c: Pull complete
6aa3859c4789: Pull complete
1ed875d851ef: Pull complete
Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #下载来源的真实地址 #docker pull mysql等价于docker pull docker.io/library/mysql:latest
指定版本下载
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Already exists
fedd960d3481: Already exists
7ab947313861: Already exists
64f92f19e638: Already exists
3e80b17bff96: Already exists
014e976799f9: Already exists
59ae84fee1b3: Already exists
7d1da2a18e2e: Pull complete
301a28b700b9: Pull complete
529dc8dbeaf3: Pull complete
bc9d021dc13f: Pull complete
Digest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
4.docker rmi 删除镜像
#1.删除指定的镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id
#2.删除多个镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id 镜像id 镜像id
#3.删除全部的镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f $(docker images -aq)
10.3 容器命令
如拉取一个centos镜像
docker pull centos
运行容器的命令说明:
docker run [可选参数] image
#参数说明
–name=“名字” 指定容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
(
-p ip:主机端口:容器端口 配置主机端口映射到容器端口
-p 主机端口:容器端口
-p 容器端口
)
-P 随机指定端口(大写的P)
运行并进入容器centos
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it centos /bin/bash
[root@bd1b8900c547 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
退出容器命令:
#exit 停止并退出容器(后台方式运行则仅退出)
#Ctrl+P+Q 不停止容器退出
[root@bd1b8900c547 /]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]#
列出运行过的容器命令:
#docker ps
# 列出当前正在运行的容器
-a # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bca129320bb5 centos “/bin/bash” 4 minutes ago Exited (0) 3 minutes ago optimistic_shtern
bd1b8900c547 centos “/bin/bash” 6 minutes ago Exited (0) 5 minutes ago cool_tesla
cf6adbf1b506 bf756fb1ae65 “/hello” 5 hours ago Exited (0) 5 hours ago optimistic_darwin
删除容器命令:
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器命令:
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止当前容器
10.4 其他常用命令
10.4 1 日志的查看
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
–details Show extra details provided to logs
-f, --follow Follow log output
–since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default “all”)
-t, --timestamps Show timestamps
–until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
常用:
docker logs -tf 容器id
docker logs --tail number 容器id #num为要显示的日志条数
#docker容器后台运行,必须要有一个前台的进程,否则会自动停止
#编写shell脚本循环执行,使得centos容器保持运行状态
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d centos /bin/sh -c “while true;do echo hi;sleep 5;done”
c703b5b1911ff84d584390263a35707b6024816e1f46542b61918a6327a570dc
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c703b5b1911f centos “/bin/sh -c 'while t…” 13 seconds ago Up 10 seconds pedantic_banach
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs -tf --tail 10 c703b5b1911f
2020-12-27T03:34:07.255599560Z hi
2020-12-27T03:34:12.257641517Z hi
2020-12-27T03:34:17.259706294Z hi
2020-12-27T03:34:22.261693707Z hi
2020-12-27T03:34:27.262609289Z hi
2020-12-27T03:34:32.267862677Z hi
2020-12-27T03:34:37.270382873Z hi
2020-12-27T03:34:42.272414182Z hi
2020-12-27T03:34:47.274823243Z hi
2020-12-27T03:34:52.277419274Z hi
10.4 2 查看容器中进程信息
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker top c703b5b1911f
UID PID PPID C STIME TTY TIME CMD
root 11156 11135 0 11:31 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 11886 11156 0 11:43 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
10.4.3 查看容器的元数据
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker inspect 容器id
10.4.4 进入当前正在运行的容器
因为通常我们的容器都是使用后台方式来运行的,有时需要进入容器修改配置
方式一:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bash
[root@c703b5b1911f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@c703b5b1911f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:31 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 279 0 0 03:54 pts/0 00:00:00 /bin/bash
root 315 1 0 03:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
root 316 279 0 03:56 pts/0 00:00:00 ps -ef
方式二:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker attach c703b5b1911f
docker exec 进入容器后开启一个新的终端,可以在里面操作
docker attach 进入容器正在执行的终端,不会启动新的进程
10.4.5 拷贝操作
拷贝操作的命令如下:
#拷贝容器的文件到主机中
docker cp 容器id:容器内路径 目的主机路径
#拷贝宿主机的文件到容器中
docker cp 目的主机路径 容器id:容器内路径
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bash
[root@c703b5b1911f /]# cd home
[root@c703b5b1911f home]# ls
#touch 新建文件
[root@c703b5b1911f home]# touch test.java
[root@c703b5b1911f home]# ls
test.java
[root@c703b5b1911f home]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c703b5b1911f centos "/bin/sh -c 'while t…" 35 minutes ago Up 35 minutes pedantic_banach
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker cp c703b5b1911f:/home/test.java /home
[root@iZwz99sm8v95sckz8bd2c4Z ~]# ls /home
hai pan test.java
命令小节的图解如下:
11. 图形化管理工具Portaniner安装
Portaniner是Docker的图形化管理工具,类似的工具还有Rancher(CI/CD再用)
下载运行Portaniner镜像并运行,设置本机映射端口为8088
[root@localhost conf]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
94cfa856b2b1: Pull complete
49d59ee0881a: Pull complete
a2300fd28637: Pull complete
Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
8c525a0137be22965bd1e3944da622a2c4248f8ad20883f4b3ea4f8a6b11e163
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7789d4505a00 portainer/portainer "/portainer" 6 seconds ago Up 5 seconds 0.0.0.0:8088->9000/tcp quirky_sinoussi
第一次登录设置admin用户的密码
如果是阿里云服务器记得设置安全组,选择连接本地的Docker,整体界面预览如下图:
12. Docker镜像详解
12.1 什么是镜像
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需要的所有内容,包括代码,运行时(一个程序在运行或者在被执行的依赖)、库,环境变量和配置文件。
12.2 Docker镜像加载原理
Docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统是UnionFS联合文件系统。
12.3 分层理解
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker image inspect nginx:latest
[
{
"Id": "sha256:ae2feff98a0cc5095d97c6c283dcd33090770c76d63877caa99aefbbe4343bdd",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9"
],
"Parent": "",
"Comment": "",
"Created": "2020-12-15T20:21:00.007674532Z",
"Container": "4cc5da85f27ca0d200407f0593422676a3bab482227daee044d797d1798c96c9",
"ContainerConfig": {
"Hostname": "4cc5da85f27c",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.19.6",
"NJS_VERSION=0.5.0",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:13bffe371b56f4aeed88218ec17d0c6f653a83b49bd3e211fc8cfa2ca5d7a3d3",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <[email protected]>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "19.03.12",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.19.6",
"NJS_VERSION=0.5.0",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:13bffe371b56f4aeed88218ec17d0c6f653a83b49bd3e211fc8cfa2ca5d7a3d3",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <[email protected]>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 132935043,
"VirtualSize": 132935043,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/cb791e78a08db7091bf2ce1d78603f1758f52199e57f1805156fe30e39067aae/diff:/var/lib/docker/overlay2/1e73a72b25af68ee9abf4eb443f778d31226e12e9af428fcc14c7b044c83b258/diff:/var/lib/docker/overlay2/88c9c01762f2af8327db65d0b0d4a64785e87c9c2ab76c62e7d03619db03a985/diff:/var/lib/docker/overlay2/7304ab112ac4a9cb91fc6f74730be28fecbe19f042e92d321aa9181424cc4b2e/diff",
"MergedDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/merged",
"UpperDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/diff",
"WorkDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9",
"sha256:5c4e5adc71a82a96f02632433de31c998c5a9e2fccdcbaee780ae83158fac4fa",
"sha256:7d2b207c26790f693ab1942bbe26af8e2b6a14248969e542416155a912fec30d",
"sha256:2c7498eef94aef8c40d106f3e42f7da62b3eee8fd36012bf7379becc4cd639a2",
"sha256:4eaf0ea085df254fd5d2beba4e2c11db70a620dfa411a8ad44149e26428caee4"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
这里指示了分层信息:
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9",
"sha256:5c4e5adc71a82a96f02632433de31c998c5a9e2fccdcbaee780ae83158fac4fa",
"sha256:7d2b207c26790f693ab1942bbe26af8e2b6a14248969e542416155a912fec30d",
"sha256:2c7498eef94aef8c40d106f3e42f7da62b3eee8fd36012bf7379becc4cd639a2",
"sha256:4eaf0ea085df254fd5d2beba4e2c11db70a620dfa411a8ad44149e26428caee4"
]
},
12.4 提交镜像
使用docker commit 命令提交容器成为一个新的版本
docker commit -m=“提交的描述信息” -a=“作者” 容器id 目标镜像名:[TAG]
由于默认的Tomcat镜像的webapps文件夹中没有任何内容,需要从webapps.dist中拷贝文件到webapps文件夹。下面自行制作镜像:就是从webapps.dist中拷贝文件到webapps文件夹下,并提交该镜像作为一个新的镜像。使得该镜像默认的webapps文件夹下就有文件。具体命令如下:
#1.复制文件夹
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it tomcat /bin/bash
root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../
root@2a3bf3eaa2e4:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a3bf3eaa2e4 tomcat "/bin/bash" 4 minutes ago Up 4 minutes 8080/tcp competent_torvalds
7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it 2a3bf3eaa2e4 /bin/bash
root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../
root@2a3bf3eaa2e4:/usr/local/tomcat# read escape sequence
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a3bf3eaa2e4 tomcat "/bin/bash" 8 minutes ago Up 8 minutes 8080/tcp competent_torvalds
7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi
#2.提交镜像作为一个新的镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker commit -m=“add webapps” -a=“Ethan” 2a3bf3eaa2e4 mytomcat:1.0
sha256:f189aac861de51087af5bc88a5f1de02d9574e7ee2d163c647dd7503a2d3982b
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 f189aac861de 7 seconds ago 653MB
mysql 5.7 f07dfa83b528 6 days ago 448MB
tomcat latest feba8d001e3f 10 days ago 649MB
nginx latest ae2feff98a0c 12 days ago 133MB
centos latest 300e315adb2f 2 weeks ago 209MB
portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB
#3.运行容器
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it mytomcat:1.0 /bin/bash
root@1645774d4605:/usr/local/tomcat# cd webapps
root@1645774d4605:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
wz99sm8v95sckz8bd2c4Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 f189aac861de 7 seconds ago 653MB
mysql 5.7 f07dfa83b528 6 days ago 448MB
tomcat latest feba8d001e3f 10 days ago 649MB
nginx latest ae2feff98a0c 12 days ago 133MB
centos latest 300e315adb2f 2 weeks ago 209MB
portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB
13. 常用容器部署
13.1 Nginx部署
(1)搜索并下载镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14207 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1932 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 797 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 137
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 123
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 107 [OK]
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
6ec7b7d162b2: Already exists
cb420a90068e: Pull complete
2766c0bf2b07: Pull complete
e05167b6a99d: Pull complete
70ac9d795e79: Pull complete
Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 5 days ago 448MB
nginx latest ae2feff98a0c 11 days ago 133MB
centos latest 300e315adb2f 2 weeks ago 209MB
可以到dockerhub官网查看Nginx的详细版本信息 :https://hub.docker.com/_/nginx
(2)运行测试
docker run -d --name nginx01 -p 3334:80 nginx
-d 后台运行
–name 给容器命名
-p 3334:80 将宿主机的端口3334映射到该容器的80端口
运行结果:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bash
Error: No such container: nginx01
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d --name nginx01 -p 3334:80 nginx
20c896637ff5de8be835797109d62ee2465e28d9d716be5a8d550ef7d547fcf5
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20c896637ff5 nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:3334->80/tcp nginx01
端口暴露的概念:
(3)配置文件
进入容器,自定义配置文件
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bash
root@20c896637ff5:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@20c896637ff5:/# cd /etc/nginx
root@20c896637ff5:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@20c896637ff5:/# cd /etc/nginx
root@20c896637ff5:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
(4)访问测试
本地主机访问测试,curl命令发起请求,如果使用阿里云服务器需要设置安全组。
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
20c896637ff5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:3334->80/tcp nginx01
[root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3334
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href=“http://nginx.org/”>nginx.org</a>.<br/>
Commercial support is available at
<a href=“http://nginx.com/”>nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
(5)安装vim
我们使用Nginx往往需要编写配置文件,但是Nginx官方镜像没有安装vim,需要我们手动进行安装。使用以下命令进行安装:
apt-get install vim
如果执行上述命令出现提示:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
则需要先同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。执行以下命令来更新:
apt-get update
更新完毕再安装即可。我们修改了配置文件,只要重新启动容器docker restart 容器id
,改动就可以生效了。
解决vim在终端不能复制的问题:在vim 中输入 :set mouse=r。
拓展:启动项目并设置数据卷,为避免nginx因为修改配置文件导致的错误而无法启动容器,我们可以通过cp命令覆盖配置文件,但是设置数据卷会更为方便。启动Nginx容器的同时设置数据卷的命令:
docker run
--name my_nginx
-d -p 80:80
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
-v /data/nginx/log:/var/log/nginx
-v /data/nginx/html:/usr/share/nginx/html
nginx
参数说明:
第一个-v:挂载nginx的主配置文件,以方便在宿主机上直接修改容器的配置文件
第二个-v:挂载容器内nginx的日志,容器运行起来之后,可以直接在宿主机的这个目录中查看nginx日志
第三个-v:挂载静态页面目录
13.2 Tomcat部署
(1)下载并运行
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
6c33745f49b4: Pull complete
ef072fc32a84: Pull complete
c0afb8e68e0b: Pull complete
d599c07d28e6: Pull complete
e8a829023b97: Pull complete
d04be46a31d1: Pull complete
db6007c69c35: Pull complete
e4ad4c894bce: Pull complete
248895fda357: Pull complete
277059b4cba2: Pull complete
Digest: sha256:57dae7dfb9b62a413cde65334c8a18893795cac70afc3be589c8336d8244655d
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d -p 3335:8080 --name tomcat01 tomcat
7136295a6082cb0f805b025a1471bde02ead4864be3e2c9dcd337b1dde0a3113
(2)进入容器
1.容器中的命令是少了
2.阿里云镜像默认下载的是最小的镜像,保证最小的运行环境。
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it tomcat01 /bin/bash
root@7136295a6082:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
root@7136295a6082:/usr/local/tomcat# cd webapps.dist
root@7136295a6082:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
root@7136295a6082:/usr/local/tomcat/webapps.dist# cd ROOT
root@7136295a6082:/usr/local/tomcat/webapps.dist/ROOT# ls
RELEASE-NOTES.txt WEB-INF asf-logo-wide.svg bg-button.png bg-middle.png bg-nav.png bg-upper.png favicon.ico index.jsp tomcat.css tomcat.svg
root@7136295a6082:/usr/local/tomcat/webapps.dist/ROOT# cd ../../
root@7136295a6082:/usr/local/tomcat# cd webapps
root@7136295a6082:/usr/local/tomcat/webapps# ls
root@7136295a6082:/usr/local/tomcat/webapps# cp -r /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps/
root@7136295a6082:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@7136295a6082:/usr/local/tomcat/webapps# exit
exit
(3)访问测试
[root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3335
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“UTF-8” />
<title>Apache Tomcat/9.0.41</title>
<link href=“favicon.ico” rel=“icon” type=“image/x-icon” />
<link href=“tomcat.css” rel=“stylesheet” type=“text/css” />
</head>
<span class="token operator"><</span>body<span class="token operator">></span>
<span class="token operator"><</span>div <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"wrapper"</span><span class="token operator">></span>
<span class="token operator"><</span>div <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"navigation"</span> <span class="token assign-left variable">class</span><span class="token operator">=</span><span class="token string">"curved container"</span><span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-home"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"https://tomcat.apache.org/"</span><span class="token operator">></span>Home<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-hosts"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"/docs/"</span><span class="token operator">></span>Documentation<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-config"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"/docs/config/"</span><span class="token operator">></span>Configuration<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-examples"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"/examples/"</span><span class="token operator">></span>Examples<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-wiki"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"https://wiki.apache.org/tomcat/FrontPage"</span><span class="token operator">></span>Wiki<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-lists"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"https://tomcat.apache.org/lists.html"</span><span class="token operator">></span>Mailing Lists<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>span <span class="token assign-left variable">id</span><span class="token operator">=</span><span class="token string">"nav-help"</span><span class="token operator">></span><span class="token operator"><</span>a <span class="token assign-left variable">href</span><span class="token operator">=</span><span class="token string">"https://tomcat.apache.org/findhelp.html"</span><span class="token operator">></span>Find Help<span class="token operator"><</span>/a<span class="token operator">></span><span class="token operator"><</span>/span<span class="token operator">></span>
<span class="token operator"><</span>br <span class="token assign-left variable">class</span><span class="token operator">=</span><span class="token string">"separator"</span> /<span class="token operator">></span>
<span class="token operator"><</span>/div<span class="token operator">></span>
13.3 ElasticSearch部署
添加 ’-e ES_JAVA_OPTS=“-Xms128m -Xmx512m” ‘ 配置ElasticSearch的虚拟机占用的内存大小。
docker stats 查看资源占用情况
$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms128m -Xmx512m" elasticsearch:7.6.2
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d --name elasticsearch01 -p 9200:9200 -p 9300:9300 -e “discovery.type=single-node” -e ES_JAVA_OPTS=“-Xms128m -Xmx512m” elasticsearch:7.6.2
3b8cd4991814896c523ee67b84ce198e32bd82b1a62d512b198138a58ca946f1
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b8cd4991814 elasticsearch:7.6.2 “/usr/local/bin/dock…” 10 seconds ago Up 6 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch01
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker stats
13.4 MySQL部署
(1)下载并运行
#拉取并运行容器
docker run -d --name mysql-5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
参数说明:
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 默认账号root 用户的密码。
(2)进入容器查看MySQL服务
进入容器,并通过账号root查看MySQL服务能否正常连接。
docker exec -it 9b3aad6819ff /bin/bash
mysql -h localhost -u root -p
Docker系列文章:
1.Docker快速入门总结笔记
2.Docker容器数据卷详解
3.Dockerfile详解与镜像发布
4.Docker网络详解与实战
笔记总结:https://www.bilibili.com/video/BV1og4y1q7M4