Docker basic commands

[TOC]

Docker command line help documentation: https://docs.docker.com/reference/

Basic commands

Check out the Docker versiondocker version
Docker version # Displays the docker versionCopy the code
root@Win10:Desktop# Docker Version Client: Docker Engine - Community 1.40 Go version: go1.13.15 Git Commit: 4484C46d9d Built: Wed Sep 16 17:02:36 2020 OS/Arch: Linux/AMd64 Experimental: False Server: Docker Engine - Community # Server Engine: Version: 19.03.13 API Version: 1.40 (minimum version 1.12) Go version: go1.13.15 Git Commit: 4484C46d9d Built: Wed Sep 16 17:01:06 2020 OS/Arch: Linux/amd64 Experimental: false containerd: Version: 1.3.7 GitCommit: 8 fba4e9a7d01810a393d5d25a3621dc101981175 runc: Version: 1.0.0 - rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker - init: Version: 0.18.0 GitCommit: fec3683Copy the code
View the Docker system informationdocker info
Docker info # Displays the Docker system information including the number of images and containersCopy the code
root@Win10:Desktop# docker info Client: Debug Mode: false Server: Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 1 Server Version: 19.03.13 Storage Driver: overlay2 Backing Filesystem: extfs Supports D_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs 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 containerd version: 8fba4e9a7d01810a393d5d25a3621dc101981175 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd init version: Fec3683 Security Options: seccomp Profile: default Kernel Version: 4.19.128-MicrosoftStandard Operating System: fec3683 Security Options: seccomp Profile: default Kernel Version: 4.19.128-MicrosoftStandard Operating System: Ubuntu 18.04.5 LTS OSType: Linux Architecture: x86_64 CPUs: 12 Total Memory: 6.083GiB Name: Win10 ID: JBNO:FN32:73YF:35IA:RDZ4:TTXI:7ZKX:VND6:CROJ:U5UV:AGAV:5AY6 Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://5e43pwrr.mirror.aliyuncs.com/ Live Restore Enabled: falseCopy the code
Docker View help documentationdocker <command> --help
Docker <command> --help #Copy the code

The mirror command

Viewing a Local Mirrordocker images
root@Win10:Desktop# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 12 months ago 13.3 kB#REPOSITORY Image REPOSITORY source
#TAG Image TAG
#IMAGE ID ID of an IMAGE
#CREATE Time when the image is created
#SIZE Mirror SIZE

#optional-a, --all Show all images (default hides intermediate images) # Displays all images -q, --quiet Only Show numeric IDs # Displays Only the image IDCopy the code
Search the mirrordocker search
root@Win10:Desktop# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, The open - source base... 10369 [OK] Mariadb Mariadb is a community-developed fork of MyS... 3842 [OK] mysql/mysql-server Optimized mysql server Docker images. Create... 757 [OK] Percona Percona Server is a fork of the MySQL Server relati... 519 [OK] centos/mysql-57-centos7 mysql 5.7 SQL Database Server 86
#optionalroot@Win10:Desktop# docker search --filter=stars=5000 mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql mysql is a widely used, open source relation... 10369 [OK]Copy the code
Download mirrordocker pull
#Docker pull < image name [:tag]>root@Win10:Desktop# docker pull mysql Using default tag: latest Pulling from library/mysql 6ec7b7d162b2: Pull complete Pull complete 64f92f19e638: Pull complete 3e80b17bff96: Pull complete 014e976799f9: Pull complete 59ae84fee1b3: Pull complete ffe10de703ea: Pull complete 657af6d90c83: Pull complete 98bfb480322c: Pull complete 6aa3859c4789: Pull complete 1ed875d851ef: Pull complete Digest: Sha256:78800 # e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 signature Status: Downloaded newer image for mysql: latest docker. IO/library/mysql: latest # real address docker pull mysql = = = docker pull docker.io/library/mysql:latestCopy the code
Remove the mirrordocker rmi
root@Win10:Desktop# docker rmi-f hello-world # Untagged: hello-world:latest Untagged: hello-world@sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0 Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b root@Win10:Desktop# docker rmi -f hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0 Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b root@Win10:Desktop# docker rmi -f $(docker Latest Untagged: mysql:latest Untagged: mysql@sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 Deleted: sha256:a347a59280467629ae4d70ce555e3f96faca8ff2ff8c57cacc8184bebb681f66 Deleted: sha256:8c4db4ce0f63c9eec74a2e555bb7e2fa5e2de08389cbc747793aa30bc3ac04e1 Deleted: sha256:6179be2adc547662cd0ef2cb032b85809ac68923b7ab9c71e5a21b88bbe7f542 Deleted: sha256:17d702350e6aa7f240ca23090d24b059f5c324e292a77b5069225e76e0b51dd6 Deleted: sha256:5c5f95733957fd96300b5265d6b2df728415ed62e7b4b30eadd7d836d48187bf Deleted: sha256:fbf118fadaf5230c8df9a3b51c608393a8f3adb99c089ea2db253438efab1a0e Deleted: sha256:43b82d704a10e6d022fa3f31a5f827a00e339ee21dd2849a9b120ab82be9af71 Deleted: sha256:a4994702421d2b9a74c4f3810aa7ac09990e849905f23a1d8f358c826d58501f Deleted: sha256:36c598c7a6f90abf6d67cde4a58b0747bfbcc7441d3b782bdeea7cda8c9ad7b6 Deleted: sha256:86f598b4f8200bdd4ae752f916154e4e29d5b4c211bb124eb150b9957a3e0141 Deleted: sha256:9e979d97f92bf78a225c77c6b4ba74eb2e03efb95b19b69206cd17cee15a4b26 Deleted: sha256:aff48ce4678f78d83d7e9bfb9e88cd951c3da52da08779e99b6082edd1cc66f3 Deleted: sha256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9Copy the code

