docker 学习笔记(二)

docker 常用操作

docker 镜像加速
国内加速器:
网易加速器链接: http://hub-mirror.c.163.com
中科大ustc加速器链接:https://docker.mirrors.ustc.edu.cn
阿里云加速器 –需要注册阿里云帐号登录
Daocloud加速器–需要注册帐号登录
官方加速:https://registry.docker-cn.com

国内仓库
时速云镜像仓库
网易云镜像服务
DaoCloud镜像市场
阿里云镜像库
[root@test docker]#  vim /etc/docker/daemon.json
 {
"registry-mirrors":["http://hub-mirror.c.163.com"],
 "live-restore":false,
"experimental":true
}

 
 [root@test docker]# sudo service docker restart
Redirecting to /bin/systemctl restart docker.service
 [root@test docker]# service docker start 
Redirecting to /bin/systemctl start docker.service
docker 版本
[root@test docker]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Thu Nov 12 15:11:46 2020
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Thu Nov 12 15:11:46 2020
 OS/Arch:         linux/amd64
 Experimental:    true
[root@test docker]# docker info
Containers: 25
 Running: 0
 Paused: 0
 Stopped: 25
Images: 15
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: xfs
 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
Swarm: active
 NodeID: 9q7q34aglofyxpef5fu9kkzft
 Is Manager: true
 ClusterID: 3nsg4b15lh7o3mppxubs2p8sh
 Managers: 1
 Nodes: 1
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 3
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
 Node Address: 192.168.218.196
 Manager Addresses:
  192.168.218.196:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: N/A (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: N/A (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-1160.6.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 487 MiB
Name: test
ID: 3FGN:UZTZ:JLAH:YGW4:IENS:7YHR:M553:WSNT:7QRI:BX4L:OGKA:EAUV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
 provider=generic
Experimental: true
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:
 https://registry.docker-cn.com
Live Restore Enabled: false
Registries: docker.io (secure)
查询 docker 仓库镜像版本
[root@test docker]# docker search nginx
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                              Official build of Nginx.                        14204     [OK]   
docker.io   docker.io/jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1932                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   797                  [OK]
docker.io   docker.io/linuxserver/nginx                  An Nginx container, brought to you by Linu...   137        
docker.io   docker.io/jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ...   123        
docker.io   docker.io/tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rt...   107                  [OK]
docker.io   docker.io/bitnami/nginx                      Bitnami nginx Docker Image                      92                   [OK]
从远程获取镜像
[root@test docker]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ... 
sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9: Pulling from docker.io/library/nginx
Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Status: Image is up to date for docker.io/nginx:latest
[root@test docker]# docker image pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ... 
sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9: Pulling from docker.io/library/nginx
Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Status: Image is up to date for docker.io/nginx:latest
查询本地镜像列表
[root@test docker]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
docker.io/bitnami/postgresql   latest              01b87603926d        4 days ago          252 MB
runoob/centos                  6.7                 465d1b54afd3        8 days ago          191 MB
docker.io/nginx                latest              ae2feff98a0c        10 days ago         133 MB
docker.io/centos               latest              300e315adb2f        2 weeks ago         209 MB
[root@test docker]# docker image ls
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
docker.io/bitnami/postgresql   latest              01b87603926d        4 days ago          252 MB
runoob/centos                  6.7                 465d1b54afd3        8 days ago          191 MB
docker.io/nginx                latest              ae2feff98a0c        10 days ago         133 MB
修改镜像 tag
[root@test ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             <none>              a9d583973f65        35 hours ago        1.23MB
[root@test ~]# docker tag a9d583973f65 busybox:latest
[root@test ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              a9d583973f65        35 hours ago        1.23MB
[root@test ~]# docker tag busybox:latest busybox:2.0
[root@test ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             2.0                 a9d583973f65        35 hours ago        1.23MB
busybox             latest              a9d583973f65        35 hours ago        1.23MB

特殊镜像 busybox
[root@test docker]# docker image pull busybox
Using default tag: latest
Trying to pull repository docker.io/library/busybox ... 
sha256:bde48e1751173b709090c2539fdf12d6ba64e88ec7a4301591227ce925f3c678: Pulling from docker.io/library/busybox
ea97eb0eb3ec: Pull complete 
Digest: sha256:bde48e1751173b709090c2539fdf12d6ba64e88ec7a4301591227ce925f3c678
Status: Downloaded newer image for docker.io/busybox:latest
删除镜像
[root@test docker]# docker rmi 6fae60ef3446
Untagged: docker.io/training/webapp:latest
Untagged: docker.io/training/webapp@sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Deleted: sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557
Deleted: sha256:875bde2b9e2d99e7c1362993645a474fe621475c6fc1b1623c9ed5312b7bdeae
[root@test docker]# docker image rm 300e315adb2f
Untagged: docker.io/centos:latest
Untagged: docker.io/centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859
查询 docker 容器列表
[root@test docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@test docker]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
运行镜像
[root@test docker]# docker run --name busy1 -it docker.io/busybox:latest
/usr/bin/docker-current: Error response from daemon: shim error: docker-runc not installed on system.
[root@test docker]# cd /usr/libexec/docker
[root@test docker]# ls
docker-init-current  docker-proxy-current  docker-runc-current
[root@test docker]# ln -s /usr/libexec/docker/docker-runc-current /usr/libexec/docker/docker-runc
[root@test docker]# ls
docker-init-current  docker-proxy-current  docker-runc  docker-runc-current
[root@test docker]# vim /etc/docker/daemon.json
{
"registry-mirrors":["http://hub-mirror.c.163.com"],
 "live-restore":false,
"experimental":true,
"runtimes": {
        "docker-runc": {
            "path": "/usr/libexec/docker/docker-runc-current"
        }
    },
    "add-runtime": "docker-runc=/usr/libexec/docker/docker-runc-current",
    "default-runtime": "docker-runc"
}
 [root@test docker]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@test docker]# docker run --name busy1 -it docker.io/busybox:latest
/ # ls
bin   dev   etc   home  proc  root  run   sys   tmp   usr   var
[root@test ~]# docker ps
CONTAINER ID        IMAGE                                                                           COMMAND                  CREATED             STATUS              PORTS               NAMES
4b419e99375b        docker.io/busybox:latest                                                        "sh"                     30 minutes ago      Up 30 minutes                           busy1
[root@test ~]# docker inspect busy1
[
    {
        "Id": "4b419e99375b99271ee0621dc45d8d32ee9d6f6a70602160737d5127430e1629",
        "Created": "2020-12-26T10:16:15.62642303Z",
        "Path": "sh",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 20599,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-12-26T10:16:15.86278471Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:219ee5171f8006d1462fa76c12b9b01ab672dbc8b283f186841bf2c3ca8e3c93",
        "ResolvConfPath": "/var/lib/docker/containers/4b419e99375b99271ee0621dc45d8d32ee9d6f6a70602160737d5127430e1629/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/4b419e99375b99271ee0621dc45d8d32ee9d6f6a70602160737d5127430e1629/hostname",
        "HostsPath": "/var/lib/docker/containers/4b419e99375b99271ee0621dc45d8d32ee9d6f6a70602160737d5127430e1629/hosts",
        "LogPath": "/var/lib/docker/containers/4b419e99375b99271ee0621dc45d8d32ee9d6f6a70602160737d5127430e1629/4b419e99375b99271ee0621dc45d8d32ee9d6f6a70602160737d5127430e1629-json.log",
        "Name": "/busy1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "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,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "docker-runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "overlay2",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/7e4edfc4e114f176b03360f6e05e3e73883e7df6db8598e79001546f9ecd4943-init/diff:/var/lib/docker/overlay2/55ebd5604ab5a7b04c548f74d7dba0e90101d2db0c06128d8660e86241d3285e/diff",
                "MergedDir": "/var/lib/docker/overlay2/7e4edfc4e114f176b03360f6e05e3e73883e7df6db8598e79001546f9ecd4943/merged",
                "UpperDir": "/var/lib/docker/overlay2/7e4edfc4e114f176b03360f6e05e3e73883e7df6db8598e79001546f9ecd4943/diff",
                "WorkDir": "/var/lib/docker/overlay2/7e4edfc4e114f176b03360f6e05e3e73883e7df6db8598e79001546f9ecd4943/work"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "4b419e99375b",
            "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": [
                "sh"
            ],
            "Image": "docker.io/busybox:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "33e452ae1064b6f06323e10465ae0819b57780c587217d998193985ce8d0b034",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/33e452ae1064",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "11ef9041887bbee812a7f1b807cad88dec92a2d7a2d7b7ea1c725d3672e78568",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42🇦🇨11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "1641bcacd8b19005a41d83ecae9af649b7102518b410a03108c5c79d45a203cb",
                    "EndpointID": "11ef9041887bbee812a7f1b807cad88dec92a2d7a2d7b7ea1c725d3672e78568",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42🇦🇨11:00:02"
                }
            }
        }
    }
]
进入 docker 容器
[root@test docker]# docker attach 58201fd4e8a6
停止运行的容器
[root@test docker]# docker stop 58201fd4e8a6
停止所有运行的容器
[root@test docker]# docker stop $(docker ps -q)
重启运行的容器
[root@test docker]# docker restart 58201fd4e8a6
重启所有运行的容器
[root@test docker]# docker restart $(docker ps -q)
删除容器
[root@test docker]# docker rm 58201fd4e8a6
删除所有容器
[root@test docker]# docker rm $(docker ps -aq)
[root@test docker]# docker stop $(docker ps -q) & docker rm $(docker ps -aq)
在容器内执行命令
[root@test docker]# docker exec -it 53414e2de4dc /bin/sh
# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
查看容器内的日志
[root@test docker]# docker logs 1f33977e8548
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

上一篇 docker 学习笔记(一)
docker 学习笔记目录
下一篇 docker 学习笔记(三)