文章目录
- 前言
- Docker 概述
-
- Docker 架构图
- Docker安装
-
- 在线安装
- 离线安装
- Docker 常用命令
-
- 帮助命令(--help)
- 镜像命令(images)
-
- docker images 查看镜像列表
- docker search 搜索镜像
- docker rmi 删除镜像
- 容器命令(container)
-
- docker run 新建/启动容器
- docker ps 查看容器
- exit 退出容器
- docker start/stop/restart 启停容器
- docker rm 删除容器
- docker commit 生成新镜像
- docker export 导出容器是归档文件
- docker import 镜像是从归档文件生成的
- docker save 保存镜像为归档文件
- docker load 从归档文件中加载镜像
- 其他命令常用
-
- docker run -d 后台启动容器
- docker logs 查看日志
- docker top 查看容器内的过程信息
- docker inspect 检查镜像的元数据
- docker exec/attach 进入运行容器
- docker cp 将容器中的文件复制到主机上
- docker events 从服务器获取实时事件
- Docker命令总结
- Docker镜像分层原理
-
- Docker简述
- 文件联合系统(UnionFS)
- Docker使用UnionFS
-
- 思考
- 写时拷贝(Copy-on-Write)
- Docker 镜像原理
- Docker 数据卷
-
- 挂载数据卷
- 具名挂载和匿名挂载
-
- 具名挂载
- 匿名挂载
- 容器间数据共享
- 初识Dockerfile
- DockerFile
-
- 简述
- DockerFile指令
-
- CMD 和 ENTRYPOINT的区别
- Dockerfile 流程小结图
- Docker 网络
-
- Docker0
- --link
- 自定义网络
- 跨网络连接容器
- Docker Compose
-
- OVERVIEW(概述)
- compose 安装
- compose 实例
-
- Step 1: Setup
- Step 2: Create a Dockerfile
- Step 3: Define services in a Compose file
- Step 4: Build and run your app with Compose
- Step 5: Edit the Compose file to add a bind mount
- Step 6: Re-build and run the app with Compose
- Step 7: Update the application
- Step 8: Experiment with some other commands
- yaml 文件
前言
Docker官网网址:https://docs.docker.com/ (强烈建议初学者参考官方文件学习使用。Docker所有学习文档! 参考本教程笔记 Docker官方文档 及 结合记录学习
Docker 概述
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
- Docker是开发、发布和运行应用程序的开源平台
- Docker为了快速交付软件,可以将应用程序与基础设施分开
- 使用Docker,基础设施(架构)可以像管理应用程序一样管理
- 通过Docker快速交付、测试和部署代码的方法可以显著减少编写代码和在生产环境中运行的延迟
- 以上概述来源于官方文档,此处仅作了解入门,详情请参考官方文档,建议阅读英文文档。
Docker 架构图
docker镜像就像一个模板,可以创建(一个或多个)容器服务,最终服务或项目在容器中运行。
Docker通过镜像创建的容器技术,独立运行(一组或一组)应用。
基本命令:启动、停止、删除
可以理解为简单linux系统
仓库是存放镜像的地方,分为共有仓库和私有仓库
Docker Hub/阿里云镜像仓库(配置镜像加速)等
Docker安装
准备:
- 了解linux基础命令
- centos服务器 (vps或本地虚拟机VMware)
- 若使用vps,需要安装远程连接软件,如MobaXterm
官方文档——在CentOS上安装Docker引擎 https://docs.docker.com/engine/install/centos/
在线安装
- 不推荐!
官方docker仓库下载安装极慢,不推荐此方法 (若设置为国内镜像源,如阿里云仓库,也可在线安装,安装教程详看阿里云官方教程)
# Uninstall old versions (卸载旧版本)
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# Set up the repository (设置docker仓库)
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 启动docker
sudo systemctl start docker
# 验证docker
sudo docker run hello-world
# 查看可用版本(可跳过)
yum list docker-ce --showduplicates | sort -r
# 安装指定版本(可跳过)
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin
- ( 推荐!
:docker官方仓库,下载速度极慢,可替换为国内仓库 https://get.daocloud.io/docker
curl -sSL https://get.daocloud.io/docker | sh
# 卸载docker
sudo yum remove docker \
docker-common \
container-selinux \
docker-selinux \
docker-engine \
# 删除相关文件夹
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
离线安装
推荐!
# 下载安装包,yum install docker安装包即可
sudo yum install /path/to/package.rpm
# 启动docker
sudo systemctl start docker
# 查看版本
docker --version
Docker 常用命令
Docker参考文档:https://docs.docker.com/reference/ 例: Docker CLI (docker run)参考文档:https://docs.docker.com/engine/reference/run/
帮助命令(–help)
# docker 帮助命令
docker 命令 --help
# 显示docker版本信息
docker version
# 显示docker详细信息
docker info
镜像命令(images)
docker images 查看镜像列表
[root@VM-16-4-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nextcloud-example_proxy latest 792f6d7fadda 4 weeks ago 42.1MB REPOSITORY # 镜像的仓库源 TAG # 镜像的
标签 IMAGE ID # 镜像的ID CREATED # 镜像的创建时间 SIZE # 镜像的大小 # 可选项 -a --all # 列出所有镜像 -q --quiet # 只显示镜像的ID [root@VM-16-4-centos ~]# docker images -aq 792f6d7fadda 61a190336ff6 cc44224bfe20 130b9666de2e
docker search 搜索镜像
[root@VM-16-4-centos ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16904 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 168
bitnami/nginx Bitnami nginx Docker Image 131 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 50
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 18 [OK]
rancher/nginx-ingress-controller 10
clearlinux/nginx Nginx reverse proxy server with the benefits… 4
# 可选项,通过搜索来过滤
-f, --filter filter Filter output based on conditions provided
# 筛选stars大于100的
[root@VM-16-4-centos ~]# docker search nginx -f=STARS=100
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16904 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 168
bitnami/nginx Bitnami nginx Docker Image 131 [OK]
[root@VM-16-4-centos ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
[root@VM-16-4-centos ~]# docker pull python
Using default tag: latest # 镜像版本,如果不写tag,默认为latest
latest: Pulling from library/python
e756f3fdd6a3: Pull complete # 分层下载,docker images的核心,联合文件系统
bf168a674899: Pull complete
e604223835cc: Pull complete
6d5c91c4cd86: Pull complete
2cc8d8854262: Pull complete
2767dbfeeb87: Pull complete
e5f27d860d89: Pull complete
98a3e4f5f5ed: Pull complete
5f15c8bc4073: Pull complete
Digest: sha256:cddebe04ec7846e28870cf8624b46313a22e6407b51ced3776588784caa12d27
Status: Downloaded newer image for python:latest
docker.io/library/python:latest # 真实地址
# 查看刚才下载的镜像
[root@VM-16-4-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest e4ccc57bca82 2 days ago 920MB
docker rmi 删除镜像
docker rmi -f 容器名/id # 删除单个容器
docker rmi -f 容器id 容器id # 删除多个容器
docker rmi -f $(docker images -aq) # 删除全部容器
容器命令(container)
docker run 新建/启动容器
[root@VM-16-4-centos ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
--name 容器名字,用来区分容器
-d Run container in background and print container ID
-it 使用交互方式运行,进入容器查看内容
-p 指定容器端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-P 随机指定端口
# 启动并进入容器
[root@VM-16-4-centos ~]# docker run -it python /bin/bash
root@6e47c6e12ff8:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@6e47c6e12ff8:/# exit
exit
docker run -dit python /bin/bash #启动后台交互式容器
docker ps 查看容器
docker ps # 列出当前正在运行的容器
-a # 列出当前+历史运行过的容器
-n=? #显示最近的容器(个数)
-q # 只显示容器编号
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96016d8178a9 python "python3" 2 minutes ago Up 2 minutes xenodochial_driscoll
[root@VM-16-4-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96016d8178a9 python "python3" 2 minutes ago Up 2 minutes xenodochial_driscoll
b0888c48befa python "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago admiring_brown
exit 退出容器
#退出
exit
ctrl + P + Q # 退出不停止
docker start/stop/restart 启停容器
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器
docker rm 删除容器
docker rm 容器ID # 删除停止的容器,不能删除运行的容器,-f 可强制删除
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
docker commit 生成新镜像
docker commit 提交容器成为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[tag]
# docker commit 帮助命令
[root@VM-16-4-centos ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@VM-16-4-centos ~]# docker commit -a="zzh" -m="volume1 2" 6cd225ef78d9 python_volume:2.0
sha256:d7efce3de1532b9934dd78512797833f766eb7c674302a3abf8c72825e3e77aa
[root@VM-16-4-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python_volume 2.0 d7efce3de153 7 seconds ago 920MB
docker export 导出容器为归档文件
# docker export -o 归档文件名 容器id
[root@VM-16-4-centos docker]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
-o, --output string Write to a file, instead of STDOUT
# docker export
[root@VM-16-4-centos docker]# docker export -o python_export 6cd225ef78d9
[root@VM-16-4-centos docker]# ls -hl
total 890M
-rw------- 1 root root 890M Jul 3 20:29 python_export
docker import 从归档文件生成镜像
# docker import 归档文件 REPOSITORY[:TAG]
[root@VM-16-4-centos docker]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
--platform string Set platform if server is multi-platform capable
[root@VM-16-4-centos docker]# docker import python_export python_export:1.0
sha256:ba99372f6da8c387d9d41e7cb006ba564ffbc980aa598120a88b7a91f1b961a7
[root@VM-16-4-centos docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python_export 1.0 ba99372f6da8 6 seconds ago 911MB
docker save 保存镜像为归档文件
# docker save -o 镜像名1 2 3 ...
[root@VM-16-4-centos docker]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
[root@VM-16-4-centos docker]# docker save -o python_save python_export python_volume
[root@VM-16-4-centos docker]# ll
total 2740716
drwxr-xr-x 2 root root 4096 Jul 2 13:37 docker_file
-rw-r--r-- 1 root root 7 Jul 2 13:04 docker_v
-rw------- 1 root root 932292608 Jul 3 20:29 python_export
-rw------- 1 root root 1874179584 Jul 3 20:44 python_save
docker load 从归档文件加载镜像
# docker load -i 归档文件名
[root@VM-16-4-centos docker]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
[root@VM-16-4-centos docker]# docker load -i python_save
Loaded image: python_volume:2.0
Loaded image: python_export:1.0
Loaded image: python_volume:1.0
常用其他命令
docker run -d 后台启动容器
docker run -d 镜像名 # 后台启动
# docker ps 发现没有正在运行的容器
# docker 容器使用后台运行,如果没有前台进程,会自动停止
# 即容器启动后,发现自己没有提供服务,会立刻停止
docker run -dit 镜像名 /bin/bash # 可以启动不会自动停止的后台容器
docker logs 查看日志
docker logs -f --tail 100 容器id/name
# 持续查看最新日志
[root@VM-16-4-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)
docker top 查看容器内进程信息
# docker top 容器id
[root@VM-16-4-centos ~]# docker top d2df53bc7527
UID PID PPID C STIME TTY TIME CMD
root 2848722 2848702 0 00:05 pts/0 00:00:00 bash
docker inspect 查看镜像的元数据
# docker inspect 容器id [root@VM-16-4-centos ~]# docker inspect d2df53bc7527 [ { "Id": "d2df53bc75271b7154ad9b32cc89304501ae0d5c92e21aa00eee616f1c338fb6", "Created": "2022-06-29T16:05:43.20344352Z", "Path": "bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2848722, "ExitCode": 0, "Error": "", "StartedAt": "2022-06-29T16:05:43.582446583Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:0f95b1e38607bbf15b19ad0d111f2316e92eb047a35370eac71973c636acb9d2", "ResolvConfPath": "/var/lib/docker/containers/d2df53bc75271b7154ad9b32cc89304501ae0d5c92e21aa00eee616f1c338fb6/resolv.conf", "HostnamePath": "/var/lib/docker/containers/d2df53bc75271b7154ad9b32cc89304501ae0d5c92e21aa00eee616f1c338fb6/hostname", "HostsPath": "/var/lib/docker/containers/d2df53bc75271b7154ad9b32cc89304501ae0d5c92e21aa00eee616f1c338fb6/hosts", "LogPath": "/var/lib/docker/containers/d2df53bc75271b7154ad9b32cc89304501ae0d5c92e21aa00eee616f1c338fb6/d2df53bc75271b7154ad9b32cc89304501ae0d5c92e21aa00eee616f1c338fb6-json.log", "Name": "/amazing_einstein", "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/d1f97469bf8dc721d3544ee0603843c45766d38fe08a5a41448a09fb2b0523c0-init/diff:/var/lib/docker/overlay2/a34dc8c6fd5f524c445da51fff81a50b3e15cf53c1db69aadca32c732b88209b/diff:/var/lib/docker/overlay2/f4cd5061845ac5514f5aaa387dd58b63ea37c14d4383e6f01360c5ab1a6357b5/diff:/var/lib/docker/overlay2/a2eeebe65fe6050b40368dfaaf6115998e46544636faf3c820c686d7fb1bde16/diff:/var/lib/docker/overlay2/4866d5cf1beb0816ba148e1700d92ea0c42cf1f7ba1ffd80e53113f95dcc2c8e/diff:/var/lib/docker/overlay2/bc1115c2e1d53bfd674718bdd08ef6a2c82c73667067fed2e05c18f2131e1c3d/diff:/var/lib/docker/overlay2/616914d1a8c57dd7386b550dbcd1ba501d11c09380afffa39fd227dcf3e4123d/diff:/var/lib/docker/overlay2/34ed671ac7b1d2cf8e30cf0f3cf85da97c34b643e217325c8110dea59c36c730/diff:/var/lib/docker/overlay2/b4e5edd486054fdcfbc558c6c2b64ccdd27e62776e6330828beab3674acfdcd6/diff:/var/lib/docker/overlay2/a09d8bfe4e8ff39f2b4b6589d36dc5bb97d3440aa2afc8d86e671a584f460eaa/diff", "MergedDir": "/var/lib/docker/overlay2/d1f97469bf8dc721d3544ee0603843c45766d38fe08a5a41448a09fb2b0523c0/merged", "UpperDir": "/var/lib/docker/overlay2/d1f97469bf8dc721d3544ee0603843c45766d38fe08a5a41448a09fb2b0523c0/diff", "WorkDir": "/var/lib/docker/overlay2/d1f97469bf8dc721d3544ee0603843c45766d38fe08a5a41448a09fb2b0523c0/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "d2df53bc7527", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "GPG_KEY=A035C8C19219BA821ECEA86B64E628F8D684696D", "PYTHON_VERSION=3.10.5", "PYTHON_PIP_VERSION=22.0.4", "PYTHON_SETUPTOOLS_VERSION=58.1.0", "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/6ce3639da143c5d79b44f94b04080abf2531fd6e/public/get-pip.py", "PYTHON_GET_PIP_SHA256=ba3ab8267d91fd41c58dbce08f76db99f747f716d85ce1865813842bb035524d" ], "Cmd": [ "bash" ], "Image": "python", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { } }, "NetworkSettings": { "Bridge": "", "SandboxID": "71af81ca4e9382a5c7cc63a1266c4b60aa27306fbe7c35ea57ff784977dd905f", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { }, "SandboxKey": "/var/run/docker/netns/71af81ca4e93", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "10cd7e0d3967516c2a6e93d2e7406a3e64d8132cf456544e3d7679193b639d4c", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.3", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:03", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "b7c80489a8d359c23120b77e174cb055562d692c0f23aa6336a3725ecbecb1c1", "EndpointID": "10cd7e0d3967516c2a6e93d2e7406a3e64d8132cf456544e3d7679193b639d4c", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.3", "IPPrefixLen": 16