200字
Docker的基础命令和存储引擎介绍
2026-03-07
2026-03-07

btw:Docker 安装和删除已经提前写好了 点击链接即可跳转、

docker的安装和删除 - Ayaka's Blog

Docker的基础命令介绍

Docker 程序环境

Dockerclient-->DockerEnginedockerd-->Containerd-->containerd-Shim-->runC-->container

环境配置文件:

/etc/sysconfig/docker-network
/etc/sysconfig/docker-storage
/etc/sysconfig/docker

Unit File:

/usr/lib/systemd/system/docker.service

docker-ce 配置文件:

/etc/docker/daemon.json

Docker Registry配置文件:

/etc/containers/registries.conf

范例: ubuntu 查看docker相关文件

#服务器端相关文件
[root@ubuntu1804 ~]#dpkg -L docker-ce
/.
/etc
/etc/default
/etc/default/docker
/etc/init
/etc/init/docker.conf
/etc/init.d
/etc/init.d/docker
/lib
/lib/systemd
/lib/systemd/system
/lib/systemd/system/docker.service
/lib/systemd/system/docker.socket
/usr
/usr/bin
/usr/bin/docker-init
/usr/bin/docker-proxy
/usr/bin/dockerd
/usr/share
/usr/share/doc
/usr/share/doc/docker-ce
/usr/share/doc/docker-ce/README.md
/usr/share/doc/docker-ce/changelog.Debian.gz
/var
/var/lib
/var/lib/docker-engine
/var/lib/docker-engine/distribution_based_engine.json

#客户端相关文件
[root@ubuntu1804 ~]#dpkg -L docker-ce-cli
/.
/usr
/usr/bin
/usr/bin/docker
/usr/libexec
/usr/libexec/docker
/usr/libexec/docker/cli-plugins
/usr/libexec/docker/cli-plugins/docker-app
/usr/libexec/docker/cli-plugins/docker-buildx
/usr/share
.........

范例: CentOS7 查看docker相关文件

[root@centos7 ~]#rpm -ql docker-ce
/usr/bin/docker-init
/usr/bin/docker-proxy
/usr/bin/dockerd
/usr/lib/systemd/system/docker.service
/usr/lib/systemd/system/docker.socket
[root@centos7 ~]#rpm -ql docker-ce-cli
/usr/bin/docker
/usr/libexec/docker/cli-plugins/docker-app
/usr/libexec/docker/cli-plugins/docker-buildx
........

Docker 命令帮助

docker 命令是最常使用的 docker 客户端命令,其后面可以加不同的参数以实现不同的功能

docker 命令格式

docker [OPTIONS] COMMAND

COMMAND分为
Management Commands  #指定管理的资源对象类型,较新的命令用法,将命令按资源类型进行分类,方便使用
Commands             #对不同资源操作的命令不分类,使用容易产生混乱

docker 命令有很多子命令,可以用下面方法查看帮助

#docker 命令帮助
man docker 
docker
docker  --help

#docker  子命令帮助
man docker-COMMAND
docker COMMAND --help

官方文档:

https://docs.docker.com/reference/
https://docs.docker.com/engine/reference/commandline/cli/

范例: 查看docker命令帮助

[root@ubuntu1804 ~]#docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:--config string      Location of client config files (default 
"/root/.docker")-c, --context string     Name of the context to use to connect to the daemon 
(overrides DOCKER_HOST env var and default
                           context 
set with "docker context use")-D, --debug
              Enable debug mode-H, --host list          Daemon socket(s) to connect to-l, --log-level string   Set the logging level 
("debug"|"info"|"warn"|"error"|"fatal") (default "info")--tls
                Use TLS; implied by --tlsverify--tlscacert string   Trust certs signed only by this CA (default 
"/root/.docker/ca.pem")--tlscert string     Path to TLS certificate file (default 
"/root/.docker/cert.pem")--tlskey string      Path to TLS key file (default 
"/root/.docker/key.pem")--tlsverify
          Use TLS and verify the remote-v, --version
            Print version information and quit
            
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
node
        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
service
     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes
Commands:
  attach      Attach local standard input, output, and error streams to a running 
container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
cp
          Copy files/folders between a container and the local filesystem
  create      Create a new container
diff
        Inspect changes to files or directories on a container's 
filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
export
      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
kill
        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
ps
          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
restart
     Restart one or more containers
rm
          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by 
default)
  search      Search the Docker Hub for images
start
       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
stop
        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top
         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit 
codes
Run 'docker COMMAND --help' for more information on a command.

Docker 相关信息和优化配置

查看 docker 版本

[root@ubuntu1804 ~]#docker version
Client: Docker Engine - Community
 Version:           