Container order

root@Win10:Desktop# docker pull centos
Copy the code
New container startdocker run
#Docker run [Optional] 

#Parameters that-p # Specify the port number -p # IP: host port: container port -p # Host port: container port -p # Container port: container port -p # Container port: container port -p # Container port: container port -p # Container port container port -p # Random port

#Running Centosroot@Win10:Desktop# docker run-it centos /bin/bash # start and enter the container [root@3c18212d26e3 /]# ls bin etc lib lost+found MNT proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@3c18212d26e3 /]# exit exit root@Win10:Desktop# ls WSharkCoder - shortcut. LNK 'Wuke university assistant development specification. Md Docker. Md Desktop. Ini lecture registration form LNK 'tacomall- SpringCloud 'WSL Ubuntu Command Line Note.md' '~$.doc'Copy the code
List all containersdocker ps
#Docker ps command
	#Lists running containers-n=? -n=? -q # displays only the CONTAINER number root@Win10:Desktop# docker ps # displays the running CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES root@Win10:Desktop# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c343f41fdac9 centos "/bin/bash" 2  minutes ago Exited (127) 9 seconds ago loving_newton 3c18212d26e3 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes  ago happy_goodall 0b54b1e62602 centos "/bin/bash" 9 minutes ago Exited (0) 2 minutes ago zealous_neumann 3df4665638f3 bf756fb1ae65 "/hello" 7 weeks ago Exited (0) 7 weeks ago nostalgic_sahaCopy the code
Out of the container
Exit # Container stops and exits CTRL + P + Q # Container does not stop exitingCopy the code
Deletes the specified containerdocker rm
Docker rm container ID# delete the specified container and cannot delete the running container; For mandatory delete: docker rm -f docker rm -f $(docker ps - aq) # delete all containers docker ps - a - q | xargs docker rm # delete all containersCopy the code
Start/stop the container
Docker start Docker restart Docker ID # Restart docker stop Docker ID # Stop running docker docker kill Docker ID # Forcibly stop the current containerCopy the code

Other Common Commands

Background startup container
#Docker run -d Image name
root@Win10:Desktop# docker run -d centos
0c7ae437f05d3726e4f5c92110e0330e04db43f7ee353aa20d749fc4d8bb2d6d
root@Win10:Desktop# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS
 PORTS               NAMES
 #Problem: Docker ps does not have the container when it is running in the backend?
 #After the Docker container runs in the background, there must be a foreground process. Docker will stop automatically if it finds no application.
