docker - 常见操作

镜像相关

查找镜像

docker seach <镜像名>

image


拉取镜像

docker pull <镜像名>

image


查看镜像列表

docker image ls

image


制作镜像

构建镜像(根据dockerfile)

docker built -t <仓库地址>/<镜像名>[:tag] <path>

  • tag 不填默认为 latest
  • path 为 dockerfile 及所需文件目录
docker build -t centos/nginx .                           # tag不填, 版本为latest; .为根据当前目录创建镜像
docker build -t centos/nginx:0.0.2 git@github.com/xxx/xx # 可以指定git仓库的dockerfile地址

# 忽略构建缓存, --no-cache 关键字
docker build --no-cache -t centos/nginx:0.0.3 .

打标签

  • docker tag <本地镜像名称> <仓库地址>/<命名空间名/项目名>/<仓库名/应用名>[:版本]
  • 如果只在本地使用,不需要推送到仓库,则不需要指定仓库地址和项目名称,写一个应用名和版本自己能看懂就行了
  • 示例
    • 仓库地址registry.cn-hangzhou.aliyuncs.com
    • 命名空间名称(项目名)vick-epoch
    • 仓库名称(应用名)epoch-message
    • 版本0.1
    • 本地镜像名称是 epoch-message:latest
    • 执行的命令:docker tag epoch-message:latest epoch- registry.cn-hangzhou.aliyuncs.com/vick-epoch/epoch-message:0.1

推送镜像

  • 先登录
    • docker login -u <username> -p <password> <server> 直接指定用户名密码
    • docker login -u <username> --password-stdin <server> 交互指定
  • 按照打好的标签进行推送:docker push epoch registry.cn-hangzhou.aliyuncs.com/vick-epoch/epoch-message:0.1

镜像导出导入

  • 首先,需要从已启动的容器中导出,其次导入的镜像
  • 导出:docker export <容器名/容器ID> <文件名>
  • 导入:docker import <文件名> <镜像名>[:tag]

删除镜像

docker rmi <镜像名>

image


容器相关

创建

  • docker create创建的容器,未启动时状态为created
  • 使用docker run创建容器相当于执行docker create + docker start

image


标签

  • 打标签:docker run -itd --name bzbx --label foo=boo busybox

  • 查看标签(使用docker inspect

    • 使用jq:docker inspect bzbx | jq .[0].Config.Labels
    • 使用自带的格式化:docker inspect --format '{{ .Config.Labels }}' bzbx
  • 示例
    image


查看

查看容器列表

docker ps [-a]

  • -a 指定所有容器,包括停止的,默认只显示正在运行的

image


查看指定容器性能

docker top <容器名/容器ID>

image


查看日志

docker logs <容器名/容器ID>

  • 该日志是展示到终端前台的日志

查看指定容器详情

docker inspect <容器名/容器ID>

  • 能查看标签网络(cidr、ip地址、网络模式等)
查看 inspect 示例
[root@master ~]# docker inspect bzbx
[
    {
        "Id": "d2e7f76b4f08f2c09ef27cfa585eb2e6be8f5cb32330160ef156b31df98b8281",
        "Created": "2024-06-20T12:34:24.031881701Z",
        "Path": "sh",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6138,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2024-06-20T12:34:24.226575104Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a",
        "ResolvConfPath": "/root/docker/containers/d2e7f76b4f08f2c09ef27cfa585eb2e6be8f5cb32330160ef156b31df98b8281/resolv.conf",
        "HostnamePath": "/root/docker/containers/d2e7f76b4f08f2c09ef27cfa585eb2e6be8f5cb32330160ef156b31df98b8281/hostname",
        "HostsPath": "/root/docker/containers/d2e7f76b4f08f2c09ef27cfa585eb2e6be8f5cb32330160ef156b31df98b8281/hosts",
        "LogPath": "/root/docker/containers/d2e7f76b4f08f2c09ef27cfa585eb2e6be8f5cb32330160ef156b31df98b8281/d2e7f76b4f08f2c09ef27cfa585eb2e6be8f5cb32330160ef156b31df98b8281-json.log",
        "Name": "/bzbx",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "bridge",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                36,
                145
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "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",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": [],
            "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",
                "/sys/devices/virtual/powercap"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/root/docker/overlay2/ca07a66cdfa859e9e71e1826c3936508402aba3cea9d97606fc3ac4b2ccae292-init/diff:/root/docker/overlay2/b4972fa9f2056af1b1247e00d05933cd0fb1ef38a1ccb17214cba765b872ce99/diff",
                "MergedDir": "/root/docker/overlay2/ca07a66cdfa859e9e71e1826c3936508402aba3cea9d97606fc3ac4b2ccae292/merged",
                "UpperDir": "/root/docker/overlay2/ca07a66cdfa859e9e71e1826c3936508402aba3cea9d97606fc3ac4b2ccae292/diff",
                "WorkDir": "/root/docker/overlay2/ca07a66cdfa859e9e71e1826c3936508402aba3cea9d97606fc3ac4b2ccae292/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "d2e7f76b4f08",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "sh"
            ],
            "Image": "busybox",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "foo": "boo"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "84b829f06ab02cc3a73259d589e77d862869566431a55a2843d34a582264a882",
            "SandboxKey": "/var/run/docker/netns/84b829f06ab0",
            "Ports": {},
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "32398186e56487935760b819eb7ef7dc298785b0995b9daa04e8c06c779301b2",
            "Gateway": "192.168.1.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "192.168.1.2",
            "IPPrefixLen": 24,
            "IPv6Gateway": "",
            "MacAddress": "02:42:c0:a8:01:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "MacAddress": "02:42:c0:a8:01:02",
                    "NetworkID": "294a21530edca9ba75badb359a284c7892d0b7493ebf37fe9b3d5f7e61860270",
                    "EndpointID": "32398186e56487935760b819eb7ef7dc298785b0995b9daa04e8c06c779301b2",
                    "Gateway": "192.168.1.1",
                    "IPAddress": "192.168.1.2",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": null
                }
            }
        }
    }
]

操作

进入容器操作

使用 -it参数创建伪终端,指定进入的容器和使用的命令

image


宿主机和容器复制

  • 容器 --> 宿主机:docker cp <容器名>:<容器内路径> <宿主机路径>
  • 宿主机 --> 容器:docker cp <宿主机路径> <容器名>:<容器内路径>

image


删除

docker rm <容器名/容器ID>


停止

docker stop <容器名/容器ID>


系统信息相关

查看磁盘占用 - docker system df

image


实时监控事件 - docker system events

image


查看系统信息 - docker system info

docker info结果相同

点击查看代码
[root@master ~]# docker info
Client: Docker Engine - Community
 Version:    26.1.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.14.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.27.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 26.1.3
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 8b3b7ca2e5ce38e8f31a34f35b2b68ceb8470d89
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 5.4.277-1.el7.elrepo.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 3.694GiB
 Name: master
 ID: 5c5b05ac-f6b5-4a2d-a96f-0f039f576469
 Docker Root Dir: /root/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://oylk2x3t.mirror.aliyuncs.com/
 Live Restore Enabled: false

清理资源 - docker system prune

  • -a:清理所有未使用的镜像
  • --filter=k=v:按照标签清理
  • -f:强制清理,不弹出二次确认
  • --volumes:清理无用的卷

image

posted @ 2024-06-20 13:29  难德糊涂  阅读(52)  评论(0)    收藏  举报