Docker Run的流程和Docker原理
run的运行流程图
底层原理
Docker是怎么工作的?
Docker是一个Client-Server结构的系统,类似于redis,Docker的守护进程运行在主机上,通过Socker从客户端访问!DockerServer接收到Docker-Client的指令,就会执行这个命令!
Docker为什么比VM快?
1、 Docker有着比虚拟机更少的抽象层。
2、 docker利用的是宿主机的内核,vm需要模拟客户操作系统Guest OS。
新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS,分钟级别的,而docker是利用宿主机的操作系统,省略了这个复杂的过程
学完所有命令后回过头来会理解的更加透彻
docker的常用命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助命令
帮助文档的地址:
镜像命令
docker images #查看所有本地的主机上的镜像
[root@iz2ze4ak3swm586uk17dv3z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 6 weeks ago 13.3kB
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的id
docker search 搜索镜像
[root@iz2ze4ak3swm586uk17dv3z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10777 [OK]
# 查询STARS大于3000的
[root@iz2ze4ak3swm586uk17dv3z ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10777 [OK]
mariadb MariaDB Server is a high performing open sou… 4058 [OK]
# 下载镜像 docker pull 镜像名[:tag]
[root@iz2ze4ak3swm586uk17dv3z ~]# docker pull mysql
Using default tag: latest # 如果不写 tag. 默认就是
latest: Pulling from library/mysql
f7ec5a41d630: Pull complete # 分层下载,docker image的核心 联合文件系统
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: Pull complete
8a2090759d8a: Pull complete
15f235e0d7ee: Pull complete
d870539cd9db: Pull complete
493aaa84617a: Pull complete
bfc0e534fc78: Pull complete
fae20d253f9d: Pull complete
9350664305b3: Pull complete
e47da95a5aab: Pull complete
Digest: sha256:04ee7141256e83797ea4a84a4d31b1f1bc10111c8d1bc1879d52729ccd19e20a
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@iz2ze4ak3swm586uk17dv3z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
f7ec5a41d630: Already exists
9444bb562699: Already exists
6a4207b96940: Already exists
181cefd361ce: Already exists
8a2090759d8a: Already exists
15f235e0d7ee: Already exists
d870539cd9db: Already exists
cb7af63cbefa: Pull complete
151f1721bdbf: Pull complete
fcd19c3dd488: Pull complete
415af2aa5ddc: Pull complete
Digest: sha256:a655529fdfcbaf0ef28984d68a3e21778e061c886ff458b677391924f62fb457
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
# 删除镜像
[root@iz2ze4ak3swm586uk17dv3z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 87eca374c0ed 3 days ago 447MB
mysql latest 0627ec6901db 3 days ago 556MB
hello-world latest d1165f221234 6 weeks ago 13.3kB
[root@iz2ze4ak3swm586uk17dv3z ~]# docker rmi -f 87eca374c0ed
Untagged: mysql:5.7
Untagged: mysql@sha256:a655529fdfcbaf0ef28984d68a3e21778e061c886ff458b677391924f62fb457
Deleted: sha256:87eca374c0ed97f0f0b504174b0d22b0a0add454414c0dbf5ae43870369f6854
Deleted: sha256:3b035442a2f8d52d6c5c2d83a18c6c21a89b4dc6c89b481bcf40df89087655ce
Deleted: sha256:a223f1762b2c619a59b81fc2304bf4c9b791c777c8bdb19760c09cbd1f061efc
Deleted: sha256:92402939b3fd03bee3745eb90df9799bcb7d0ef92ca8ecf7ef37ad9c8a550084
Deleted: sha256:028b21e33aa4cd9c88acdd194d5cbef25638ffbca8669bfc0da72ad1eb148997
# 删除所有镜像
[root@iz2ze4ak3swm586uk17dv3z ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:04ee7141256e83797ea4a84a4d31b1f1bc10111c8d1bc1879d52729ccd19e20a
Deleted: sha256:0627ec6901db4b2aed6ca7ab35e43e19838ba079fffe8fe1be66b6feaad694de
Deleted: sha256:94d5db550d62032ddc8ad8d4cfef8bac06fe7d35757deb2f8638d0fdc1e89217
Deleted: sha256:5fbbb0e9bfb8f2a4d665cc60aaf4876191c057898db276d2d030a0d6123afc8e
Deleted: sha256:124a9d262ebecea284b6438b0a04766b076bd3f89cb0f29a8a475f26bf93911f
Deleted: sha256:5aa37b6c8e31197ab2f357c09755bcb83228ff0fb69c2009a12b6eddb087e884
Deleted: sha256:d0b1d5665c308823bbc5b8986d405e5202ade6994dfa00d8ff576eff372dd045
Deleted: sha256:cd35e2328f0670969657f1abae8beffbc1eb1fddbaf667e1e6e6286598500a35
Deleted: sha256:068b92efc0504adcd3c23f16fde80775a2f4dfe485e242206f638eae72c4fa1b
Deleted: sha256:7c8818a166d9666c68fcdbe421c30568d60d51a505e540f42901664113047a75
Deleted: sha256:5aa8f65565168fd7db2aa6b9f8fb1db746aa598fa3854dcbdbb49d5a29f6d8a5
Deleted: sha256:cca9d1bafa1ee67bb4d7178732c0955a40a5dea6e5b989f61248984f26f7306b
Deleted: sha256:34ca91e79c4027120ca740231d415c739cccad57d1ee68d6a6e67ca60bbaf3a4
Deleted: sha256:7e718b9c0c8c2e6420fe9c4d1d551088e314fe923dce4b2caf75891d82fb227d
Untagged: hello-world:latest
Untagged: hello-world@sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
[root@iz2ze4ak3swm586uk17dv3z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
容器命令
我们有了镜像才可以创建容器,下面我们来下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
测试,启动并进入容器
[root@iz2ze4ak3swm586uk17dv3z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 4 months ago 209MB
[root@iz2ze4ak3swm586uk17dv3z ~]# docker run -it centos /bin/bash
[root@073a5567a04b /]# ls #查看容器内的centos,基础版本
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@073a5567a04b /]# exit
exit
# 从容器中退回主机
[root@iz2ze4ak3swm586uk17dv3z /]# ls
bin data etc lib lost+found mnt proc run srv tmp var
boot dev home lib64 media opt root sbin sys usr
列出所有的运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+历史运行过的容器
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
073a5567a04b centos "/bin/bash" 7 minutes ago Exited (127) 2 minutes ago hopeful_merkle
4baa586b4e9c d1165f221234 "/hello" 43 hours ago Exited (0) 43 hours ago vigorous_pasteur
[root@iz2ze4ak3swm586uk17dv3z /]#
退出容器
exit #直接容器停止并退出
Ctrl + P + Q #容器不停止退出
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iz2ze4ak3swm586uk17dv3z /]# clear
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iz2ze4ak3swm586uk17dv3z /]# docker run -it centos /bin/bash
[root@7878e12e7a45 /]# [root@iz2ze4ak3swm586uk17dv3z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7878e12e7a45 centos "/bin/bash" 10 seconds ago Up 10 seconds musing_tharp
删除容器
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q|xargs docker rm # 删除所有的容器
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7878e12e7a45 centos "/bin/bash" 20 minutes ago Up 20 minutes musing_tharp
073a5567a04b centos "/bin/bash" 41 minutes ago Exited (127) 36 minutes ago hopeful_merkle
4baa586b4e9c d1165f221234 "/hello" 44 hours ago Exited (0) 44 hours ago vigorous_pasteur
[root@iz2ze4ak3swm586uk17dv3z /]# docker rm 073a5567a04b
073a5567a04b
[root@iz2ze4ak3swm586uk17dv3z /]# docker rm 7878e12e7a45
Error response from daemon: You cannot remove a running 7878e12e7a45aa4b4fc7c82d6fbffebf6c40a04d76b296f59caa2fddce770bd7. Stop the before attempting removal or force remove
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps -aq
7878e12e7a45
4baa586b4e9c
[root@iz2ze4ak3swm586uk17dv3z /]# docker rm -f $(docker ps -aq)
7878e12e7a45
4baa586b4e9c
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps -aq
启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器
Docker常用的其他命令
后台启动容器
# 命令 docker run -d 镜像名!
[root@iz2ze4ak3swm586uk17dv3z /]# docker run -d centos
12b6b41f13c8be84b70f7ab7850af982da09e3bdfcf0c2b8c005be42fd8ac096
# 问题docker ps,发现 centos 停止了
# 常见的问题,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t --tail 容器
docker ps
# 显示日志
-tf # 显示日志
--tail number # 要显示日志条数
[root@iz2ze4ak3swm586uk17dv3z /]# docker logs -tf --tail 10 b05887b91ff2
查看容器中进程信息ps
# 命令docker top 容器id
# docker top dce45a67f1ffbh
查看镜像的元数据
# 命令
docker inspect 容器id
[root@iz2ze4ak3swm586uk17dv3z /]# docker inspect aa416ff29186
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bashShell
# 测试
[root@iz2ze4ak3swm586uk17dv3z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
805fa99a8af0 centos "/bin/bash" 2 minutes ago Up 17 seconds magical_buck
[root@iz2ze4ak3swm586uk17dv3z /]# docker exec -it 805fa99a8af0 /bin/bash
[root@805fa99a8af0 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
# 方式二
docker attach 容器id
# 测试
[root@iz2ze4ak3swm586uk17dv3z /]# docker attach 805fa99a8af0
[root@805fa99a8af0 /]#
# docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach #进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的的主机路径
# 拷贝是一个手动过程,未来我们使用 -v卷的技术,可以实现
docker官方给的总结