docker安装-macos安装
  • 官网下载最新的docker版本:macos为Docker-Destop版本
    • https://download.docker.com/mac/stable/Docker.dmg
  • 具体安装略

docker命令
  • 前置:在docker 1.13版本后,新增了Management Commands,这些命令通过逻辑分组,提升了命令的可记忆性、易用性,便于用户入门学习
  • http://dockone.io/article/2059
有如下几种:— 这里介绍非管理命令
  • 帮助命令
  • 认证登录
  • 镜像命令
  • 容器命令
帮助命令
  • docker help:查看命令指导
  • docker info:docker系统全部信息
# client端信息
Client:
 Debug Mode: false

# server端信息(daemon进程)
Server:
 Containers: 26		# 所有启动过的容器数(包括退出的)
  Running: 1		# 正在运行的容器数
  Paused: 0			# 暂停的容器
  Stopped: 25		# 停止的容器
 Images: 12			# 镜像数
 Server Version: 19.03.8		# server版本
 Storage Driver: overlay2		# 存储驱动,含有多种:
 # https://docs.docker.com/storage/storagedriver/select-storage-driver/
  Backing Filesystem: <unknown>
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file		# 日志驱动
 Cgroup Driver: cgroupfs		# cgroup驱动
 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				# 运行环境
 Default Runtime: runc		# 默认运行环境
 Init Binary: docker-init	# docker-init库
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.19.76-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 1.943GiB
 Name: docker-desktop
 ID: 2ZVU:YSRR:RQ4P:7UGT:QWMK:S3WF:4DPR:CVQJ:QPQD:KATC:23RD:FRPG
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 41
  Goroutines: 56
  System Time: 2020-05-31T07:29:56.6708494Z
  EventsListeners: 3
 HTTP Proxy: gateway.docker.internal:3128		# HTTP代理
 HTTPS Proxy: gateway.docker.internal:3129		# HTTPS代理
 Registry: https://index.docker.io/v1/			# registry地址
 Labels:
 Experimental: false		# 实验命令
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:			# 镜像仓库(国内,用于镜像加速)
  https://7c473ae7bfe94484ab4c600817accc78.mirror.swr.myhuaweicloud.com/
 Live Restore Enabled: false
 Product License: Community Engine
  • docker version:查看版本信息
zhaoyue@zhaoyuedeMacBook-Pro html % docker version
# client端信息,Docker Engine为docker的管理工具
#  Community社区信息
Client: Docker Engine - Community
 Version:           19.03.8			# 版本信息
 API version:       1.40			# API版本,周边工具可能对此有依赖
 Go version:        go1.12.17		# go的版本
 Git commit:        afacb8b			# github仓库commit
 Built:             Wed Mar 11 01:21:11 2020
 OS/Arch:           darwin/amd64	# 架构信息
 Experimental:      false 			# 是否是测试命令,可以通过export方式打开

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:		# 容器运行环境的核心引擎,已开源,实现对容器各种操作
 # https://www.oschina.net/news/80084/docker-ngine-containerd-open
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:				# docker的运行环境版本
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:		# 用于指定容器的初始进程(1号进程)
 # https://docs.docker.com/engine/reference/run/
  Version:          0.18.0
  GitCommit:        fec3683
认证登录
  • docker login:登录registry
    • docker login -u $AK -p $PWD $RegistryURL
镜像命令
  • docker build:镜像构建
    • 示例(具体见后续Dockerfile学习):docker build -f Dockerfile -t firstimage:1.0.0 .
  • docker pull:拉取镜像
    • docker pull [OPTIONS] NAME[:TAG|@DIGEST]
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker pull redis
Using default tag: latest
latest: Pulling from library/redis
afb6ec6fdc1c: Already exists 
608641ee4c3f: Already exists 
668ab9e1f4bc: Already exists 
78a12698914e: Pull complete 
d056855f4300: Pull complete 
618fdf7d0dec: Pull complete 
Digest: sha256:d27740b5bd12087efc2b30ac9102fa767d6cc83611dc0fc28f0edb042e835996
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
  • docker push:推送镜像

  • docker search:搜索镜像

zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker search redis
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis                            Redis is an open source key-value store that…   8223                [OK]                
bitnami/redis                    Bitnami Redis Docker Image                      145                                     [OK]
  • docker rmi:删除镜像
  • docker images:查看本地镜像列表
    • -q:只显示id
    • -a:显示所有信息
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              36304d3b4540        2 days ago          104MB
  • docker tag:从源镜像创建一个引用、新的镜像
    • 删除源镜像时,一般要显示删除该引用镜像
  • docker history:查看一个镜像的变更(构建、提交)历史
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker history redis
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
36304d3b4540        2 days ago          /bin/sh -c #(nop)  CMD ["redis-server"]         0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  EXPOSE 6379                  0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B                  
<missing>           2 days ago          /bin/sh -c #(nop) COPY file:df205a0ef6e6df89…   374B                
<missing>           2 days ago          /bin/sh -c #(nop) WORKDIR /data                 0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  VOLUME [/data]               0B           
...(下面的略)
  • docker save:导出镜像为压缩文件(配合load命令一起使用)

    • 示例:docker save redis:latest -o redis.tar.gz,将redis镜像导出为tar.gz
  • docker load:从tar压缩文件或者STDIN中导入镜像

zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker load -i redis.tar.gz 
c112794a20c5: Loading layer [==================================================>]  30.48MB/30.48MB
bf8b736583f0: Loading layer [==================================================>]  1.536kB/1.536kB
6ef422d19214: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: redis:latest
  • docker commit:从一个容器中导出一个新的镜像:一般用于手动制作镜像
