docker 容器镜像操作命令

▶ docker commit

docker 能将 读写层 的内容制作成镜像。所以这就需要有运行着的镜像。

命令:

[root@server ~]# docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Aliases:
  docker container commit, docker commit

Options:
  -a, --author string    Author (e.g., "John
                         Hannibal Smith
                         <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile
                         instruction to the
                         created image
  -m, --message string   Commit message
  -p, --pause            Pause container during
                         commit (default true

▷ docker commit 具体使用

[root@server ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED       STATUS       PORTS     NAMES
02eb36863db9   httpd     "httpd-foreground"   11 days ago   Up 3 hours   80/tcp    gracious_germain

此时有个正在运行的容器,该容器的 ID 是 02eb36863db9

[root@server ~]# docker commit 02eb36863db9 mymakehttpd:lates
sha256:cd018a9ca87d940b8f2e5848edfb407f1cbc0eba10f296c7e6ea5e2173d7847b

使用 docker commit CONTAINER-ID REPOSITORY:TAG 就能生成一个基于容器的镜像了。

该镜像的容器名字必须都是小写,不然制作镜像的时候会有 must be lowercase 这个问题。

[root@server ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
mymakehttpd   lates     cd018a9ca87d   15 seconds ago   148MB

通过 docker images 可以查看到已经制作的镜像了。


▶ docker save

这个命令能将存在的 docker镜像 导出成一个 tar 归档文件。

命令:

[root@server ~]# docker save --help

Usage:  docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Aliases:
  docker image save, docker save

Options:
  -o, --output string   Write to a file, instead of STDOUT

▷ docker save 具体使用

[root@server ~]# docker save -o thesaveubuntu.tar ubuntu:latest

[root@server ~]# ll
-rw-------. 1 root root  80630784 Oct  9 02:07 thesaveubuntu.tar

通过 save 选项,tar 格式文件归档文件就形成了。

对于其他主机来说就能使用这个 tar 包了,对于这个 tar 包的使用会使用到 load 这个选项。


▶ docker load

将其他人分享的镜像导入到本地,这通常是容器镜像分发的方式之一。

命令

[root@server ~]# docker load --help

Usage:  docker load [OPTIONS]

Load an image from a tar archive or STDIN

Aliases:
  docker image load, docker load

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

▷ docker load 具体使用

[root@server ~]# docker load -i thesaveubuntu.tar

▶ docker export

把正在运行的容器导出。

命令

[root@server ~]# docker export -h
Flag shorthand -h has been deprecated, use --help

Usage:  docker export [OPTIONS] CONTAINER

Export a container's filesystem as a tar archive

Aliases:
  docker container export, docker export

Options:
  -o, --output string   Write to a file, instead of STDOUT 

▷ docker expoet 具体使用

[root@server ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED       STATUS       PORTS     NAMES
02eb36863db9   httpd     "httpd-foreground"   11 days ago   Up 3 hours   80/tcp    gracious_germain

[root@server ~]# docker export -o httpd.tar 02eb36863db9

这样就 export 出来了。

[root@server ~]# ll
-rw-------. 1 root root 150238720 Oct  9 02:22 httpd.tar

▶ docker import

导入使用docker export导入的容器做为本地容器镜像。

命令

[root@server ~]# docker import -h
Flag shorthand -h has been deprecated, use --help

Usage:  docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

Aliases:
  docker image import, docker import

Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

▷ docker import 具体使用

[root@server ~]# docker import httpd.tar httpd:v1
posted @ 2024-10-26 20:59  takenika  阅读(34)  评论(0)    收藏  举报