1 Docker概述
1.1 Docker诞生背景
开发和运维是互联网公司两大主干工种,开发写完程序之后经过测试发给运维上线,经典的对话:
运维小李:Hello,小王,你这程序有bug,我这里运行不起来啊!
开发小王:程序在我这运行没问题啊,怎么到你那就不行了!肯定是你环境的问题啊
运维小李:我的环境没问题,是你的程序的问题
开发小王:你的环境问题。。。。
运维小李:就是你的程序问题。。。
事件就这样过去了一个月,产品耽误了最佳上线时间,开发了半年的产品没有获得预期的收益,小李和小王被公司开除,开发组长和运维组长被降薪,公司承受了经济损失。
软件的环境配置很复杂,换一台机器就要重新配置环境,软件换一个版本也要重新配置环境,运维工程师苦不堪言。
再举一个例子,部署一个Python的Web程序,需要部署Nginx,python解释器,数据库,WSGI服务等等,如果我在Linux部署完了,不能迁移到windows,就算不跨平台,我重新部署到另外一台Linux下,还是要重新执行一遍命令。开发结束后,开发者会把用的全部配置文件的软件环境告诉运维,但是还是会出现各种环境问题,原则上讲程序开发结束,就应该产出可以执行的文件,可实际上并非如此。
1.2 Docker诞生了
2010年几个雄心壮志的小伙在旧金山成立了“dotcloud”一家Paas公司,随着公司的发展,将自己的容器技术进行标准化,取名“Docker”,Docker最开始并没有引起关注,很快dotcloud面临着倒闭的风险。
2013年3月,dotCloud公司创始人,Docker之父 solomon hykes将Docker开源,开源之后Docker的优点被越来越多的IT工程师发现,开源当月,发布Docker 0.1版本,很多开发者加入docker开源社区,贡献代码和意见,此后docker每月保持更新,到2014-6-9Docker 1.0版本发布。
伴随着微服务的爆红,Docker已经成为行业里人气最火爆的开源技术,没有之一,Google,MicorSoft,VMware都对Docker连连称赞
1.3 Docker设计理念
Docker的设计思想来源于集装箱,就像它的Logo一样,一直小海豚上面有很多集装箱,集装箱的作用就是将不同的货物按照不同的类别规整的摆放起来,各种货物被集装箱标准化了,不同集装箱之间相互隔离,互不影响。在没有集装箱之前,运水果使用一艘船,运军火使用一艘船,有了集装箱之后就可以把不同的获取装在不同的集装箱中一起运走。
在容器技术发现出现之前,“虚拟机”是业界的宠儿,与虚拟机相比,docker更“轻”,虚拟机需要在电脑中安装一个软件,比如Vmware,VirtualBox,然后在虚拟机中安装操作系统,在虚拟机的操作系统里面在部署应用软件。不同的虚拟机就好比不同的两台电脑,互相隔离;
容器技术也是一种虚拟化技术,容器技术不需要虚拟出”整台电脑“,虚拟机启动很慢,占用空间很大,耗费物理机资源高(GB级别),容器启动很快可以达到秒级甚至毫秒级,看镜像的大小,占用空间很小,耗费物理机资源是MB级别(甚至KB级别)。
综合来看,Docker深受IT工程师欢迎,正处于高速发展期
1.4 Docker技术理念
Docker是基于Go实现的开源项目,使用户的APP一次封装,到处运行(”Build,Ship and Run Any App , Anywhere“)
应用运行在Docker容器上面,Docker容器在任何操作系统都是一致的,实现了跨平台,跨服务器。
2 Docker安装
Docker安装相关的操作看这里
Docker安装教程
3 Docker架构
上图是Docker官网的一张图,这张图很好的诠释了Docker的架构设计
镜像(image)
镜像是一个模板,通过镜像可以生成容器,一个镜像可以生成多个容器,镜像可以类别成面向对象中的类,容器就是面向对象中的实例
容器(container)
容器是用镜像创造的一个或一组实例,它可以被创建、删除、启动、停止,每个容器之间相互隔离,互不干扰,容器可以比作一个简单的Linux系统,包括进程、线程、用户、权限和运行在里面的程序(代码)。容器的定义和镜像一样也是分层的,唯一的区别在于容器的最上面一层是可读写的。
仓库(repository)
- 存放镜像文件的仓库
- 仓库与仓库服务器(registry)不同,仓库服务器有多个仓库,不同仓库又有不同的镜像,不同的镜像又有不同的版本(Tag)
- 仓库分为公有仓库和私有仓库
- 最大的公有仓库是docker hub(https://hub.docker.com/),国内有阿里云镜像仓库,镜像仓库提供大量的镜像供下载
4 测试环境搭建
本文使用centos7做为测试环境
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
[root@localhost ~]# 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 hello world
[root@localhost ~]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
Status: Downloaded newer image for docker.io/hello-world:latest
[root@localhost ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
4.1 Docker run执行的流程
4.2 底层工作原理
Docker是C/S架构的系统,Docker守护进程进行在宿主机上,接收来自客户端的请求,守护进程接收到命令并管理运行在宿主机上面的容器。
Docker不需要Hypervisor实现硬件资源的虚拟化,docker使用的是实际物理机的硬件资源,使用宿主机的内核。
5 常用命令
5.1 帮助命令
[root@localhost ~]# docker version # 显示 Docker 版本信息
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-204.git0be3e21.el7.x86_64
Go version: go1.10.3
Git commit: 0be3e21/1.13.1
Built: Fri Mar 19 13:57:09 2021
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-204.git0be3e21.el7.x86_64
Go version: go1.10.3
Git commit: 0be3e21/1.13.1
Built: Fri Mar 19 13:57:09 2021
OS/Arch: linux/amd64
Experimental: false
[root@localhost ~]# docker info # 显示docker系统信息,包括镜像和容器数
[root@localhost ~]# docker --help # 帮助信息
5.2 镜像命令
5.2.1 列出所有镜像
# 列出本地主机上的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo2 0.1 ac90049663d1 7 days ago 896 MB
docker.io/python 3.9 cba42c28d9b8 13 days ago 886 MB
docker.io/mysql 5.7 8cf625070931 2 weeks ago 448 MB
docker.io/mysql <none> 2c9028880e58 2 months ago 447 MB
docker.io/hello-world latest d1165f221234 5 months ago 13.3 kB
- REPOSITORY 镜像仓库源
- TAG 镜像版本标签
- IMAGE ID 镜像ID
- CREATED 镜像创建时间
- SIZE 镜像大小
同一个仓库源有多个Tag,使用Rspository:Tag的方式区分,TAG不指定的话docker默认就是latest
[root@localhost ~]# docker images -a # 显示所有镜像信息
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 8466cc5001a7 7 days ago 896 MB
<none> <none> 67018efa6fa5 7 days ago 896 MB
<none> <none> 7945a1499c72 7 days ago 896 MB
demo2 0.1 ac90049663d1 7 days ago 896 MB
<none> <none> 1d37ea45b614 7 days ago 886 MB
<none> <none> 55be199548bf 7 days ago 886 MB
<none> <none> 78a3e02d4c49 7 days ago 886 MB
docker.io/python 3.9 cba42c28d9b8 13 days ago 886 MB
docker.io/mysql 5.7 8cf625070931 2 weeks ago 448 MB
docker.io/mysql <none> 2c9028880e58 2 months ago 447 MB
docker.io/hello-world latest d1165f221234 5 months ago 13.3 kB
[root@localhost ~]# docker images -q # 只显示镜像ID
ac90049663d1
cba42c28d9b8
8cf625070931
2c9028880e58
d1165f221234
[root@localhost ~]# docker images --digests # 显示摘要信息
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
demo2 0.1 <none> ac90049663d1 7 days ago 896 MB
docker.io/python 3.9 sha256:acb4e43d0c66c168e72ceaba5913cde472e4a17017cec9346969c9725a1fea94 cba42c28d9b8 13 days ago 886 MB
docker.io/mysql 5.7 sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596 8cf625070931 2 weeks ago 448 MB
docker.io/mysql <none> sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa 2c9028880e58 2 months ago 447 MB
docker.io/hello-world latest sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e d1165f221234 5 months ago 13.3 kB
5.2.2 搜索镜像
[root@localhost ~]# docker search mysql # 在docker hub中搜索mysql镜像
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 11243 [OK]
docker.io docker.io/mariadb MariaDB Server is a high performing open s... 4274 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 835 [OK]
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 88
docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 59 [OK]
docker.io docker.io/databack/mysql-backup Back up mysql databases to... anywhere! 45
docker.io docker.io/deitch/mysql-backup REPLACED! Please use http://hub.docker.com... 41 [OK]
docker.io docker.io/prom/mysqld-exporter 40 [OK]
docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 35
docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 30
docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 30 [OK]
docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 21
docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
docker.io docker.io/arey/mysql-client Run a MySQL client from a docker container 18 [OK]
docker.io docker.io/fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron t... 16 [OK]
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/cytopia/mysql-5.5 MySQL 5.5 on CentOS 7 4 [OK]
docker.io docker.io/devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offi... 3
docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
docker.io docker.io/jelastic/mysql An image of the MySQL database server main... 2
docker.io docker.io/centos/mysql-80-centos7 MySQL 8.0 SQL database server 1
docker.io docker.io/vitess/mysqlctld vitess/mysqlctld 1 [OK]
docker.io docker.io/widdpim/mysql-client Dockerized MySQL Client (5.7) including Cu... 1 [OK]
docker.io docker.io/monasca/mysql-init A minimal decoupled init container for mysql 0
[root@localhost ~]# docker search --filter=stars=10000 mysql # 在dockerhub中搜索stars数量大于等于10000的镜像
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 11243 [OK]
5.2.3 拉取镜像
[root@localhost ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ...
latest: Pulling from docker.io/library/redis 不写Tag,默认最新
33847f680f63: Already exists # 已经存在这个镜像文件不重复下载。文件分层
26a746039521: Pull complete
18d87da94363: Pull complete
5e118a708802: Pull complete
ecf0dbe7c357: Pull complete
46f280ba52da: Pull complete
Digest: sha256:cd0c68c5479f2db4b9e2c5fbfdb7a8acb77625322dd5b474578515422d3ddb59 # 签名
Status: Downloaded newer image for docker.io/redis:latest # 真实镜像位置
[root@localhost ~]# docker pull redis:5.0 # 指定版本下载
5.2.4 删除镜像
删除一个镜像
[root@localhost ~]# docker images -qa
67018efa6fa5
7945a1499c72
8466cc5001a7
ac90049663d1
1d37ea45b614
55be199548bf
78a3e02d4c49
cba42c28d9b8
aa4d65e670d6
8cf625070931
2c9028880e58
d1165f221234
[root@localhost ~]# docker rmi -f d1165f221234 # 后面是镜像ID
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
删除多个镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo2 0.1 ac90049663d1 7 days ago 896 MB
docker.io/python 3.9 cba42c28d9b8 13 days ago 886 MB
docker.io/redis latest aa4d65e670d6 2 weeks ago 105 MB
docker.io/mysql 5.7 8cf625070931 2 weeks ago 448 MB
docker.io/mysql <none> 2c9028880e58 2 months ago 447 MB
[root@localhost ~]# docker rmi -f docker.io/python:3.9 demo2:0.1
Untagged: docker.io/python:3.9
Untagged: docker.io/python@sha256:acb4e43d0c66c168e72ceaba5913cde472e4a17017cec9346969c9725a1fea94
Untagged: demo2:0.1
Deleted: sha256:ac90049663d13902c942c1535dfa39c6c30268c3daf41164c9c44b66e1c35e30
Deleted: sha256:8466cc5001a74817ead804a34571e6d25c54525d7698c265120b6a200e5092a4
Deleted: sha256:67018efa6fa59137f0a43f96ceed09107284e3ad27d848ff280270aa8a3f49b8
Deleted: sha256:7945a1499c72032e8120bac3c1515958da624c013c10f7ea06b5ddf4ae4261bf
Deleted: sha256:78a3e02d4c496be3c1aebee864316f909a49e6ccb18181af15e1f2e6d7ead0a0
Deleted: sha256:1d37ea45b6143f8b99f3f0ce9b6568c8ef3c47fe4585c503d116255da59e94b7
Deleted: sha256:55be199548bfea89bb4227bd87216f4971edf57afa39633489ecfff5f6c7c450
Deleted: sha256:cba42c28d9b81a80b75165cc9b61a71828c30be2b685c2d5aaa8ebe8d2ef1e8e
删除所有镜像
[root@localhost ~]# docker rmi -f $(docker images -qa)
Untagged: docker.io/mysql@sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Deleted: sha256:2c9028880e5814e8923c278d7e2059f9066d56608a21cd3f83a01e3337bacd68
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
5.3 容器命令
在测试一个技术前,确保环境是干净的,我们先删除所有存在的容器和镜像。
原则上先移除容器,后移除镜像
删除全部容器
[root@localhost ~]# docker ps -qa # 查看当前宿主机所有的容器的ID
8b2ffc2eec66
12fb775b136f
8895054a1dfd
ff92eafd5a6c
[root@localhost ~]# docker rm $(docker ps -qa) # 删除所有容器
8b2ffc2eec66
12fb775b136f
8895054a1dfd
ff92eafd5a6c
[root@localhost ~]# docker ps -a # 检查一下已经没有容器了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
删除全部镜像请看5.2.4小节,在这不重复
5.3.1 创建容器
拉取镜像到本地
[root@localhost ~]# docker pull centos # 拉取一个最新的centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for docker.io/centos:latest
[root@localhost ~]# docker images # 镜像下载成功
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 300e315adb2f 8 months ago 209 MB
创建容器命令
[root@localhost ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
-d, --detach Run container in background and print container ID 后台运行,命令行输出容器ID
-e, --env list Set environment variables (default []) 设置容器的环境变量 -e MYSQL_PASSWORD=test
-h, --hostname string Container host name 设置容器的hostname
-i, --interactive Keep STDIN open even if not attached 交互模式运行容器 与-t配合使用
-p, --publish list Publish a container's port(s) to the host (default []) -p 宿主机端口:容器端口
-P, --publish-all Publish all exposed ports to random ports 开放所有端口
-t, --tty Allocate a pseudo-TTY 分配一个新终端 与 -i配合使用
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-v, --volume list Bind mount a volume (default [])
-w, --workdir string Working directory inside the container
生成镜像
[root@localhost ~]# docker run -it --name centos1 centos
[root@b0b070b71f1a /]# ls # 已经进入到容器内部
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@b0b070b71f1a /]# exit # 退出容器
exit
5.3.2 列出所有容器
[root@localhost ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running) 显示所有容器,默认只显示正在运行的
-f, --filter filter Filter output based on conditions provided 根据条件过滤
-n, --last int Show n last created containers (includes all states) (default -1) 显示最近创建的n个容器,包括所有状态的容器
-l, --latest Show the latest created container (includes all states) 显示最新创建的容器
-q, --quiet Only display numeric IDs 只显示容器ID
-s, --size Display total file sizes 显示总文件大小
[root@localhost ~]# docker ps # 显示正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0b070b71f1a centos "/bin/bash" 8 minutes ago Up 7 minutes centos1
[root@localhost ~]# docker ps -a # 显示所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0b070b71f1a centos "/bin/bash" 8 minutes ago Up 7 minutes centos1
[root@localhost ~]# docker ps -q # 显示容器ID
b0b070b71f1a
[root@localhost ~]# docker ps -s # 显示容器的文件大小
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
b0b070b71f1a centos "/bin/bash" 8 minutes ago Up 7 minutes centos1 80 B (virtual 209 MB)
5.3.3 退出容器
exit # 容器停止退出
ctrl+P+Q # 不停止容器直接退出
5.3.4 启停操作容器
docker start/stop/restart/kill 容器名或者容器ID
[root@localhost ~]# docker ps # 显示正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0b070b71f1a centos "/bin/bash" 12 minutes ago Up 11 minutes centos1
[root@localhost ~]# docker stop centos1 # 停止容器
centos1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker start centos1 # 启动容器
centos1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0b070b71f1a centos "/bin/bash" 12 minutes ago Up 2 seconds centos1
[root@localhost ~]# docker restart centos1 # 重启容器
centos1
[root@localhost ~]# docker kill centos1
centos1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.3.5 删除容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0b070b71f1a centos "/bin/bash" 19 minutes ago Up 6 minutes centos1
[root@localhost ~]# docker rm b0b070b71f1a # 不使用-f参数不能删除掉正在运行的容器
Error response from daemon: You cannot remove a running container b0b070b71f1aeb3022b1b9237b7b9f16e9e5d991baa01e3917918005bbb1f962. Stop the container before attempting removal or use -f
[root@localhost ~]# docker rm -f b0b070b71f1a
b0b070b71f1a
[root@localhost ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL) 强制删除
-l, --link Remove the specified link 移除特殊链接
-v, --volumes Remove the volumes associated with the container 移除连接到当前容器的宿主机目录
5.3.6 查看容器的日志
[root@localhost ~]# 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 从一个时间戳往后取日志
--tail string Number of lines to show from the end of the logs (default "all") 显示多少条数据,最新的日志在文档的最下方
-t, --timestamps Show timestamps 显示时间戳
[root@localhost ~]# docker run -d centos echo "hello world"
ad225eb1f0bf3676ec5e121f9ce027442860f5408e7e4c6493be96faa3fe1241
[root@localhost ~]# docker logs -f -t ad225eb1f0bf
2021-08-10T02:34:48.970641000Z hello world
5.3.6 查看容器的进程和详细信息
[root@localhost ~]# docker top serene_elion
UID PID PPID C STIME TTY TIME CMD
root 2840 2827 0 10:44 pts/1 00:00:00 /bin/bash
[root@localhost ~]# docker inspect serene_elion
[
{
"Id": "e19c915f1462b843fbfde0e044566423666c8b3732c1aa0de78d75983495d06d", 完整ID
"Created": "2021-08-10T02:44:29.89866322Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2840,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-08-10T02:44:54.816965356Z",
"FinishedAt": "2021-08-10T02:44:33.37322001Z"
},
//。。。// 此处省略一大堆
]
5.3.7 进入容器内部
exec 在容器中打开新终端,可以启动新进程
attach 直接进入容器启动命名的终端,不启动新进程
[root@localhost ~]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background 后台运行(不推荐)
-e, --env list Set environment variables (default []) 设置环境变量
-i, --interactive Keep STDIN open even if not attached 交互式输入窗口
-t, --tty Allocate a pseudo-TTY 分配一个新窗口
-u, --user string Username or UID (format: <name|uid>[:<group|gid>]) 用户信息
[root@localhost ~]# docker exec -it e19c915f1462 /bin/bash
[root@e19c915f1462 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost ~]# docker attach e19c915f1462
[root@e19c915f1462 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
5.3.8 宿主机与容器文件互传
[root@localhost ~]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
--help Print usage
[root@localhost ~]# docker ps # 查看运行中的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e19c915f1462 centos "/bin/bash" 25 minutes ago Up 2 seconds serene_elion
[root@localhost ~]# docker exec -it e19c915f1462 /bin/bash # 进入镜像内部
[root@e19c915f1462 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@e19c915f1462 /]# mkdir -p test/floder1 # 递归创建文件夹
[root@e19c915f1462 /]# cd test/floder1/ # 切换到目录
[root@e19c915f1462 floder1]# touch hell.txt # 新建文件
[root@e19c915f1462 floder1]# echo "hello world" > hell.txt # 往文件中写入一些测试数据
[root@e19c915f1462 floder1]# exit # 退出容器
exit
[root@localhost ~]# docker cp e19c915f1462:/test/floder1/hell.txt /data 复制
[root@localhost ~]# cd /
[root@localhost /]# ls
bin boot data dev etc home lib lib64 media mnt opt proc rabbitmq-server-generic-unix-3.7.15.tar root run sbin srv sys testdir tmp usr var web_serve
[root@localhost /]# cd data # 切换到目录
[root@localhost data]# cat hell.txt # 查看文件
hello world
6 基础篇总结
上图基本上把Docker的基本命令都涵盖了,你知道这些命令就可以对Docker基本使用了,恭喜一下达成了“调包工程师”成就
attach Attach to a running container 链接指定镜像
build build an image from Dockerfile
commit create a new image from a container changes 提交当前容器为一个新镜像
cp 拷贝文件到宿主机
create Create a new container 同run 但是不启动
diff 查看容器的变化情况
events Get real time events from the server
exec Run a command in an existing container 进入一个正在进行容器的内部
export 导出容器为一个tar文件 与import对应
history show the history of an image
images List images
import 从tar包创建一个新镜像
info 显示系统相关信息
inspect 显示容器详细信息
kill 强制停止容器
load load an image from a tar achive
login Register or login to the docker registry server
logout 从当前Docker registry退出
logs Fetch the logs of container
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT #查看映射端口对应的容器内部源端口
pause Pause all processes within a container # 暂停容器
ps List containers # 列出运行容器列表
pull Pull an image or a repository from the docker registry server
push Push an image or a repository to the docker registry server #
restart Restart a running container # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container # 创建一个新的容器并运行一个命令
save Save an image to a tar archive # 保存一个镜像为一个tar 包[对应 load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索镜像
start Start a stopped containers # 启动容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 给源中镜像打标签
top Lookup the running processes of a container # 查看容器中运行的进程信息
unpause Unpause a paused container # 取消暂停容器
version Show the docker version information # 查看 docker 版本号
wait Block until a container stops, then print its exit code # 截取容器停止时的退出状态值