docker基础命令
文章目录
docker pull 拉取镜像
在渡渡鸟镜像站 :https://docker.aityp.com/
[root@cwf ~]# docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:mainline-alpine3.18-perl
docker images 显示镜像列表
[root@cwf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx mainline-alpine3.18-perl 5d0cd28768c1 7 months ago 78.1MB
[root@cwf ~]#
显示所有镜像
使用选项 -a (--all) 列出本地所有镜像(含中间镜像层)
[root@cwf ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx mainline-alpine3.18-perl 5d0cd28768c1 7 months ago 78.1MB
[root@cwf ~]# docker images --all
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx mainline-alpine3.18-perl 5d0cd28768c1 7 months ago 78.1MB
[root@cwf ~]#
调整镜像信息
选项 --no-trunc 表示显示完整的镜像信息
[root@localhost ~]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginxinc/nginx-unprivileged 1.27-alpine sha256:5b7e2ee8c78d7a7d8410785cb72824a01e52605d2c15b38d0f8da54c8a0ce975 2 months ago 47MB
httpd bookworm sha256:bfe6700e67790a2c4a4ea7e55bf2ce823764a6c9f76d512418ed8a60bf8f1526 8 months ago 147MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/httpd bookworm sha256:bfe6700e67790a2c4a4ea7e55bf2ce823764a6c9f76d512418ed8a60bf8f1526 8 months ago 147MB
选项 -q (--quiet) 表示只要显示镜像ID
[root@cwf ~]# docker images -q
5d0cd28768c1
[root@cwf ~]# docker images --quiet
5d0cd28768c1
使用 --digests 选项将镜像的摘要显示出来
[root@cwf ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx mainline-alpine3.18-perl sha256:961005a67cf19ad3af9fdd8ac1866371b024bf4dfc762490aa64ec9da1d47fbe 5d0cd28768c1 7 months ago 78.1MB
显示镜像列表
.ID:镜像ID。
.Repository:镜像仓库名称。
.Tag:镜像标签。
.Digest:镜像摘要。
.CreatedSince:镜像创建以来的时长。
.CreatedAt:镜像创建的具体时间。
.Size:镜像硬盘占用空间
docker inspect查看镜像详细信息
格式
docker inspect --format='{{镜像}}'Ubuntu
[root@localhost ~]# docker inspect 5b7e
[
{
"Id": "sha256:5b7e2ee8c78d7a7d8410785cb72824a01e52605d2c15b38d0f8da54c8a0ce975",
"RepoTags": [
"nginxinc:1",
"swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginxinc/nginx-unprivileged:1.27-alpine"
],
"RepoDigests": [
"swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginxinc/nginx-unprivileged@sha256:ce8ea2f4879d11736fe5d12a966a5f03a79cec063b67a6915d278891a155e762"
],
"Parent": "",
"Comment": "buildkit.dockerfile.v0",
"Created": "2024-10-14T01:24:20.523389378Z",
"DockerVersion": "",
"Author": "",
"Config": {
"Hostname": "",
......
docker history 查看镜像的构建历史
[root@cwf ~]# docker history 5d0c
IMAGE CREATED CREATED BY SIZE COMMENT
5d0cd28768c1 7 months ago RUN /bin/sh -c set -x && apkArch="$(cat … 35.6MB buildkit.dockerfile.v0
<missing> 7 months ago RUN /bin/sh -c set -x && apkArch="$(cat … 30.7MB buildkit.dockerfile.v0
<missing> 7 months ago ENV NJS_VERSION=0.8.3 0B buildkit.dockerfile.v0
<missing> 7 months ago CMD ["nginx" "-g" "daemon off;"] 0B buildkit.dockerfile.v0
<missing> 7 months ago STOPSIGNAL SIGQUIT 0B buildkit.dockerfile.v0
<missing> 7 months ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 7 months ago ENTRYPOINT ["/docker-entrypoint.sh"] 0B buildkit.dockerfile.v0
<missing> 7 months ago COPY 30-tune-worker-processes.sh /docker-ent… 4.62kB buildkit.dockerfile.v0
<missing> 7 months ago COPY 20-envsubst-on-templates.sh /docker-ent… 3.02kB buildkit.dockerfile.v0
<missing> 7 months ago COPY 15-local-resolvers.envsh /docker-entryp… 336B buildkit.dockerfile.v0
<missing> 7 months ago COPY 10-listen-on-ipv6-by-default.sh /docker… 2.12kB buildkit.dockerfile.v0
<missing> 7 months ago COPY docker-entrypoint.sh / # buildkit 1.62kB buildkit.dockerfile.v0
<missing> 7 months ago RUN /bin/sh -c set -x && addgroup -g 101… 4.56MB buildkit.dockerfile.v0
<missing> 7 months ago ENV PKG_RELEASE=1 0B buildkit.dockerfile.v0
<missing> 7 months ago ENV NGINX_VERSION=1.25.4 0B buildkit.dockerfile.v0
<missing> 7 months ago LABEL maintainer=NGINX Docker Maintainers <d… 0B buildkit.dockerfile.v0
<missing> 8 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 8 months ago /bin/sh -c #(nop) ADD file:8729f9c0258836b64… 7.34MB
[root@cwf ~]#
docker save 将镜像导出到归档文件
[root@cwf ~]# docker save --output hello.tar 5d0c
[root@cwf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.elastic.co/logstash/logstash-oss 8.14.0 a901f80573eb 3 months ago 796MB
hellov2 1.0 5d0cd28768c1 7 months ago 78.1MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx mainline-alpine3.18-perl 5d0cd28768c1 7 months ago 78.1MB
docker load 从归档文件加载镜像
[root@cwf ~]# docker load --input hello.tar
Loaded image ID: sha256:5d0cd28768c128082724c83e84bfd410e706170ab47f717b353fab8f590049d7
[root@cwf ~]#
docker rmi 删除指定的镜像
方法一: docker rmi 镜像ID
方法二: docker rmi 镜像名+TAG
[root@localhost ~]# docker rmi cb8f
Untagged: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:alpine3.20
Untagged: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx@sha256:76684c43f32804f2601310a067fd5039f85bdf76bb23a477c2a7440f12b883fd
Deleted: sha256:cb8f91112b6b50ead202f48bbf81cec4b34c254417254efd94c803f7dd718045
Deleted: sha256:09f99be15f4ff5db64a85472139db4395c3c14e00c7a90f37499237625556ed4
Deleted: sha256:a3a02df44147c897aef503f952dfa230308fbb8a9bbb5ca28fac36b7b866d6d6
Deleted: sha256:1f47bf4b72792d8c5ea114da41dba55e908ad3b920e8076bec908c771b81d4b4
Deleted: sha256:415c1787dcb7fd187fa40535785c2b3d14d2920ea736ec2a745e1860e92b312a
Deleted: sha256:d0a43343a6d59e04b17a9cc72934e435f6609689259eb4e4adf5658de3eaf8e0
Deleted: sha256:79d58bb4d8ae0b5b8c8cd790f4a8ae29facd883297f357932d3a5266b4f4e30d
Deleted: sha256:adffcb7a535b6915444890e65729bc15929c8fd060c7b4641c57fff05c7f5999
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginxinc 1 5b7e2ee8c78d 2 months ago 47MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginxinc/nginx-unprivileged 1.27-alpine 5b7e2ee8c78d 2 months ago 47MB
nginx 2 cb8f91112b6b 2 months ago 47MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx alpine3.20 cb8f91112b6b 2 months ago 47MB
[root@localhost ~]# docker rmi nginxinc:1
Untagged: nginxinc:1
[root@localhost ~]# docker rmi nginx:2
Untagged: nginx:2
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginxinc/nginx-unprivileged 1.27-alpine 5b7e2ee8c78d 2 months ago 47MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx alpine3.20 cb8f91112b6b 2 months ago 47MB
docker run 运行镜像
-d 后台运行容器镜像
-it 标准化输出容器id
[root@localhost ~]# docker run -d cb8f
1f65b39480b34915dfaf070c6a6f1181d77011aa07ad99c7f1fa1e8a5224e9fa
[root@localhost ~]# docker run -d -it cb8f
a6a9df8d27ede07393564b8cc9be6a0275977f4f81ed705974cbce0f4e4521e4
docker ps 查看正在运行的容器
-a 所有容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6a9df8d27ed cb8f "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp awesome_jones
1f65b39480b3 cb8f "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp focused_chatterjee
4a75dbaed8a1 5b7e "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 8080/tcp hungry_lovelace
docker attach 连接正在运行的容器
[root@localhost ~]# docker attach a6a9
2024/12/23 08:12:39 [notice] 1#1: signal 28 (SIGWINCH) received
2024/12/23 08:12:39 [notice] 31#31: signal 28 (SIGWINCH) received
2024/12/23 08:12:39 [notice] 1#1: signal 28 (SIGWINCH) received
2024/12/23 08:12:39 [notice] 31#31: signal 28 (SIGWINCH) received
2024/12/23 08:12:39 [notice] 30#30: signal 28 (SIGWINCH) received
ctrl + c 退出容器
docker container prune 清理没有运行的容器
[root@localhost ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
a6a9df8d27ede07393564b8cc9be6a0275977f4f81ed705974cbce0f4e4521e4
1f65b39480b34915dfaf070c6a6f1181d77011aa07ad99c7f1fa1e8a5224e9fa
Total reclaimed space: 2.186kB
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a75dbaed8a1 5b7e "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 8080/tcp hungry_lovelace
docker tag 修改镜像名称和标签
[root@localhost ~]# docker tag swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:alpine3.20 nginx:2
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginxinc 1 5b7e2ee8c78d 2 months ago 47MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginxinc/nginx-unprivileged 1.27-alpine 5b7e2ee8c78d 2 months ago 47MB
nginx 2 cb8f91112b6b 2 months ago 47MB
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx alpine3.20 cb8f91112b6b 2 months ago 47MB
[root@localhost ~]#
标签:ago,nginx,months,基础,命令,镜像,docker,root
From: https://blog.csdn.net/2403_87303550/article/details/144734146