版本比较
标识
- 该行被添加。
- 该行被删除。
- 格式已经改变。
Docker命令行介绍
代码块 | ||
---|---|---|
| ||
docker + 管理的对象(比如容器,镜像) + 具体操作(比如创建,启动,停止,删除) |
示例:
docker image pull nginx
拉取一个叫nginx的docker image镜像。docker container stop web
停止一个叫web的docker container容器。
基础命令:
docker version
查看版本信息和client/server信息。展开 代码块 ~# docker version Client: Docker Engine - Community Cloud integration: v1.0.23 Version: 20.10.14 API version: 1.41 Go version: go1.16.15 Git commit: a224086 Built: Thu Mar 24 01:48:21 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Desktop Engine: Version: 20.10.14 API version: 1.41 (minimum version 1.12) Go version: go1.16.15 Git commit: 87a90dc Built: Thu Mar 24 01:46:14 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.5.11 GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc: Version: 1.0.3 GitCommit: v1.0.3-0-gf46b6ba docker-init: Version: 0.19.0 GitCommit: de40ad0
docker info
查看本机容器和镜像相关的信息,比如有多少容器和镜像,正在运行的有多少,停止的有多少等。展开 代码块 ~# docker info Client: Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc., v0.8.2) compose: Docker Compose (Docker Inc., v2.4.1) sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 5 Running: 0 Paused: 0 Stopped: 5 Images: 2 Server Version: 20.10.14 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux Default Runtime: runc Init Binary: docker-init containerd version: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc version: v1.0.3-0-gf46b6ba init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.19.128-microsoft-standard Operating System: Docker Desktop OSType: linux Architecture: x86_64 CPUs: 8 Total Memory: 12.4GiB Name: docker-desktop ID: 4JD3:ZGZ6:AQSF:6QEF:PYNP:D7I5:RIW7:IO5G:HUGE:ZLRU:6SXH:QSX6 Docker Root Dir: /var/lib/docker Debug Mode: false HTTP Proxy: http.docker.internal:3128 HTTPS Proxy: http.docker.internal:3128 No Proxy: hubproxy.docker.internal Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: hubproxy.docker.internal:5000 127.0.0.0/8 Live Restore Enabled: false WARNING: No blkio throttle.read_bps_device support WARNING: No blkio throttle.write_bps_device support WARNING: No blkio throttle.read_iops_device support WARNING: No blkio throttle.write_iops_device support WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled
镜像 vs 容器
image镜像
- Docker image是一个
read-only
文件。 - 这个文件包含文件系统,源码,库文件,依赖,工具等一些运行application所需要的文件。
- 可以理解成一个模板。
- Docker image具有分层的概念。
container容器
- 一个运行中的Docker image。
- 实质是复制image并在image最上层加上一层
read-write
的层 (称之为container layer
, 容器层)。 - 基于同一个image可以创建多个container。
Docker image的获取
- 自己制作(借助Dockerfile,参考 4. Dockerfile完全指南)
- 从registry拉取(比如Docker Hub)
容器的基本操作
常用命令
操作 | 命令(全) | 命令(简) |
---|---|---|
容器的创建 | docker container run <image name> | docker run <image name> |
容器的列出(up) | docker container ls | docker ps |
容器的列出(up和exit) | docker container ls -a | docker ps -a |
容器的停止 | docker container stop <name or ID> | docker stop <container name or ID> |
容器的删除 | docker container rm <name or ID> | docker rm <container name or ID> |
提示 |
---|
docker container rm不能删除一个正在运行的容器,如果要强制删除,可以使用 |
示例:
代码块 |
---|
$ docker container run nginx $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 343fd4031609 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp xenodochial_clarke $ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 343fd4031609 nginx "/docker-entrypoint.…" 14 seconds ago Up 13 seconds 80/tcp xenodochial_clarke $ docker container stop 34 34 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 343fd4031609 nginx "/docker-entrypoint.…" 29 seconds ago Exited (0) 5 seconds ago xenodochial_clarke d9095daa8bcf nginx "/docker-entrypoint.…" 28 minutes ago Exited (0) 28 minutes ago suspicious_shamir $ docker container rm 34 34 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9095daa8bcf nginx "/docker-entrypoint.…" 28 minutes ago Exited (0) 28 minutes ago suspicious_shamir $ |
命令小技巧
批量停止命令小技巧:批量停止&删除容器
- 批量停止:
docker container stop $(docker container ps -q)
。 - 批量删除:
docker container rm $(docker container ps -aq)
。
示例:
代码块 |
---|
$ docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd3a825fedeb nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 80/tcp mystifying_leakey 269494fe89fa nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 80/tcp funny_gauss 34b68af9deef nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 80/tcp interesting_mahavira 7513949674fc nginx "/docker-entrypoint.…" 13 seconds ago Up 12 seconds 80/tcp kind_nobel |
方法1方法1:
代码块 |
---|
$ docker container rm cd3 269 34b 751 |
方法2方法2:
代码块 |
---|
$ docker container stop $(docker container ps -q) cd3a825fedeb 269494fe89fa 34b68af9deef 7513949674fc $ docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd3a825fedeb nginx "/docker-entrypoint.…" 30 seconds ago Exited (0) 2 seconds ago mystifying_leakey 269494fe89fa nginx "/docker-entrypoint.…" 32 seconds ago Exited (0) 2 seconds ago funny_gauss 34b68af9deef nginx "/docker-entrypoint.…" 35 seconds ago Exited (0) 2 seconds ago interesting_mahavira 7513949674fc nginx "/docker-entrypoint.…" 36 seconds ago Exited (0) 2 seconds ago kind_nobel $ |
提示 |
---|
也可以使用 |
批量删除
和批量停止类似,可以使用 docker container rm $(docker container ps -aq)
。
提示 |
---|
|
docker命令帮助文档
提示 |
---|
|
命令小技巧:用完即删
使用docker contianer run --rm
,容器运行完之后即自动删除。
docker命令帮助文档
提示 |
---|
↓
↓
|
首先是docker --help
,用于查看全部的帮忙选项,输出如下:
展开 | ||
---|---|---|
|
接下来是针对某个命令的帮助文档,比如查看容器命令相关的文档,可以使用docker container --help
,如下:
展开 | ||
---|---|---|
|
还可以继续查看下一级命令的帮助文档,比如使用docker container run --help
查看运行容器的相关选项,如下:
展开 | ||
---|---|---|
|
容器运行模式
- attach模式:前台模式,交互式模式,占用当前命令行,可以在命令行看到容器的输入输出,Ctrl-C退出时会停止容器。
- detach模式:后台模式,容器在后台运行,不占用当前命令行。
测试用例:
代码块 |
---|
docker container run -p 80:80 nginx docker containerWorking rundirectory inside the container |
容器运行模式
//TODO attach模式和detach模式。-p 80:80 -d nginx |
提示 |
---|
|
连接容器的 shell
docker container run -it
创建一个容器并进入交互式模式。
代码块 |
---|
➜ ~ docker container run -it busybox sh / # / # / # ls bin dev etc home proc root sys tmp usr var / # ps PID USER TIME COMMAND 1 root 0:00 sh 8 root 0:00 ps / # exit |
docker container exec -it
在一个已经运行的容器里执行一个额外的command。
代码块 |
---|
➜ ~ docker container run -d nginx 33d2ee50cfc46b5ee0b290f6ad75d724551be50217f691e68d15722328f11ef6 ➜ ~ ➜ ~ docker container exec -it 33d sh # # # ls bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var # # exit ➜ ~ |
容器 vs 虚拟机
容器不是Mini虚拟机
- 容器其实是进程(Containers are just processes)。
- 容器中的进程被限制了对CPU内存等资源的访问。
- 当进程停止后,容器就退出了。
容器的进程process
提示 |
---|
以下是在Ubuntu20.04中演示,因Windows环境下的Docker和Linux具有一些差异。 |
代码块 |
---|
➜ ~ docker container run -d nginx 57fe4033dd7e1e620a0b6a7b83b85d4f8f98772f0ce585624c384de254826fd0 ➜ ~ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 57fe4033dd7e nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 80/tcp festive_proskuriakova ➜ ~ docker container top 57f UID PID PPID C STIME TTY TIME CMD root 7646 7625 0 12:14 ? 00:00:00 nginx: master process nginx -g daemon off; systemd+ 7718 7646 0 12:14 ? 00:00:00 nginx: worker process ➜ ~ ➜ ~ ➜ ~ ps -aef --forest UID PID PPID C STIME TTY TIME CMD root 1 0 0 May14 ? 00:00:00 /init root 157 1 0 May14 ? 00:00:00 /init root 523 157 0 May14 ? 00:02:32 \_ /usr/bin/dockerd -p /var/run/docker.pid root 545 523 0 May14 ? 00:25:55 | \_ containerd --config /var/run/docker/containerd/containerd.toml --log-level info root 7625 157 0 12:14 ? 00:00:00 \_ /usr/bin/containerd-shim-runc-v2 -namespace moby -id 57fe4033dd7e1e620a0b6a7b83b85d4f8f98772f0ce585624c384de254826fd0 -address /var/run/d root 7646 7625 0 12:14 ? 00:00:00 \_ nginx: master process nginx -g daemon off; systemd+ 7718 7646 0 12:14 ? 00:00:00 \_ nginx: worker process root 6442 1 0 May18 ? 00:00:00 /init root 6443 6442 0 May18 ? 00:00:00 \_ /init penxiao 6444 6443 0 May18 pts/2 00:00:01 \_ -zsh penxiao 7770 6444 0 12:15 pts/2 00:00:00 \_ ps -aef --forest ➜ ~ ➜ ~ pstree -halps 7718 init,1 └─init,157 └─containerd-shim,7625 -namespace moby -id 57fe4033dd7e1e620a0b6a7b83b85d4f8f98772f0ce585624c384de254826fd0 -address /var/run/docker/containerd/containerd.sock └─nginx,7646 └─nginx,7718 |
Gain Access to the MobyLinux VM on Windows or MacOS
https://nickjanetakis.com/blog/docker-tip-70-gain-access-to-the-mobylinux-vm-on-windows-or-macos提示 |
---|
容器其实是进程,也就是说,通过容器启动nginx,实际上nginx进程就是位于宿主机上,基于可以在宿主机上用ps命令查到,但是,这个nginx进程的进程号有些要注意,对于宿主机来说,它有一个进程号,但是在容器里,这个进程其实是另一个进程号。这可以通过docker container run -it busybox sh来验证,进到busybox容器里执行sh,再执行ps,可以看到当前sh的进程号,换到另一个终端,通过docker container ps可以查到另一个进程号,这两个是不一样的。 |
docker container run 背后发生了什么?
$ docker container run -d --publish 80:80 --name webhost nginx
- 在本地查找是否有nginx这个image镜像,但是没有发现
- 去远程的image registry查找nginx镜像(默认的registry是Docker Hub)
- 下载最新版本的nginx镜像 (nginx:latest 默认)
- 基于nginx镜像来创建一个新的容器,并且准备运行
- docker engine分配给这个容器一个虚拟IP地址
- 在宿主机上打开80端口并把容器的80端口转发到宿主机上
- 启动容器,运行指定的命令(这里是一个shell脚本去启动nginx)
目录 |
---|