课程笔记跟自Bilibili 遇见狂神说
课程地址:【Java狂神说】Docker最新超详细版教程通俗易懂
文章目录
一、Docker概述
1.Docker为什么出现?
一款产品:开发–上线 两套环境!应用环境 应用配置!
开发 – 运维。问题:我在我的电脑上可以运行!版本更新,导致服务不可用!对于运维人员来说,考验就十分大?
环境配置是十分麻烦的,每一个机器都要部署环境(集群Redis、ES、Hadoop……)费时费力。
发布一个项目jar + (可能需要的环境:Redis MySQL jdk ES 部署十分麻烦) war
Q:项目能不能都带上环境安装打包?(Docker就是来干这个的)
之前在服务器配置一个应用环境Redis MySQL jdk Hadoop,配置超麻烦,不能够跨平台(java可以,但环境不可以 Windows,最后发布到Linux)
传统:jar,运维来做!
现在:开发打包部署上线,一套流程做完!
-
java – apk --发布 (应用商店)-- 张三使用apk – 安装即可用!
-
java – jar (环境)-- 打包项目带上环境(称之为镜像)-- (Docker仓库:类似商店)-- 下载我们发布的镜像 – 直接运行即可! 不用考虑环境
Docker给以上问题提出了解决方案!
Docker的思想就来自于集装箱!
JRE – 上面运行多个应用 (端口冲突 or 其他冲突)-- 原来是交叉的(用一套环境)!
现在把他们隔离:隔离就是docker的核心思想!打包装箱!每个箱子是互相隔离的。
Docker通过隔离机制,可以将服务器利用到极致!
2.Docker的历史
2010年,几个搞IT的年轻人,就在美国成立了一家公司dotCloud
做一些pass的云计算服务!LXC有关的容器技术!
他们将自己的技术(容器化技术)命名就是Docker!
Docker刚刚诞生的时候,没有引起行业的注意!dotCloud,就活不下去!
开源
开放源代码
2013年,Docker开源!
Docker越来越多的人发现了docker的优点!活了,Docker每个月都会更新一个版本!
2014年4月9日,Docker1.0发布!
Docker为什么这么火?十分轻巧!
在容器技术出来前,我们都是使用虚拟机技术!
虚拟机:在Window中装一个Vmware,通过这个软件我们可以虚拟出来一台或者多台电脑!笨重!
虚拟机也是属于虚拟化技术,Docker容器技术,也是一种虚拟化技术!
vm: Linux centos原生镜像(相当于一个电脑!)隔离,需要开启多个虚拟机!几个G 几分钟
docker: 隔离,镜像(最核心的环境 4m +jdk + musql)十分小巧,运行镜像就可以了!小巧! 几个M KB 秒级启动!
聊聊Docker
Docker是基于 Go 语言开发的!开源项目!
官网:https://www.docker.com/
文档地址:https://docs.docker.com/Docker的文档是超级详细的!
仓库地址:https://hub.docker.com/
3.Docker能干嘛
-
之前的虚拟机技术!
虚拟机技术缺点:
1.资源占用十分多
2.冗余步骤多
3.启动很慢! -
容器化技术
容器化技术不是模拟的一个完整的操作系统
比较Docker和虚拟技术的不同:
1.传统虚拟机,虚拟出硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件。
2.Docker容器内的应用直接运行在宿主机(运行在哪儿就叫宿主机)的内容,容器是没有自己的内核的(利用宿主机内核),也没有虚拟硬件。
3.每个容器都是互相隔离的,每个容器都有属于自己的文件系统,互不影响。
DevOps(开发、运维)
应用更快速的交付和部署
传统:一堆帮助文档,安装程序
Docker:打包镜像发布测试,一键运行
更便捷的升级和扩缩容
使用了Docker之后,我们部署应用就和搭积木一样!
(SpringBoot1.5 Redis1.5 tomcat 8)升级!
项目打包为一个镜像,扩展 服务器A!服务器B
更简单的系统运维
在容器化之后,我们的开发,测试环境都是高度一致的。
更高效的计算资源利用
Docker是内核级别的虚拟化,可以在一个物理机上运行很多的容器实例!服务器的性能可以被压榨到极致。
二、Docker安装
1.Docker的基本组成
docker架构
镜像(image):
docker镜像就好比是一个模板,可以通过这个模板来创建容器服务,tomcat镜像===>run===>tomcat01容器(提供服务的),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
容器(container):
Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的。
基本命令:启动,停止,删除
目前就可以把这个容器理解为一个简易的Linux系统
仓库(repository):
存放镜像的地方!
仓库分为公有仓库和私有仓库!
Docker Hub(默认是国外的)
阿里云……都有容器云服务器(配置镜像加速!比如maven就需要镜像加速)
2.安装Docker
环境准备:
1.Linux基础
2.centos 7
3.Xshell链接服务器进行操作
系统内核:
系统版本:
安装:
帮助文档:
# 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
# 3、设置镜像的仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo #默认是从国外的!
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo#推荐使用阿里云的十分的快
#更新yum软件包索引
centos 7 : yum makecache fast
centos 8 :yum makecache
# 4、安装docker相关的内容 docker-ce社区版 ee企业版
yum install docker-ce docker-ce-cli containerd.io
# 5、启动docker
systemctl start docker
#6、使用docker version检查是否安装成功
# 7、hello-world
docker run hello-world
# 8、查看一下这个下载的Hello-world 镜像
[root@VM-16-5-centos /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB
了解:卸载docker
# 1、卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2、删除资源
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
#/var/lib/docker docker的默认工作路径
3.腾讯云镜像加速
4.回顾Hello World流程
5.底层原理
Docker是怎么工作的?
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socker从客户端访问!
Docker Server接收到Docker-Client的指令,就会执行这个指令!
Docker为什么比vm快
1、Docker有着比虚拟机更少的抽象
2、docker利用宿主机的内核,VM需要的是Guest OS
新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机是需要加载Guest OS,分钟级别的,而docker是利用宿主机的操作系统,省略了这个复杂的系统,秒级。
Docker和VM的对比如下:
三、Docker常用命令
1.基础命令
docker version #查看docker的版本信息
docker info #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令(可查看可选的参数)
docker COMMAND --help
命令的帮助文档地址:https://docs.docker.com/engine/reference/commandline/docker/
2.镜像命令
- 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
- 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]
- 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
# 指定版本下载
docker pull mysql:5.7
[root@VM-16-5-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
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)
3.容器命令
说明:我们有了镜像才可以创建容器
如拉取一个centos容器
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="名字" 指定容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
(
-p ip:主机端口:容器端口 配置主机端口映射到容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
)
-P 随机指定端口(大写的P)
进入容器
# 测试 启动并进入容器
[root@VM-16-5-centos ~]# docker run -it centos /bin/bash
[root@cc2c0f737256 /]#
# 查看容器内的centos,基础版本,很多命令都是不完善的
[root@cc2c0f737256 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
推出容器
#从容器中退回主机
[root@cc2c0f737256 /]# exit
exit
[root@VM-16-5-centos ~]#
列出运行过的容器
# docker ps 命令
-a #列出当前正在运行的容器 + 带出历史运行过的容器
-n=? #显示最经创建的容器
-q #只显示容器的编号
[root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-16-5-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc2c0f737256 centos "/bin/bash" 6 minutes ago Exited (130) 2 minutes ago magical_banzai
832bed61b645 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago quirky_newton
[root@VM-16-5-centos ~]#
退出容器
exit #直接容器停止并退出
Ctrl+P+Q #容器不停止退出
删除容器
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 #强制停止当前容器
4.其他常用命令
后台启动容器
# 命令 docker run -d 镜像名
[root@VM-16-5-centos ~]# docker run -d centos
# Q: docker ps , 发现centos停止了
## 常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx 容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
查看日志
[root@VM-16-5-centos ~]# 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)
[root@VM-16-5-centos ~]#
常用:
docker logs -tf 容器id
docker logs --tail number 容器id #num为要显示的日志条数
#docker容器后台运行,必须要有一个前台的进程,否则会自动停止
#编写shell脚本循环执行,使得centos容器保持运行状态
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d centos [root@VM-16-5-centos ~]# docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done"
62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc
[root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62c62c79de2f centos "/bin/sh -c 'while t…" 31 seconds ago Up 31 seconds hardcore_poitras
[root@VM-16-5-centos ~]# ^C
[root@VM-16-5-centos ~]# docker logs -ft --tail 10 62c62c79de2f
2021-12-24T21:48:07.605257310Z hi
2021-12-24T21:48:12.606857443Z hi
2021-12-24T21:48:17.608452645Z hi
2021-12-24T21:48:22.610188996Z hi
2021-12-24T21:48:27.611977881Z hi
2021-12-24T21:48:32.613910487Z hi
2021-12-24T21:48:37.615574135Z hi
2021-12-24T21:48:42.617314057Z hi
2021-12-24T21:48:47.618973599Z hi
2021-12-24T21:48:52.620664556Z hi
2021-12-24T21:48:57.622521980Z hi
2021-12-24T21:49:02.624402772Z hi
查看容器中进程信息 ps
#命令 docker top 62c62c79de2f
[root@VM-16-5-centos ~]# docker top 62c62c79de2f
UID PID PPID C STIME TTY TIME CMD
root 98105 98086 0 05:48 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 99102 98105 0 05:52 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
[root@VM-16-5-centos ~]#
查看容器的元数据
# 命令 docker inspect 容器id
[root@VM-16-5-centos ~]# docker inspect 62c62c79de2f
[
{
"Id": "62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc",
"Created": "2021-12-24T21:48:02.310283057Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hi;sleep 5;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 98105,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-24T21:48:02.605422515Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc/hostname",
"HostsPath": "/var/lib/docker/containers/62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc/hosts",
"LogPath": "/var/lib/docker/containers/62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc/62c62c79de2f3cb77e12b0c57e0612e542117527c0dc694a107efdcc133a9cbc-json.log",
"Name": "/hardcore_poitras",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/c4f3081053962033225e19b0581e176e04e252483194d126375727b32e4d36c8-init/diff:/var/lib/docker/overlay2/c97da68408563dc983433da970a74ab601fdf60c7d4f5a419347833a84cc2239/diff",
"MergedDir": "/var/lib/docker/overlay2/c4f3081053962033225e19b0581e176e04e252483194d126375727b32e4d36c8/merged",
"UpperDir": "/var/lib/docker/overlay2/c4f3081053962033225e19b0581e176e04e252483194d126375727b32e4d36c8/diff",
"WorkDir": "/var/lib/docker/overlay2/c4f3081053962033225e19b0581e176e04e252483194d126375727b32e4d36c8/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "62c62c79de2f",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo hi;sleep 5;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "58ab991da98cb5851f0c0e5d43a1578489a5890b863ada37db118ef3d37a06bb",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/58ab991da98c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "067ddb8b64fb9407f840587910486bf16c031a78ef8e7de4484e626cebdb3b54",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "bd22e640820925a9055d6aaf2065ac382470962240dbf1a45776aacfcc9567a5",
"EndpointID": "067ddb8b64fb9407f840587910486bf16c031a78ef8e7de4484e626cebdb3b54",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@VM-16-5-centos ~]#
进入当前正在运行的容器
#我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令
docker exec 容器id bashShell
#测试
[root@VM-16-5-centos ~]# docker exec -it 62c62c79de2f /bin/bash
[root@62c62c79de2f /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@62c62c79de2f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Dec24 ? 00:00:02 /bin/sh -c while true;do ech
root 9269 0 0 10:40 pts/0 00:00:00 /bin/bash
root 9291 1 0 10:40 ? 00:00:00 /usr/bin/coreutils --coreuti
root 9292 9269 0 10:40 pts/0 00:00:00 ps -ef
[root@62c62c79de2f /]#
#方式二
docker attach 容器id
[root@VM-16-5-centos ~]# docker attach 62c62c79de2f
正在执行当前的代码……
docker exec #进入容器后开启一个新的终端,可以在里面操作
docker attach #进入容器正在执行的终端,不会启动新的进程
拷贝容器文件到主机
拷贝容器的文件到主机中
docker cp 容器id:容器内路径 目的主机路径
[root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-16-5-centos ~]# docker run -it centos /bin/bash
[root@782385c94f95 /]# [root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
782385c94f95 centos "/bin/bash" 15 seconds ago Up 14 seconds tender_mcnulty
[root@VM-16-5-centos ~]# docker attacj 782385c94f95
docker: 'attacj' is not a docker command.
See 'docker --help'
[root@VM-16-5-centos ~]# docker attact 782385c94f95
docker: 'attact' is not a docker command.
See 'docker --help'
[root@VM-16-5-centos ~]# docker attach 782385c94f95
[root@782385c94f95 /]# cd /home
[root@782385c94f95 home]# ls
[root@782385c94f95 home]# touch test.java
[root@782385c94f95 home]# ls
test.java
[root@782385c94f95 home]# exit
exit
[root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-16-5-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
782385c94f95 centos "/bin/bash" 2 minutes ago Exited (0) 13 seconds ago tender_mcnulty
[root@VM-16-5-centos ~]# docker cp 782385c94f95:/home/test.java /home
[root@VM-16-5-centos ~]# ls
[root@VM-16-5-centos ~]# cd /home
[root@VM-16-5-centos home]# ls
lighthouse test.java
[root@VM-16-5-centos home]#
5.小节
6.作业练习
Docker安装Nginx
# 1.搜索镜像 serach
# 2.下载镜像 pull
# 3.运行测试
[root@VM-16-5-centos ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16018 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2103 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 820 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 297
linuxserver/nginx An Nginx container, brought to you by LinuxS… 161
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 148 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 147 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 112 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 80 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 61 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 59
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 56
nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 31
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 25 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 22
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 16
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
bitwarden/nginx The Bitwarden nginx web server acting as a r… 12
flashspys/nginx-static Super Lightweight Nginx Image 11 [OK]
mailu/nginx Mailu nginx frontend 10 [OK]
webdevops/nginx Nginx container 9 [OK]
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 3 [OK]
wodby/nginx Generic nginx 1 [OK]
[root@VM-16-5-centos ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
f3409a9a9e73: Pull complete
9919a6cbae9c: Pull complete
fc1ce43285d7: Pull complete
1f01ab499216: Pull complete
13cfaf79ff6d: Pull complete
Digest: sha256:366e9f1ddebdb844044c2fafd13b75271a9f620819370f8971220c2b330a9254
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@VM-16-5-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6987c8d6ed5 4 days ago 141MB
mysql 5.7 c20987f18b13 4 days ago 448MB
centos latest 5d0da3dc9764 3 months ago 231MB
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
[root@VM-16-5-centos ~]# docker run -d --name nginx01 -p 3344:80 nginx
3989c87d475d0869ee9294eed3a5c170fa86a404fc5836dab9aae04cdd55e4fb
[root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3989c87d475d nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
[root@VM-16-5-centos ~]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
[root@VM-16-5-centos ~]#
#进入容器
[root@VM-16-5-centos ~]# docker exec -it nginx01 /bin/bash
root@3989c87d475d:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@3989c87d475d:/# cd /etc/nginx
root@3989c87d475d:/etc/nginx# ls
conf.d mime.types nginx.conf uwsgi_params
fastcgi_params modules scgi_params
root@3989c87d475d:/etc/nginx#
# 停止后就访问不了了
root@3989c87d475d:/etc/nginx# exit
exit
[root@VM-16-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3989c87d475d nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
[root@VM-16-5-centos ~]# docker stop 3989c87d475d
3989c87d475d
[root@VM-16-5-centos ~]#
端口暴露的概念
思考问题:我们每次改动nginx配置文件,都需要进入容器内部?十分的麻烦,我要是可以在容器的外部提供一个映射路径,达到在容器修改文件,容器内部就可以自动修改? -v 数据卷!