Docker:镜像命令和容器命令

文章目录

  • 镜像命令
    • [docker images](#docker images)
    • [docker image inspeact](#docker image inspeact)
    • [docker tag](#docker tag)
  • 容器命令
    • [docker run](#docker run)
    • [docker ps](#docker ps)

下面进入到关于镜像命令的学习中

镜像命令

docker images

这个命令的功能是列出本地的镜像

语法:

shell 复制代码
docker images [options] [repository[:tag]]

别名:

shell 复制代码
docker image ls, docker image list

参数选项:

  1. -a:表示列出本地所有镜像
  2. --digests:显示镜像的摘要信息
  3. -f:显示满足条件的镜像
  4. --format:指定返回值的模板文件
  5. --no-trunc:显示完整的镜像信息
  6. -q:只显示镜像id

具体使用:

shell 复制代码
root@VM-24-7-ubuntu:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   13 months ago   13.3kB

root@VM-24-7-ubuntu:~# docker images ubuntu
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

root@VM-24-7-ubuntu:~# docker images hello-world
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   13 months ago   13.3kB

docker image inspeact

显示镜像详细信息

shell 复制代码
docker image inspect [options] image [image...]

使用如下:

shell 复制代码
root@VM-24-7-ubuntu:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   13 months ago   13.3kB

root@VM-24-7-ubuntu:~# docker image inspect d2c94e258dcb
[
    {
        "Id": "sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:266b191e926f65542fa8daaec01a192c4d292bff79426f47300a046e1bc576fd"
        ],
        "Parent": "",
        "Comment": "buildkit.dockerfile.v0",
        "Created": "2023-05-02T16:49:27Z",
        "DockerVersion": "",
        "Author": "",
        "Config": {
            "Hostname": "",
            "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": [
                "/hello"
            ],
            "ArgsEscaped": true,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "/",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 13256,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/ff16204e50b936f729c2efe9e015aa67e27c554d14ddbefdd51cf371bca8341d/merged",
                "UpperDir": "/var/lib/docker/overlay2/ff16204e50b936f729c2efe9e015aa67e27c554d14ddbefdd51cf371bca8341d/diff",
                "WorkDir": "/var/lib/docker/overlay2/ff16204e50b936f729c2efe9e015aa67e27c554d14ddbefdd51cf371bca8341d/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

docker tag

标记本地镜像,将其归入某一仓库

shell 复制代码
docker tag source_image[:tag] target_image[:tag]

别名:

shell 复制代码
docker image tag

样例:

shell 复制代码
root@VM-24-7-ubuntu:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   13 months ago   13.3kB

root@VM-24-7-ubuntu:~# docker image tag hello-world myregistry.com/myhelloworld

容器命令

docker run

功能:创建一个新的容器并运行一个命令

shell 复制代码
docker run [options] image [command] [arg...]

例如:

shell 复制代码
docker container run

具体使用:

使用Docker镜像nginx:lastest在后台模式启动一个容器,并把容器命名为mynginx:

shell 复制代码
root@VM-24-7-ubuntu:~# docker run --name mynginx -d nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
09f376ebb190: Pull complete 
5529e0792248: Pull complete 
9b3addd3eb3d: Pull complete 
57910a8c4316: Pull complete 
7b5f78f21449: Pull complete 
b7923aa4e8a6: Pull complete 
785625911f12: Pull complete 
Digest: sha256:0f04e4f646a3f14bf31d8bc8d885b6c951fdcf42589d06845f64d18aec6a3c4d
Status: Downloaded newer image for nginx:latest
3c6ab83484c462da21a583eedd8d2544858034de6f30ada9eac1d10c6779b791

使用镜像nginx:lastest,以后台模式启动一个容器,把容器的80端口映射到主机的80端口,主机的目录/data映射到容器的/data

shell 复制代码
docker run -p 80:80 -v /data:/data -d nginx:latest

docker ps

功能:列出容器

shell 复制代码
docker ps

别名:

shell 复制代码
docker container ls
docker container list
docker container ps

实例:

shell 复制代码
root@VM-24-7-ubuntu:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                         PORTS     NAMES
3c6ab83484c4   nginx:latest   "/docker-entrypoint...."   About a minute ago   Up About a minute              80/tcp    mynginx
b9c61a19414e   hello-world    "/hello"                 About an hour ago    Exited (0) About an hour ago             upbeat_swartz
相关推荐
运维全栈笔记5 小时前
K8S部署Redis高可用全攻略:1主2从3哨兵架构实战
redis·docker·云原生·容器·架构·kubernetes·bootstrap
SCBAiotAigc7 小时前
2026.5.1:`DockerDesktop must be owned by an elevated account`错误的解决办法
人工智能·docker·具身智能
AI木马人7 小时前
9.人工智能实战:GPU 服务如何上 Kubernetes?从单机部署到 K8s + NVIDIA Device Plugin + HPA 的生产级改造
人工智能·容器·kubernetes
身如柳絮随风扬14 小时前
使用 Docker 部署禅道并实现自动化部署——从项目搭建到运维自动化的完整指南
运维·docker·自动化
eRTE XFUN15 小时前
docker下搭建redis集群
redis·docker·容器
一只小bit15 小时前
Docker 存储卷:本地文件与容器内部文件建立绑定关系
运维·docker·容器
都在酒里16 小时前
在公共服务器上构建 RK3588 SDK 的纯净 Docker 方案
运维·服务器·docker
jeCA EURG16 小时前
docker离线安装及部署各类中间件(x86系统架构)
docker·中间件·系统架构
身如柳絮随风扬16 小时前
使用 Docker 部署 Jenkins 并实现自动化部署 —— 从零到一的 CI/CD 实践指南
docker·自动化·jenkins
就叫飞六吧16 小时前
docker快速启动sqlserver实例并自动测试shell脚本
docker·容器·sqlserver