本文共 23559 字,大约阅读时间需要 78 分钟。
背景
整理一下常用的Docker命令
常用命令
帮助命令
1、查看版本
$ docker versionClient:Version: 18.03.0-ceAPI version: 1.37Go version: go1.9.4Git commit: 0520e24302Built: Fri Mar 23 08:31:36 2018OS/Arch: windows/amd64Experimental: falseOrchestrator: swarmServer: Docker Engine - CommunityEngine: 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:45 2019 OS/Arch: linux/amd64 Experimental: falsecontainerd: Version: v1.2.10 GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339runc: Version: 1.0.0-rc8+dev GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657docker-init: Version: 0.18.0 GitCommit: fec3683
2、查看详细信息
$ docker infoContainers: 2Running: 1Paused: 0Stopped: 1Images: 2Server Version: 19.03.5Storage Driver: overlay2Backing Filesystem: extfsSupports d_type: trueNative Overlay Diff: trueLogging Driver: json-fileCgroup Driver: cgroupfsPlugins:Volume: localNetwork: bridge host ipvlan macvlan null overlayLog: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslogSwarm: inactiveRuntimes: runcDefault Runtime: runcInit Binary: docker-initcontainerd version: b34a5c8af56e510852c35414db4c1f4fa6172339runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657init version: fec3683Security Options:seccomp Profile: defaultKernel Version: 4.14.154-boot2dockerOperating System: Boot2Docker 19.03.5 (TCL 10.1)OSType: linuxArchitecture: x86_64CPUs: 1Total Memory: 989.5MiBName: defaultID: XKXE:UYOL:PRQH:OYPM:DOWD:OBDF:4ZWT:FBKX:XAA7:QTCA:RQMD:SJYKDocker Root Dir: /mnt/sda1/var/lib/dockerDebug Mode (client): falseDebug Mode (server): falseRegistry: https://index.docker.io/v1/Labels:provider=virtualboxExperimental: falseInsecure Registries:127.0.0.0/8Registry Mirrors:https://r7g9xefx.mirror.aliyuncs.com/Live Restore Enabled: false
3、查看可用命令
$ docker --helpUsage: docker COMMANDA self-sufficient runtime for containersOptions: --config string Location of client config files (default "C:\\Users\\songzeceng\\.docker") -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 "C:\\Users\\songzeceng\\.docker\\machine\\machines\\default\\ca.pem") --tlscert string Path to TLS certificate file (default "C:\\Users\\songzeceng\\.docker\\machine\\machines\\default\\cert.pem") --tlskey string Path to TLS key file (default "C:\\Users\\songzeceng\\.docker\\machine\\machines\\default\\key.pem") --tlsverify Use TLS and verify the remote (default true) -v, --version Print version information and quitManagement Commands: config Manage Docker configs container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumesCommands: 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 codesRun 'docker COMMAND --help' for more information on a command.
镜像命令
1、列出本地镜像
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEscrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GBhello-world latest fce289e99eb9 14 months ago 1.84kB
选项:
1)、列出本地镜像,含中间镜像层
$ docker images -aREPOSITORY TAG IMAGE ID CREATED SIZEscrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GBhello-world latest fce289e99eb9 14 months ago 1.84kB
2)、只列出本地镜像id
$ docker images -q4ddd2efcb0dffce289e99eb9
3)、列出本地镜像和说明
$ docker images --digestsREPOSITORY TAG DIGEST IMAGE ID CREATED SIZEscrapinghub/splash latest sha256:01c89e3b0598e904fea184680b82ffe74524e83160f793884dc88d184056c49d 4ddd2efcb0df 8 weeks ago 2.17GBhello-world latest sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752 fce289e99eb9 14 months ago 1.84kB
4)、列出本地镜像和完整说明
$ docker images --digests --no-truncREPOSITORY TAG DIGEST IMAGE ID CREATED SIZEscrapinghub/splash latest sha256:01c89e3b0598e904fea184680b82ffe74524e83160f793884dc88d184056c49d sha256:4ddd2efcb0df639651607fe30ad532abf14e19c2c8f7d2b3c7f5eedfb7dbe4ce 8 weeks ago 2.17GBhello-world latest sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752 sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e 14 months ago 1.84kB
2、查询某个镜像的名字
$ docker search tomcatNAME DESCRIPTION STARS OFFICIAL AUTOMATEDtomcat Apache Tomcat is an open source implementati… 2654 [OK]tomee Apache TomEE is an all-Apache Java EE certif… 75 [OK]dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]bitnami/tomcat Bitnami Tomcat Docker Image 31 [OK]kubeguide/tomcat-app Tomcat image for Chapter 1 28consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 17 [OK]cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 12 [OK]....
其中official标签下为ok的是官方镜像
3、下载镜像
$ docker pull mysqlUsing default tag: latestlatest: Pulling from library/mysql68ced04f60ab: Pull completef9748e016a5c: Pull completeda54b038fed1: Pull complete6895ec5eb2c0: Pull complete111ba0647b87: Pull completec1dce60f2f1a: Pull complete702ec598d0af: Pull complete4aba2fcbe869: Pull completeb26bbbd533e6: Pull complete7bd100a66c55: Pull complete74149336419a: Pull complete145ea1f01648: Pull completeDigest: sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2cStatus: Downloaded newer image for mysql:latest
检验下载的镜像
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql latest 9b51d9275906 2 days ago 547MBtomcat latest 4e7840b49fad 9 days ago 529MBscrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GBhello-world latest fce289e99eb9 14 months ago 1.84kB
4、删除镜像
$ docker rmi -f hello-worldUntagged: hello-world:latestUntagged: hello-world@sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
镜像名(hello-world)后面可以加版本号(hello-world:3.2),默认最新
删除后检验
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql latest 9b51d9275906 2 days ago 547MBtomcat latest 4e7840b49fad 9 days ago 529MBscrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB
选项:
1)、批量删除
$ docker rmi -f mysql hello-world...
2)、删除全部
$ docker rmi -f $(docker images -qa)Untagged: mysql:latestUntagged: mysql@sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2cDeleted: sha256:9b51d9275906910446e03bb86e16a0fe0051d6518ba7ae39c8780fc2323fd637Deleted: sha256:3b5f20d41feb31513164dab347e9f5bea57dc9c644819aeea2dd5cbb7c7213bfDeleted: sha256:0c9402cadbef6d34b3c8db27a0d793011c2de41cabc14354734752ec7bd325c1...
检验之
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
5、提交容器副本,使之成为一个新的镜像
$ docker commit -a "szc" -m "tomcat of szc" 73832de15319 szc/tomcat:1.1sha256:6d1afa72732ce3c920783e9ce354559ec64ffde81cb453c887640b17b2582b59
-a指定作者,-m指定提交信息,后面的73832de15319 为要提交的容器id,最后szc/tomcat:1.1为新镜像的名字和版本
$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEszc/tomcat 1.1 6d1afa72732c About a minute ago 529MBtomcat latest 4e7840b49fad 9 days ago 529MBcentos latest 470671670cac 7 weeks ago 237MBscrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB$ docker run -it -p 7777:8080 szc/tomcat:1.1
:后面是tag值,不加表示latest
容器命令
1、新建并运行一个镜像容器,并启动一个交互式的伪终端
$ docker run -it centos[root@92ef52a1b68f /]#
root@后面的92ef52a1b68f就是这个容器的id
选项:1)、以守护进程的方式运行容器,可以在run后面加上-d选项
$ docker run -d centos1b4eef18d21a2c9de9c99e9dbca3b675fe9e1f8ad45f031954a8d453e0b4a949$
此时如果用docker ps -a查看,会发现容器已经退出。因为容器运行的如果不是那些一直挂起的命令,就会立刻退出。
2)、也可以指定端口号,在run后面加上-p选项,后面跟上容器在docker上的端口:容器在宿主机上的端口
$ docker run -it -p 8080:8080 tomcat
2、列出所有运行着的容器
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES92ef52a1b68f centos "/bin/bash" 6 minutes ago Up 6 minutes exciting_johnson
选项:列出历史上所有运行过的容器
$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES92ef52a1b68f centos "/bin/bash" 9 minutes ago Up 9 minutes exciting_johnsona8c26efb021b fce289e99eb9 "/hello" 12 hours ago Exited (0) 12 hours ago exciting_austinfb410e7667b4 scrapinghub/splash "python3 /app/bin/sp…" 21 hours ago Exited (255) 11 hours ago 0.0.0.0:8050->8050/tcp reverent_swartz
最后面的名字可以在启动容器时使用--name指定,如下
$ docker run -i -t --name "szc" centos[root@916e23b387b1 /]#
然后在另一Quickstart Terminal使用docker ps时输出如下
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES916e23b387b1 centos "/bin/bash" 8 seconds ago Up 8 seconds szc
3、容器退出
1)、容器停止退出
[root@92ef52a1b68f /]# exitexitsongzeceng@LAPTOP-GCPD2768 MINGW64 /d/develop/Docker Toolbox$
命令行验证
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES92ef52a1b68f centos "/bin/bash" 10 minutes ago Exited (0) 49 seconds ago exciting_johnsona8c26efb021b fce289e99eb9 "/hello" 12 hours ago Exited (0) 12 hours ago exciting_austinfb410e7667b4 scrapinghub/splash "python3 /app/bin/sp…" 21 hours ago Exited (255) 11 hours ago 0.0.0.0:8050->8050/tcp reverent_swartz
2)、容器不停止退出:Ctrl+P+Q
此时再进去,可以用docker attach(不启动新的进程)
[root@bbf512a4aa58 /]#$ docker attach bbf512a4aa58[root@bbf512a4aa58 /]#
或者使用docker exec
$ docker exec -t bbf512a4aa58 /bin/bash[root@bbf512a4aa58 /]#
/bin/bash表示以root的身份进入容器终端
3)、如果只是执行一段命令,可以用exec(启动新的进程)
$ docker exec -t bbf512a4aa58 ls -l /tmp total 8-rwx------ 1 root root 671 Jan 13 21:49 ks-script-_srt3u3c-rwx------ 1 root root 1379 Jan 13 21:49 ks-script-gpqu_kuo$
-t后面跟着容器id,再后面是要执行的命令
4、启动容器
$ docker start 92ef52a1b68f92ef52a1b68f
start后面是容器id或容器名
5、重启容器
$ docker restart 92ef52a1b68f92ef52a1b68f
restart后面是容器id或容器名
6、关闭容器
$ docker stop 92ef52a1b68f92ef52a1b68f
或者
$ docker kill 92ef52a1b68f92ef52a1b68f
7、删除已停止的容器
$ docker rm 92ef52a1b68f92ef52a1b68f
可以加上-f删除正在运行着的容器;如果要删除所有已停止的容器,可以输入如下命令
$ docker rm $(docker ps -a)1b4eef18d21a916e23b387b1exciting_austinfb410e7667b4...
8、查看容器日志
$ docker logs -t -f bbf512a4aa58[root@bbf512a4aa58 /]# ls -l2020-03-08T01:42:04.553406000Z total 48.....2020-03-08T01:42:34.398428190Z [root@bbf512a4aa58 /]# pwd2020-03-08T01:42:34.398477984Z /
其中-t表示显示日期,-f表示不断追加。下面是运行容器时执行的所有命令和输出
9、查看容器内部细节
$ docker inspect bbf512a4aa58 [ { "Id": "bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe", "Created": "2020-03-08T01:41:24.391275544Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 3571, "ExitCode": 0, "Error": "", "StartedAt": "2020-03-08T01:41:24.584563557Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee", "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/resolv.conf", "HostnamePath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/hostname", "HostsPath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/hosts", "LogPath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe-json.log", "Name": "/agitated_wilbur", "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": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 30, 120 ], "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": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875-init/diff:/mnt/sda1/var/lib/docker/overlay2/956a46cec26f803c02d493bde34cd3b2ef9b779d2df20629036fe96324ac5eb2/diff", "MergedDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875/merged", "UpperDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875/diff", "WorkDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "bbf512a4aa58", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20200114", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-01-14 00:00:00-08:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "1b44bd7ba751a5be73ce9f497b59487718d62444ea468d2199994acc898bde9b", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/1b44bd7ba751", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "a25e990586b1166973b72e22bc85b4c8eed851b12a88a6bdb180755670d09fdd", "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": "ea6a244a17bafa7d0161cb9016496335d961f50b7380e6d730d8113fff1d9985", "EndpointID": "a25e990586b1166973b72e22bc85b4c8eed851b12a88a6bdb180755670d09fdd", "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 } } } }]
10、查看容器内运行着的进程
$ docker top bbf512a4aa58UID PID PPID C STIME TTY TIME CMDroot 3571 3549 0 01:41 ? 00:00:00 /bin/bash
11、把文件从容器复制到主机上
$ docker cp bbf512a4aa58:/tmp/ks-script-gpqu_kuo /
格式为docker cp 容器id:容器文件路径 目标路径
转载地址:https://blog.csdn.net/qq_37475168/article/details/105028095 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!