实验环境
CentOS 7.5
安装并启动 docker
- yum install -y docker
- systemctl start docker
镜像
安装镜像
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
注: 对于 Docker 镜像来说, 如果不显式地指定 TAG, 则默认会选择 latest 标签, 即下载仓库中最新版本的镜像.
从 Docker Hub 的 CentOS 仓库下载一个最新的 CentOS 操作系统的镜像.
- [root@kvm ~]# docker pull CentOS
- Using default tag: latest
- Trying to pull repository docker.io/library/CentOS ...
- latest: Pulling from docker.io/library/CentOS
- a02a4930cb5d: Pull complete
- Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
- Status: Downloaded newer image for docker.io/CentOS:latest docker pull CentOS
- Using default tag: latest
- Trying to pull repository docker.io/library/CentOS ...
- latest: Pulling from docker.io/library/CentOS
- a02a4930cb5d: Pull complete
- Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
- Status: Downloaded newer image for docker.io/CentOS:latest docker pull CentOS
- Using default tag: latest
- Trying to pull repository docker.io/library/CentOS ...
- latest: Pulling from docker.io/library/CentOS
- a02a4930cb5d: Pull complete
- Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
- Status: Downloaded newer image for docker.io/CentOS:latest
注: 该命令实际上下载的就是 CentOS:latest 镜像, 目前最新的 7.6.1810 版本的镜像.
从 Docker Hub 的 CentOS 仓库下载一个指定版本的 CentOS 操作系统的镜像.
- [root@kvm ~]# docker pull CentOS:7.3.1611
- Trying to pull repository docker.io/library/CentOS ...
- 7.3.1611: Pulling from docker.io/library/CentOS
- b8e0383d5f94: Pull complete
- Digest: sha256:6fe8d484f2897e2ea75f496bfb6792b539baf3e56880fce3b9b6505d76e266dd
- Status: Downloaded newer image for docker.io/CentOS:7.3.1611
查看镜像信息
列出本地主机上已有的镜像
- [root@kvm ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- docker.io/CentOS latest 1e1148e4cc2c 7 weeks ago 202 MB
- docker.io/CentOS 7.3.1611 1e20acd39495 3 months ago 192 MB
各字段释义
REPOSITORY: 来自于哪个仓库, 比如 CentOS 仓库;
TAG: 镜像的标签信息, 比如 7.3.1611;
IMAGE ID: 镜像的 ID 号(唯一);
CREATED: 创建时间;
SIZE: 镜像大小.
创建镜像
创建镜像的方法主要有三种: 基于已有镜像的容器创建, 基于本地模板导入, 基于 Dockerfile 创建; 这里着重介绍前两种.
基于已有镜像的容器创建
该方法实现创建的命令行格式
- docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- [root@kvm ~]# docker commit --help
- Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- Create a new image from a container's changes
- Options:
- [root@kvm ~]# docker run -t -i docker.io/CentOS:7.3.1611 /bin/bash
- [root@90d3b762f848 /]# touch test.txt
- [root@90d3b762f848 /]# exit
- [root@kvm ~]# docker commit -m "add a file named test.txt" -a "docker new" 90d3b762f848 www.wholj.com:7.3
- sha256:3a51b6661c9f9c31ec8239405535cc57f782972f6eedf8452e2d8f4bce0bc2d6
- [root@kvm ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- www.wholj.com 7.3 3a51b6661c9f 7 seconds ago 192 MB
- docker.io/CentOS latest 1e1148e4cc2c 7 weeks ago 202 MB
- docker.io/CentOS 7.3.1611 1e20acd39495 3 months ago 192 MB
- [root@kvm /home/tools/ISO]# docker import --help
- Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
- Import the contents from a tarball to create a filesystem image
- Options:
- -c, --change list Apply Dockerfile instruction to the created image (default [])
- [root@kvm /home/tools/ISO]# cat CentOS-7-x86_64-minimal.tar.gz | docker import - www.wholj.com:7.5
- sha256:4950a54ede5a5c0da704c6f74e6bcc43d440e83260b0752a926325035435a7dc
- [root@kvm /home/tools/ISO]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- www.wholj.com 7.5 4950a54ede5a 10 seconds ago 435 MB
- www.wholj.com 7.3 3a51b6661c9f 2 days ago 192 MB
- docker.io/CentOS latest 1e1148e4cc2c 7 weeks ago 202 MB
- docker.io/CentOS 7.3.1611 1e20acd39495 3 months ago 192 MB
- docker save [OPTIONS] IMAGE [IMAGE...]
- [root@kvm ~]# docker save --help
- Usage: docker save [OPTIONS] IMAGE [IMAGE...]
- Save one or more images to a tar archive (streamed to STDOUT by default)
- Options:
- [root@kvm ~]# docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- www.wholj.com 7.3 3a51b6661c9f About an hour ago 192 MB
- docker.io/CentOS latest 1e1148e4cc2c 7 weeks ago 202 MB
- docker.io/CentOS 7.3.1611 1e20acd39495 3 months ago 192 MB
- [root@kvm ~]# ll
- total 8
- -rw-------. 1 root root 2090 Nov 30 11:33 anaconda-ks.cfg
- -rw-------. 1 root root 1955 Nov 30 11:33 original-ks.cfg
- [root@kvm ~]# docker save -o centos_7.6.tar docker.io/CentOS:latest
- [root@kvm ~]# ll
- total 205272
- -rw-------. 1 root root 2090 Nov 30 11:33 anaconda-ks.cfg
- -rw------- 1 root root 210186752 Jan 25 17:17 centos_7.6.tar
- -rw-------. 1 root root 1955 Nov 30 11:33 original-ks.cfg
- [root@kvm ~]#
- docker load [OPTIONS]
- [root@kvm ~]# docker load --help
- Usage: docker load [OPTIONS]
- Load an image from a tar archive or STDIN
- Options:
- [root@kvm ~]# docker load -i ./centos_7.6.tar
- Loaded image: docker.io/CentOS:latest
- [root@kvm ~]# docker load < ./centos_7.6.tar
- Loaded image: docker.io/CentOS:latest
来源: http://www.bubuko.com/infodetail-2936816.html