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

镜像命令

docker images

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

语法:

docker images [options] [repository[:tag]]

别名:

docker image ls, docker image list

参数选项:

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

具体使用:

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

显示镜像详细信息

docker image inspect [options] image [image...]

使用如下:

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

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

docker tag source_image[:tag] target_image[:tag]

别名:

docker image tag

样例:

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

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

docker run [options] image [command] [arg...]

例如:

docker container run

具体使用:

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

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

docker run -p 80:80 -v /data:/data -d nginx:latest

docker ps

功能:列出容器

docker ps

别名:

docker container ls
docker container list
docker container ps

实例:

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
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