19.03.5
 API version:       
1.40
 Go version:        go1.12.12
 Git commit:        633a0ea838
 Built:             Wed Nov 13 07:29:52 2019
 OS/Arch:           linux/amd64
 Experimental:      
false
Server: Docker Engine - Community
 Engine:
  Version:          
19.03.5
  API version:      
1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea838
  Built:            Wed Nov 13 07:28:22 2019
  OS/Arch:          linux/amd64
  Experimental:     
false
 containerd:
  Version:          
1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          
1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          
0.18.0
  GitCommit:        fec3683

查看 docker 详解信息

[root@ubuntu1804 ~]#docker info
Client:
 Debug Mode: false     
#client 端是否开启 debug
Server:
 Containers: 2   
  Running: 0     
  Paused: 0      
  Stopped: 2     
 Images: 4       
#当前主机运行的容器总数
#有几个容器是正在运行的
#有几个容器是暂停的
#有几个容器是停止的
#当前服务器的镜像数
 Server Version: 19.03.5   
#服务端版本
 Storage Driver: overlay2  #正在使用的存储引擎
  Backing Filesystem: extfs   
#后端文件系统,即服务器的磁盘文件系统
  Supports d_type: true  #是否支持 d_type
 Native Overlay Diff: true  #是否支持差异数据存储
 Logging Driver: json-file #日志类型,每个容器的标准输出以日志存放
在/var/lib/docker/containers/<CONTAINER ID>/<CONTAINER ID>-json.log 
 Cgroup Driver: cgroupfs  #Cgroups 类型
 Plugins:                  
#插件
  Volume: local            
#卷 
  Network: bridge host ipvlan macvlan null overlay # overlay 跨主机通信
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk 
syslog  # 日志类型
 Swarm: inactive    
#是否支持 swarm
 Runtimes: runc     
#已安装的容器运行时
 Default Runtime: runc   
#默认使用的容器运行时
 Init Binary: docker-init   
#初始化容器的守护进程,即 pid 为 1 的进程
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 #版本
 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657  #runc 版本
 init version: fec3683  #init 版本
 Security Options:   
#安全选项
  apparmor     
  seccomp  
#安全模块,https://docs.docker.com/engine/security/apparmor/
#安全计算模块,即制容器操作,
https://docs.docker.com/engine/security/seccomp/
   Profile: default  #默认的配置文件
 Kernel Version: 4.15.0-29-generic  #宿主机内核版本
 Operating System: Ubuntu 18.04.1 LTS  #宿主机操作系统
 OSType: linux    
#宿主机操作系统类型
 Architecture: x86_64   
#宿主机架构
 CPUs: 1    
#宿主机 CPU 数量
 Total Memory: 962MiB   
#宿主机总内存 
 Name: ubuntu1804.wang.org #宿主机 hostname
 ID: IZHJ:WPIN:BRMC:XQUI:VVVR:UVGK:NZBM:YQXT:JDWB:33RS:45V7:SQWJ #宿主机 ID
 Docker Root Dir: /var/lib/docker   
#宿主机关于docker数据的保存目录,建议使用独立SSD的磁
盘,保证性能和空间
 Debug Mode: false   
#server 端是否开启 debug
 Registry: https://index.docker.io/v1/  #仓库路径
 Labels:
 Experimental: false  #是否测试版
 Insecure Registries:
127.0.0.0/8  : #非安全的镜像仓库
 Registry Mirrors:
  https://si7y70hh.mirror.aliyuncs.com/   
#镜像仓库
 Live Restore Enabled: false  #是否开启活动重启 (重启docker-daemon 不关闭容器 )
WARNING: No swap limit support  #系统警告信息 (没有开启 swap 资源限制 )

范例: 解决上述SWAP报警提示

官方文档: 安装后步骤 |Docker 文档

[root@ubuntu1804 ~]#docker info
......
WARNING: No swap limit support 

[root@ubuntu1804 ~]# vim /etc/default/grub
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_ release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 swapaccount=1"  #修改此行

[root@ubuntu1804 ~]# update-grub
[root@ubuntu1804 ~]# reboot

查看 docker0 网卡

在docker安装启动之后,默认会生成一个名称为docker0的网卡并且默认IP地址为172.17.0.1的网卡

