Common docker commands

Help document

  • How to use the document

Basic commands

docker version
Copy the code
docker info 
Copy the code
Docker -- help commandCopy the code

The mirror command

View a list of all mirrors

docker  images
Copy the code
  • optional
    • -a Lists all mirrors
    • -q Displays only the ID of the mirror

Search the mirror

Docker Search Image nameCopy the code
  • Search for images using DockerHub: Find images
  • optional
    • –filter =stars=3000 Filters by stars

Download mirror

Docker pull Image name Docker pull image name :tagCopy the code

Remove the mirror

Rmi -------remove Image Docker RMI -f Image ID/image name Delete an image Docker RMI -f Image ID Image ID Delete multiple images Docker RMI -F $(Docker images) -aq) delete all mirrorsCopy the code

Container order

  • We have the image to create containers, Linux, download a centos image to test learning
    • docker pull centos

Create a new container and start

  • Docker run [Optional] image
    • –name = “name” Give the containers a name centos1 Centos2 is used to distinguish containers
    • -d Runs in background mode
    • – IT Displays the contents of the container in interactive mode
    • -p Specifies the port of the container. -p 8080:8080
      • Four ways to specify ports
        • -p IP: host port: container port
        • -p Host port: container port (most commonly used)
        • -p Indicates the container port
        • The port number

Lists all running containers

  • Docker ps command
    • No argument lists the containers that are currently running
    • -a lists currently running containers + Brings out containers that have been run in history
    • – n =? Displays the recently created container
    • -q Displays only the container number

Out of the container

  • ExitThe immediate container stops and exits
  • Ctrl+QThe container does not stop but exits

Remove the container

  • Docker RM Container ID
  • Docker RM Container ID Container ID
  • Docker rm -f $(docker ps -aq)
    • Docker ps – a – q | xargs Docker rm pipe way to remove all containers
    • -f is forcibly deleted

Start and stop container operations

  • Start the
    • Docker start Container ID
  • Restart the container
    • Docker restart Container ID
  • Stop the container
    • Docker stop Container ID
  • Kill the container
    • Docker kill Container ID

Other Common commands

Background startup container

  • Docker run -d specifies the image name
    • Docker PS found that centos stopped
    • Common pit: Docker container using background running, there must be a foreground process, Docker found no application, it will automatically stop
      • Popular understanding is: when we run centos, we will switch to the container for operation before, but -d directly starts from the background, docker will detect that there is no operation in this part or the container does not move, and will stop the corresponding resources by itself
      • So when the nginx container starts and finds that it is not providing services, it will stop immediately

See the log

  • Docker logs -tf --tail 10 Container ID
  • test
    • Write a shell script and let the program run in the background
      • Docker run -d centos /bin/sh -c “while true; do echo hahaha; sleep 1; done”
    • docker ps
    • Docker logs -tf –tail 10 Container ID

View information about processes inside a container

  • The top command
    • Docker Top Container ID

View the metadata of the container

  • Docker inspect Container ID
[
    {
        "Id": "eea991c4d00ba5edf76f1eaf1bc769c6d78169b39d8426ebcb6bfa8a1710d7d4",            #容器的id
        "Created": "2020-10-02T10:40:04.102524201Z",                     # 创建时间
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo haha;sleep 1;done"                                          # 参数,运行的时候的参数
        ],
        "State": {                                                      # 状态
            "Status": "running",
            "Running": true,               
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 28047,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-10-02T10:45:04.993272581Z",
            "FinishedAt": "2020-10-02T10:42:22.663247801Z"
        },
        "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
        "ResolvConfPath": "/var/lib/docker/containers/eea991c4d00ba5edf76f1eaf1bc769c6d78169b39d8426ebcb6bfa8a1710d7d4/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/eea991c4d00ba5edf76f1eaf1bc769c6d78169b39d8426ebcb6bfa8a1710d7d4/hostname",
        "HostsPath": "/var/lib/docker/containers/eea991c4d00ba5edf76f1eaf1bc769c6d78169b39d8426ebcb6bfa8a1710d7d4/hosts",
        "LogPath": "/var/lib/docker/containers/eea991c4d00ba5edf76f1eaf1bc769c6d78169b39d8426ebcb6bfa8a1710d7d4/eea991c4d00ba5edf76f1eaf1bc769c6d78169b39d8426ebcb6bfa8a1710d7d4-json.log",
        "Name": "/dreamy_mcclintock",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "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/d0ed21bb25573ab3a59182ddfc05c54a169353094b5742fc3394cdce4288e3b8-init/diff:/var/lib/docker/overlay2/8c0d8f954b69ff5d36f707acf628e795ab75eaff1be8a6013c8529a0515e6cc7/diff",
                "MergedDir": "/var/lib/docker/overlay2/d0ed21bb25573ab3a59182ddfc05c54a169353094b5742fc3394cdce4288e3b8/merged",
                "UpperDir": "/var/lib/docker/overlay2/d0ed21bb25573ab3a59182ddfc05c54a169353094b5742fc3394cdce4288e3b8/diff",
                "WorkDir": "/var/lib/docker/overlay2/d0ed21bb25573ab3a59182ddfc05c54a169353094b5742fc3394cdce4288e3b8/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "eea991c4d00b",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo haha;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200809",
                "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": "95611910bf93a7f37cc5ce62b7430a21bde5736bc9831f658f3cbfa974b365a5",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/95611910bf93",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "a04cb929cd28c88618e0f170ff3d968b23d350f1bb7baa8e080d3e4e03d6c324",
            "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": "d80554cf321bff3d2821acfe37abdff08809fa1ee0f4a8d37693bb949e529476",
                    "EndpointID": "a04cb929cd28c88618e0f170ff3d968b23d350f1bb7baa8e080d3e4e03d6c324",
                    "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

Enter the currently running container

  • exec
    • Typically, containers run in the background, requiring us to go into the container and make some configuration changes
    • Docker exec -it Container ID /bash/bin
    • After entering a container, open a new terminal that you can operate on. (Common)
  • Attach
    • Docker Attach Container ID
    • Enter the terminal that is executing commands in the container and no new process will be started

Copy files from a container to a host

  • Docker cp Container ID :/xx/ File path The local path
    • Copying is a manual process, and in the future we will use -v volume technology to achieve automatic synchronization

View History Command

  • Docker History Image ID

Command summary