容器命令
  • docker ps:查看容器列表
    • -a:显示所有,包括停止的
    • -n:显示最近的n条
    • -q:只显示id
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3eda31c046d5        tomcat              "catalina.sh run"   5 days ago          Up 5 days           8080/tcp            new_24
  • docker stop:停止容器
  • docker pause:暂停容器
  • docker unpause:恢复所有的暂停容器
  • docker kill:杀死一个或者多个运行中的容器
  • docker run:运行容器
    • 参数较多,主要包括如下:
      port相关、io相关、cpu相关、磁盘相关、健康检查、ip设置、内存、安全等等
    • 示例:docker run -it --name centos-my centos
      运行centos容器,命名为centos-my, 并进入伪终端
    • docker run -p 4306:3306 --env MYSQL_ROOT_PASSWORD=123456 mysql
      启动mysql容器,并注入密码
    • Exit:退出容器,并停止容器
    • Ctrl+p+q:退出容器,但是容器还在运行
    • ctlr+d:退出容器
  • docker start:启动一个或多个停止的容器
  • docker attach:进入容器的根目录(根目录由Dockerfile中的WORKDIR定义
  • docker exec:在运行中的容器运行命令
    • docker exec -it [容器id] /bin/bash:进入容器bash命令界面
    • docker exec [容器id] ls:在容器运行ls命令,并显示结果
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker exec 3eda31c046d5 ls
bin
conf
lib
logs
native-jni-lib
temp
webapps
webapps.dist
work
  • docker stats:显示容器的实时资源使用信息
  • docker inspect:查看容器的所有信息(后续再仔细研究)
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker inspect 3eda31c046d5
[
    {
        "Id": "3eda31c046d552f53db240ea103fe1a70f2c711f379433a7a9ef152c0b91ffd0",	# 容器id
        "Created": "2020-05-26T07:17:46.7974837Z",
        "Path": "catalina.sh",
        "Args": [
            "run"
        ],
        "State": {	# 状态
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 7276,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-05-26T07:17:47.1282966Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": 	"sha256:1b6b1fe7261ea2f94c5911ad04395cb8d05428cad39fa60e9fee556f79e9afc2",		# 容器的镜像id
        "ResolvConfPath": "/var/lib/docker/containers/3eda31c046d552f53db240ea103fe1a70f2c711f379433a7a9ef152c0b91ffd0/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3eda31c046d552f53db240ea103fe1a70f2c711f379433a7a9ef152c0b91ffd0/hostname",
        "HostsPath": "/var/lib/docker/containers/3eda31c046d552f53db240ea103fe1a70f2c711f379433a7a9ef152c0b91ffd0/hosts",
        "LogPath": "/var/lib/docker/containers/3eda31c046d552f53db240ea103fe1a70f2c711f379433a7a9ef152c0b91ffd0/3eda31c046d552f53db240ea103fe1a70f2c711f379433a7a9ef152c0b91ffd0-json.log",
        "Name": "/new_24",
        "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": [
                "hungry_herschel"
            ],
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "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/74fb4ba4b099ec82a1a3215f9cda35f4889da59222cb73ba3d9d91e931f9b52a-init/diff:/var/lib/docker/overlay2/7c36eed283d796641a995ce0224219169adff1ff7ab9953e4c31890f51b19851/diff:/var/lib/docker/overlay2/8f70bb6e512bba14b47fb757baba8d611c5160bd602d3e9d510df7ab5c29a9b9/diff:/var/lib/docker/overlay2/3f8d381e0e4871f93f94843dc6c9e13c705504d733d88786efdeffe1eb226afa/diff:/var/lib/docker/overlay2/4ef5922c4bfe574f6d790c5b47661289ab8aa585152306320c59c759c5745be0/diff:/var/lib/docker/overlay2/ad8ccc75aeaadb96ccc37a7bfd968aa18c5a7f188f84fa853ae839fd6229e418/diff:/var/lib/docker/overlay2/c92dc377ae60cf7983627c0fef371a28f37c28955177d1e81ed81c42881866d9/diff:/var/lib/docker/overlay2/416d601e0d7e15b979a56abd9ccfd22d8a5b7b6e610dfcc0f6fdc26b70a33a76/diff:/var/lib/docker/overlay2/326e8ef78ce72a5ae8141966fc8a775eb4268af8a83eceb9c9053a8f0c8d715e/diff:/var/lib/docker/overlay2/3d4b89e0cc0987cc6b5a50ff12993bfe225ec0897dbbc2a0079fa3a6aeaaa282/diff:/var/lib/docker/overlay2/4ed83fcb49aac69c4cb1df2bcc99b1887a1de1a741857c97050637ff5c0a7371/diff",
                "MergedDir": "/var/lib/docker/overlay2/74fb4ba4b099ec82a1a3215f9cda35f4889da59222cb73ba3d9d91e931f9b52a/merged",
                "UpperDir": "/var/lib/docker/overlay2/74fb4ba4b099ec82a1a3215f9cda35f4889da59222cb73ba3d9d91e931f9b52a/diff",
                "WorkDir": "/var/lib/docker/overlay2/74fb4ba4b099ec82a1a3215f9cda35f4889da59222cb73ba3d9d91e931f9b52a/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/tmp/myHost",
                "Destination": "/tmp/cHost",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "3eda31c046d5",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8080/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "JAVA_HOME=/usr/local/openjdk-11",
                "JAVA_VERSION=11.0.7",
                "JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk11-upstream-binaries/releases/download/jdk-11.0.7%2B10/OpenJDK11U-jdk_",
                "JAVA_URL_VERSION=11.0.7_10",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23",
                "TOMCAT_MAJOR=9",
                "TOMCAT_VERSION=9.0.35",
                "TOMCAT_SHA512=0db27185d9fc3174f2c670f814df3dda8a008b89d1a38a5d96cbbe119767ebfb1cf0bce956b27954aee9be19c4a7b91f2579d967932207976322033a86075f98"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "Image": "tomcat",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "3777bf5673a151769e2c6e9bcb0d89a3130e6c6f318f6c1513503ba15ef11370",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/3777bf5673a1",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "574694263a3bc98d8a82138140c3ec3c0325e7b5ed2afd6421e3c50dfbfe67a2",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "62067515bfbd22f1c293055752865b88b09a3ac892025a060fe220340b5f2dda",
                    "EndpointID": "574694263a3bc98d8a82138140c3ec3c0325e7b5ed2afd6421e3c50dfbfe67a2",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
  • docker restart:重启容器
  • docker diff:查看容器的文件系统改变情况
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker diff 3eda31c046d5
C /usr
C /usr/local
C /usr/local/tomcat
C /usr/local/tomcat/conf
A /usr/local/tomcat/conf/Catalina
A /usr/local/tomcat/conf/Catalina/localhost
C /usr/local/tomcat/logs
A /usr/local/tomcat/logs/catalina.2020-05-26.log
A /usr/local/tomcat/logs/host-manager.2020-05-26.log
A /usr/local/tomcat/logs/localhost.2020-05-26.log
A /usr/local/tomcat/logs/localhost_access_log.2020-05-26.txt
A /usr/local/tomcat/logs/manager.2020-05-26.log
C /tmp
A /tmp/cHost
C /tmp/hsperfdata_root
A /tmp/hsperfdata_root/1
  • docker top:查看容器的进程情况:
zhaoyue@zhaoyuedeMacBook-Pro /tmp % docker top 3eda31c046d5
PID                 USER                TIME                COMMAND
7276                root                9:21                /usr/local/openjdk-11/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
  • docker rm:移除一个或多个容器(包括正在运行的)
  • docker rename:重命名一个容器:比如想改变一个容器的名称
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