Copy the code
See the log
#Docker logs -f -t --tail container  
#Parameters that-tf # Displays logs (with timestamp format) --tail <num> # displays the number of logsCopy the code
View container process information
#Docker Top container ID
root@Win10:Desktop# docker top af112ec23bd3
UID  	PID  	PPID C   	STIME	TTY  	TIME    	CMD
root 	2840 	2823 0   	21:47	pts/0	00:00:00	/bin/bash
Copy the code
View the container metadata
#Docker INSPECT Container ID
root@Win10:Desktop# docker inspect af112ec23bd3
[
    {
        "Id": "af112ec23bd3f895f7106a3a8991badc14584abf4d45dfc4e6d35e04b809f4d5", #容器ID
        "Created": "2021-01-11T13:47:12.9095342Z",					#创建时间 
        "Path": "/bin/bash",										#路径
        "Args": [],													#参数
        "State": {													#状态
            "Status": "running",		
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2840,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-11T13:47:14.0800604Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image":                                                	#镜像ID					"sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/af112ec23bd3f895f7106a3a8991badc14584abf4d45dfc4e6d35e04b809f4d5/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/af112ec23bd3f895f7106a3a8991badc14584abf4d45dfc4e6d35e04b809f4d5/hostname",
        "HostsPath": "/var/lib/docker/containers/af112ec23bd3f895f7106a3a8991badc14584abf4d45dfc4e6d35e04b809f4d5/hosts",
        "LogPath": "/var/lib/docker/containers/af112ec23bd3f895f7106a3a8991badc14584abf4d45dfc4e6d35e04b809f4d5/af112ec23bd3f895f7106a3a8991badc14584abf4d45dfc4e6d35e04b809f4d5-json.log",
        "Name": "/compassionate_tesla",
        "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": "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/01f04733198f946b5597fa9f1462d4cf8dfc23952bed3dea5271b628bc2a14e5-init/diff:/var/lib/docker/overlay2/857dbd38140359f25d6c1e554a458284414ec27fe68a740c4c7d6a67bb5612ca/diff",
                "MergedDir": "/var/lib/docker/overlay2/01f04733198f946b5597fa9f1462d4cf8dfc23952bed3dea5271b628bc2a14e5/merged",
                "UpperDir": "/var/lib/docker/overlay2/01f04733198f946b5597fa9f1462d4cf8dfc23952bed3dea5271b628bc2a14e5/diff",
                "WorkDir": "/var/lib/docker/overlay2/01f04733198f946b5597fa9f1462d4cf8dfc23952bed3dea5271b628bc2a14e5/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "af112ec23bd3",
            "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": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "e46430bd77a7d305766d041539da1cffef18005fea23102425f68bc48e3e453b",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/e46430bd77a7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "73e3e993e3ff97dcae6dbbe13d60e04d6cc6c203311e3ad2a5eae237df597ec5",
            "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": "365a4c0425fa7e56734ab48ebf6da2047a15708b20377dca45a73ab405b1fbde",
                    "EndpointID": "73e3e993e3ff97dcae6dbbe13d60e04d6cc6c203311e3ad2a5eae237df597ec5",
                    "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
                }
            }
        }
    }
]
Copy the code
Go to the currently running container
#Usually the container is running in the background, there is a need to enter the container to modify the configuration file

#Methods a
#docker exec- it IDbashShell container
#test
root@Win10:Desktop# docker exec -it af112ec23bd3 /bin/bash
[root@af112ec23bd3 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

#Way 2
#Docker ATTACH container ID
#test
root@Win10:Desktop# docker attach  af112ec23bd3
[root@af112ec23bd3 src]#

#The difference between them:
#docker exec  	Open a new terminal after entering the container, can operate in the new terminal;
#docker attach	# If you enter a terminal that is executing in the container, no new process will be started.
Copy the code
Copy files from the container to the host
#Docker CP Container ID: container PATH Host PATH

#test[root@af112ec23bd3 SRC]# touch test.txt # Create file in container [root@af112ec23bd3 SRC]# exit # Exit root@Win10:Desktop# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES af112ec23bd3 centos "/bin/bash" 48 minutes ago Exited (0) 7 seconds ago compassionate_tesla 8a629d96378a centos "/bin/bash" 48 minutes ago Exited (0) 48 minutes ago nifty_kilby C49a9ee5acd2 centos "/bin/sh -c 'while..." 49 minutes ago Exited (127) 49 minutes ago quizzical_buck 0b8c449f610c centos "/bin/sh -c 'while t..." 3 hours ago Exited (127) 3 hours ago brave_knuth 4f76f3259f6b centos "/bin/bash -c 'while..." 3 hours ago Exited (127) 3 hours ago vigilant_wing fb617b7afe78 centos "-C 'while true; Do e..." 3 hours ago Created naughty_franklin 2a979838e425 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago amazing_goldwasser 0c7ae437f05d centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago eloquent_maxwell 157ebdeb0df4 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago romantic_antonelli 6a4e7d5ffe18 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago interesting_ptolemy 3484e4aa3e54 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago pensive_elgamal c343f41fdac9 centos "/bin/bash" 3 hours ago Exited (127) 3 hours ago loving_newton 3c18212d26e3 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago happy_goodall 0b54b1e62602 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago zealous_neumann 3df4665638f3 bf756fb1ae65 "/hello" 8 weeks ago Exited (0) 8 weeks ago nostalgic_saha#Put af112EC23bd3 in /usr/localCopy the/SRC/test. TXT file to the current directory
root@Win10:Desktop# docker cp af112ec23bd3:/usr/local/src/Test.txt ./	
Copy the code

summary

Docker version Show the Docker version information docker info Display system-wide information Docker images List images # docker search search docker-hub for images # docker-hub Image or a repository from a register docker RMi Remove one or more images Docker rm Remove one or more containers Start start one or more containers # Docker restart One or more containers # Restart one or more containers Docker stop containers stop one or more running containers Docker top Display the running processes of a container docker logs Fetch the logs of a container Container # Check log information docker inspect Return low-level information on docker objects docker exec Run a command Docker Attach Attach local standard input, output, Docker cp Copy Files /folders between a container and the local docker cp Copy Files /folders between a container and the local Filesystem # copies files from the container to the current systemCopy the code