安装Docker
连接阿里云服务器
- 重置实例密码
- 配置安全组
-
环境查看
root@iZbp16gi14kuk4bpt0kf69Z:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.7 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.7 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
安装
卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
更新需要的安装包
```bash sudo apt-get update
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg
<a name="9NRTy"></a>
#### 添加docker's official gpg key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg —dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
https://mzsb7ers.mirror.aliyuncs.com
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-‘EOF’ { “registry-mirrors”: [“https://mzsb7ers.mirror.aliyuncs.com“] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
echo \ “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
![image.png](https://cdn.nlark.com/yuque/0/2021/png/1734557/1614911057335-e8f83f57-7ea0-4379-ae3f-75dd7342a209.png#align=left&display=inline&height=459&margin=%5Bobject%20Object%5D&name=image.png&originHeight=459&originWidth=743&size=36899&status=done&style=none&width=743)
/var/lib/docker docker的默认工作环境
<a name="tsXeT"></a>
## 底层原理
<a name="iiYTV"></a>
### docker是怎么工作的
Docker是一个Client-Server结构的系统,Docker的守护运行在主机上,通过Socker从客户端访问<br />DockerServer接收到Docker-Client的指令,就会执行这个命令
<a name="S2lll"></a>
### Docker为什么比VM快
1. Docker有着比虚拟机更少的抽象层
![](https://cdn.nlark.com/yuque/0/2021/png/1734557/1614912408398-2a3d2ab5-15a8-40e2-b541-d457e3e1cc92.png#align=left&display=inline&height=263&margin=%5Bobject%20Object%5D&originHeight=263&originWidth=529&size=0&status=done&style=none&width=529)
2. Docker利用的是宿主机的内核,VM需要的是guest OS
所以说,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS,分钟级别的,而Docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级
<a name="7OAMT"></a>
## Docker的常用命令
<a name="rPEf6"></a>
### 帮助命令
docker version # 显示docker的版本信息 docker info # 显示docker的系统命令,包括系统和镜像的数量 docker 命令 —help 万能命令
帮助文档的地址: [https://docs.docker.com/engine/reference/commandline/docker/](https://docs.docker.com/engine/reference/commandline/docker/)
<a name="Tr7H6"></a>
### 镜像命令
<a name="ltrgA"></a>
#### docker images 查看所有本地的主机上的镜像
```bash
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 14 months ago 13.3kB
# 解释
Repository 镜像的仓库源
tag 镜像的标签
image id 镜像的id
created 镜像的创建时间
size 镜像的大小
# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像ID
docker search 搜索镜像
docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10575 [OK]
mariadb MariaDB Server is a high performing open sou… 3956 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 775 [OK]
percona Percona Server is a fork of the MySQL relati… 527 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 48 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 39
prom/mysqld-exporter 37 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27
# 可选项,通过搜藏来过滤
--filter=STARS=3000 搜索出来的镜像就是大于3000的
root@iZbp16gi14kuk4bpt0kf69Z:/# docker search mysql --filter=stars=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10575 [OK]
mariadb MariaDB Server is a high performing open sou… 3956 [OK]
root@iZbp16gi14kuk4bpt0kf69Z:/#
docker pull docker下载
# 下载镜像docker pull 镜像名[:tag]
root@iZbp16gi14kuk4bpt0kf69Z:/# docker pull mysql
Using default tag: latest # 如果不加tag默认就是latest,默认下载最新
latest: Pulling from library/mysql
a076a628af6f: Pull complete # 分层下载,docker image的核心 联合文件系统
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 指定版本下载
root@iZbp16gi14kuk4bpt0kf69Z:/# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
root@iZbp16gi14kuk4bpt0kf69Z:/# docker rmi -f 容器id
root@iZbp16gi14kuk4bpt0kf69Z:/# docker rmi -f 容器id 容器id 容器id 容器id # 删除多个容器
root@iZbp16gi14kuk4bpt0kf69Z:/# docker rmi -f $(docker images -aq) # 删除全部的容器
容器命令
有了镜像才可以下载容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动 docker run
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01,tomcat用来区分容器
-d 后台运行方式
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-p 随机指定端口
# 测试,启动并进入容器
# docker run it centos /bin/bash
# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
# 从容器中退回主机
# exit
查看正在运行的容器 docker ps -a
docker ps
# 参数
#列出当前正在运行的容器
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 显示编号
docker ps -aq
退出容器
exit # 直接停止容器并退出
ctrl + p + q # 容器不停止退出
删除容器
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果要强制删除,rm -rf
docker rm -rf $(docker ps -aq) # 删除所有的容器
启动和停止容器
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器
常用其他命令
后台启动容器
# 命令docker run -d 镜像名
docker run -d centos
# 问题:docker ps ,发现centos停止了
# 常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有前台进程,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -t -f --tail 容器
# 显示日志
-tf # 显示日志,时间戳
--tail number # 要显示的日志条数
docker logs -tf --tail 10 容器id
查看容器中的进程信息
# docker top 容器id
docker top 0a37b92836f4
UID PID PPID C STIME TTY TIME CMD
root 28883 28860 0 21:10 pts/0 00:00:00 /bin/bash
查看镜像的源数据
# 命令
docker inspect 容器id
root@iZbp16gi14kuk4bpt0kf69Z:~# docker inspect 0a37b92836f4
[
{
"Id": "0a37b92836f4f49327a43eac0a89a7671dc562e3e1273f8d2bd9d7557e99d2b1",
"Created": "2021-03-07T13:10:08.806981023Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 28883,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-07T13:10:09.224987792Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/0a37b92836f4f49327a43eac0a89a7671dc562e3e1273f8d2bd9d7557e99d2b1/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/0a37b92836f4f49327a43eac0a89a7671dc562e3e1273f8d2bd9d7557e99d2b1/hostname",
"HostsPath": "/var/lib/docker/containers/0a37b92836f4f49327a43eac0a89a7671dc562e3e1273f8d2bd9d7557e99d2b1/hosts",
"LogPath": "/var/lib/docker/containers/0a37b92836f4f49327a43eac0a89a7671dc562e3e1273f8d2bd9d7557e99d2b1/0a37b92836f4f49327a43eac0a89a7671dc562e3e1273f8d2bd9d7557e99d2b1-json.log",
"Name": "/peaceful_darwin",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"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/af0bd59c7288bb9953f6aaf97d35039074575160f5447102545d7acaae57db4b-init/diff:/var/lib/docker/overlay2/eba4907e08a3a70e0de5e9f67b7cc5b5c5be4be59dd019435d11c63b758d2f84/diff",
"MergedDir": "/var/lib/docker/overlay2/af0bd59c7288bb9953f6aaf97d35039074575160f5447102545d7acaae57db4b/merged",
"UpperDir": "/var/lib/docker/overlay2/af0bd59c7288bb9953f6aaf97d35039074575160f5447102545d7acaae57db4b/diff",
"WorkDir": "/var/lib/docker/overlay2/af0bd59c7288bb9953f6aaf97d35039074575160f5447102545d7acaae57db4b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "0a37b92836f4",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "d3165be4e721fdd2bc1d2067f47fd29cadc3112b080800154b6ba99de1b28f7c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/d3165be4e721",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "866c32531013c8d9022660d9ed715a9ebf0877486b1d16f0e15c68aaa8985563",
"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": "9aec5ff3dc1cfd9ac4bea062d1fc993767571638d2fd53b245f25f35f5ad510a",
"EndpointID": "866c32531013c8d9022660d9ed715a9ebf0877486b1d16f0e15c68aaa8985563",
"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
}
}
}
}
]
进入当前正在运行的容器
# 通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bash shell
# 方式二
docker attach 容器id
# docker exec # 进入容器后开启一个新的终端,可以再里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 主机路径
作业
Docker 安装nginx
- 搜索镜像
- 下载镜像
```shell root@iZbp16gi14kuk4bpt0kf69Z:~# docker run -d —name nginx01 -p 3344:80 nginx addf33b67b93bb4ac5a4de9f144319d8fd05f26e4dc0167e43f084ab0f0d057e root@iZbp16gi14kuk4bpt0kf69Z:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES addf33b67b93 nginx “/docker-entrypoint.…” 9 seconds ago Up 7 seconds 0.0.0.0:3344->80/tcp nginx01 5d26034a3e93 nginx “/docker-entrypoint.…” About a minute ago Up About a minute 80/tcp practical_kare root@iZbp16gi14kuk4bpt0kf69Z:~# docker kill 5d26034a3e93 5d26034a3e93 root@iZbp16gi14kuk4bpt0kf69Z:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES addf33b67b93 nginx “/docker-entrypoint.…” 31 seconds ago Up 30 seconds 0.0.0.0:3344->80/tcp nginx01 root@iZbp16gi14kuk4bpt0kf69Z:~# curl localhost:3344 <!DOCTYPE html>Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
```