#ubuntu18.04安装docker后网卡配置
[root@ubuntu1804 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group 
default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP 
group default qlen 1000
    link/ether 00:0c:29:34:df:91 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe34:df91/64 scope link 
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
group default 
    link/ether 02:42:d3:26:ed:4e brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:d3ff:fe26:ed4e/64 scope link 
       valid_lft forever preferred_lft forever
       
       
#CentOS 7.6 安装docker后网卡配置     
[root@centos7 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group 
default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP 
group default qlen 1000
    link/ether 00:0c:29:ca:00:e4 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feca:e4/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state 
DOWN group default 
    link/ether 02:42:d2:81:c2:e0 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
       
#CentOS 8.1 安装docker后网卡配置
[root@centos8 ~]#ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group 
default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP 
group default qlen 1000
    link/ether 00:0c:29:4d:ef:3e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.18/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe4d:ef3e/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state 
DOWN group default 
    link/ether 02:42:f5:3e:65:b6 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
       
[root@centos8 ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.2        0.0.0.0         UG    100    0        0 eth0
10.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0

docker 存储引擎

官方文档关于存储引擎的相关文档:

https://docs.docker.com/storage/storagedriver/

https://docs.docker.com/storage/storagedriver/select-storage-driver/

  • AUFS: (Advanced Mult-Layered Unification Filesystem,版本2之前旧称AnotherUnionFS)是 一种 Union FS ,是文件级的存储驱动。Aufs是之前的UnionFS的重新实现,2006年由Junjiro Okajima开发
    • 所谓 UnionFS就是把不同物理位置的目录合并 mount 到同一个目录中。简单来说就是支持将不同 目录挂载到一个虚拟文件系统下的。这种可以层层地叠加修改文件。无论底下有多少都是只读的, 最上系统可写的。当需要修改一个文件时, AUFS 创建该文件的一个副本,使用 CoW 将文件从只 读层复制到可写进行修改,结果也保存在Docker 中,底下的只读层就是 image,可写层就是 Container
    • aufs 被拒绝合并到主线 Linux 。其代码被批评为"dense, unreadable, uncommented 密集、不可 读、未注释"。 相反,OverlayFS被合并到 Linux 内核中。在多次尝试将 aufs 合并到主线内核失败 后,作者放弃了
    • AUFS 是 Docker 18.06 及更早版本的首选存储驱动程序,在内核 3.13 上运行 Ubuntu 14.04 时不支持overlay2
  • Overlay: 一种 Union FS 文件系统, Linux 内核 3.18 后支持
  • Overlay2: Overlay 的升级版,到目前为止,所有 Linux 发行版推荐使用的存储类 型,也是docker 默认使用的存储引擎为overlay2,需要磁盘分区支持d-type功能,因此需要系统磁盘的额外支持,相 对AUFS来说Overlay2 有以下优势: 更简单地设计; 从3.18开始就进入了Linux内核主线;资源消 耗更少
  • devicemapper: 因为CentOS 7.2和RHEL 7.2 的之前版本内核版本不支持 overlay2,默认使用的存 储驱动程序,最大数据容量只支持100GB且性能不佳,当前较新版本的CentOS 已经支持 overlay2, 因此推荐使用 overlay2,另外此存储引擎已在Docker Engine 18.09中弃用
  • ZFS(Sun -2005)/btrfs(Oracle-2007): 目前没有广泛使用
  • vfs: 用于测试环境,适用于无法使用 copy-on -writewrite 时的情况。 此存储驱动程序的性能很 差,通常不建议用于生产

修改存储引擎

参考文档: https://docs.docker.com/storage/storagedriver/overlayfs-driver/

范例: 在CentOS7.2修改存储引擎

[root@centos7 ~]#vim /lib/systemd/system/docker.service
.....
ExecStart=/usr/bin/dockerd -s overlay2 -H fd:// -
containerd=/run/containerd/containerd.sock
......

#创建新的xfs分区,添加ftype特性,否则默认无法启动docker服务
[root@centos7 ~]#mkfs.xfs  -n ftype=1 /dev/sdb
[root@centos7 ~]#mount /dev/sdb /var/lib/docker

[root@centos7 ~]#systemctl daemon-reload
[root@centos7 ~]#systemctl restart docker

注意:修改存储引擎会导致所有容器丢失,所以先备份再修改

#查看Ubuntu1804的默认存储引擎
[root@ubuntu1804 ~]#docker info |grep Storage
WARNING: No swap limit support
 Storage Driver: overlay2
 
#查看CentOS7.6的默认存储引擎
[root@centos7 ~]#docker info |grep Storage
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Storage Driver: overlay

Docker官方推荐首选存储引擎为overlay2,其次为devicemapper,但是devicemapper存在使用空间方 面的一些限制,虽然可以通过后期配置解决,但是官方依然推荐使用overlay2

范例: aufs 实现联合文件系统挂载

[root@ubuntu1804 ~]#cat /proc/filesystems 
nodev   sysfs
nodev   rootfs
nodev   ramfs
nodev   bdev
nodev   proc
nodev   cpuset
nodev   cgroup
nodev   cgroup2
nodev   tmpfs
nodev   devtmpfs
nodev   configfs
nodev   debugfs
nodev   tracefs
nodev   securityfs
nodev   sockfs
nodev   dax
nodev   bpf
nodev   pipefs
nodev   hugetlbfs
nodev   devpts
ext3
ext2
ext4
squashfs
vfat
nodev   ecryptfs
fuseblk
nodev   fuse
nodev   fusectl
nodev   pstore
nodev   mqueue
btrfs
nodev   autofs
nodev   rpc_pipefs
nodev   nfsd
nodev   overlay
nodev   aufs
[root@ubuntu1804 ~]#grep -i aufs /boot/config-4.15.0-29-generic 
CONFIG_AUFS_FS=m
CONFIG_AUFS_BRANCH_MAX_127=y
# CONFIG_AUFS_BRANCH_MAX_511 is not set
# CONFIG_AUFS_BRANCH_MAX_1023 is not set
# CONFIG_AUFS_BRANCH_MAX_32767 is not set
CONFIG_AUFS_SBILIST=y
# CONFIG_AUFS_HNOTIFY is not set
CONFIG_AUFS_EXPORT=y
CONFIG_AUFS_INO_T_64=y
CONFIG_AUFS_XATTR=y
# CONFIG_AUFS_FHSM is not set
# CONFIG_AUFS_RDU is not set
CONFIG_AUFS_DIRREN=y
# CONFIG_AUFS_SHWH is not set
# CONFIG_AUFS_BR_RAMFS is not set
# CONFIG_AUFS_BR_FUSE is not set
CONFIG_AUFS_BR_HFSPLUS=y
CONFIG_AUFS_BDEV_LOOP=y
# CONFIG_AUFS_DEBUG is not set

[root@ubuntu1804 ~]#mkdir dir{1,2}
[root@ubuntu1804 ~]#echo here is dir1 > dir1/file1
[root@ubuntu1804 ~]#echo here is dir2 > dir2/file2

[root@ubuntu1804 ~]#mkdir /data/aufs
[root@ubuntu1804 ~]#mount -t aufs -o br=/root/dir1=ro:/root/dir2=rw none /data/aufs
[root@ubuntu1804 ~]#ll /data/aufs/
total 16
drwxr-xr-x 4 root root 4096 Jan 25 16:22 ./
drwxr-xr-x 4 root root 4096 Jan 25 16:22 ../-rw-r--r-- 1 root root   
13 Jan 25 16:22 file1-rw-r--r-- 1 root root   
13 Jan 25 16:22 file2
[root@ubuntu1804 ~]#cat /data/aufs/file1
here is dir1
[root@ubuntu1804 ~]#cat /data/aufs/file2
here is dir2
Filesystem      Type      1K-blocks      Used Available Use% Mounted on
udev            devtmpfs     462560         0    462560   0% /dev
tmpfs           tmpfs         98512     10296     88216  11% /run
/dev/sda2       ext4       47799020   2770244  42570972   7% /
tmpfs           tmpfs        492552         0    492552   0% /dev/shm
tmpfs           tmpfs          5120         0      5120   0% /run/lock
tmpfs           tmpfs        492552         0    492552   0% /sys/fs/cgroup
/dev/sda3       ext4       19091540     45084  18053588   1% /data
/dev/sda1       ext4         944120     77112    801832   9% /boot
tmpfs           tmpfs         98508         0     98508   0% /run/user/0
none            aufs       47799020   2770244  42570972   7% /data/aufs

[root@ubuntu1804 ~]#echo write to file1 >> /data/aufs/file1-bash: /data/aufs/file1: Read-only file system
[root@ubuntu1804 ~]#echo write to file2 >> /data/aufs/file2
[root@ubuntu1804 ~]#cat /data/aufs/file1
here is dir1
[root@ubuntu1804 ~]#cat /data/aufs/file2
here is dir2
write to file2
[root@ubuntu1804 ~]#umount /data/aufs 
[root@ubuntu1804 ~]#mv dir1/file1 dir1/file2
[root@ubuntu1804 ~]#cat dir1/file2
here is dir1
[root@ubuntu1804 ~]#cat dir2/file2
here is dir2
write to file2
[root@ubuntu1804 ~]#mount -t aufs -o br=/root/dir1=ro:/root/dir2=rw none 
/data/aufs
[root@ubuntu1804 ~]#ls /data/aufs -l
total 4-rw-r--r-- 1 root root 13 Jan 25 16:22 file2
[root@ubuntu1804 ~]#cat /data/aufs/file2 
here is dir1
[root@ubuntu1804 ~]#

评论