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
相关推荐
景天科技苑1 小时前
【云原生开发】K8S多集群资源管理平台架构设计
云原生·容器·kubernetes·k8s·云原生开发·k8s管理系统
wclass-zhengge2 小时前
K8S篇(基本介绍)
云原生·容器·kubernetes
颜淡慕潇2 小时前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
川石课堂软件测试4 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
昌sit!10 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
追风林11 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
A ?Charis13 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
城南vision13 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge13 小时前
Docker篇(Docker Compose)
运维·docker·容器
北漂IT民工_程序员_ZG14 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes