1. 程式人生 > 實用技巧 >三、docker的映象操作

三、docker的映象操作

docker的映象操作

一、搜尋docker映象

1、搜尋官方映象

[root@inode3 ~]# docker search nginx

使用官方映象建議選這OFFICIAL或者STARS星多的映象

2、在第三方網站搜尋映象檔案

https://hub.docker.com/

二、docker映象下載

獲取映象
docker pull nginx
docker pull centos:6.9
如果在獲取映象時,沒有指定版本,預設是最新版本。如:nginx:latest   centos:latest(現在centos最新為centos8)

如下pull一個centos6.9的映象

[root@inode3 ~]# docker pull centos:6.9
6.9: Pulling from library/centos 831490506c47: Pull complete Digest: sha256:6fff0a9edc920968351eb357c5b84016000fec6956e6d745f695e5a34f18ecd2 Status: Downloaded newer image for centos:6.9 docker.io/library/centos:6.9

三、docker映象的檢視

檢視當前本地有些什麼映象檔案
[root@inode3 ~]# docker images 
[root@inode3 ~]# docker image ls

四、docker映象檔案的匯出

1、匯出單個映象檔案

匯出nginx的映象檔案到/mnt下
[root@inode3 ~]# docker image save nginx:latest > /mnt/docker_nginx.tar.gz
[root@inode3 ~]# ll /mnt
total 127492
-rw-r--r-- 1 root root 130548224 Mar  4 16:54 docker_nginx.tar.gz

2、同時匯出多個映象檔案為一個檔案

同時匯出nginx和centos6.9
[root@inode3 ~]# docker image save -o /mnt/docker_nginx_centos.tar
.gz nginx:latest centos:6.9 [root@inode3 ~]# ll /mnt total 452660 -rw------- 1 root root 332968448 Mar 4 16:57 docker_nginx_centos.tar.gz -rw-r--r-- 1 root root 130548224 Mar 4 16:54 docker_nginx.tar.gz

五、刪除映象檔案

刪除nginx映象檔案
[root@inode3 ~]# docker image rm nginx:latest

六、映象匯入

重新匯入nginx映象檔案
[root@inode3 ~]# docker image load -i /mnt/docker_nginx.tar.gz 
f2cb0ecef392: Loading layer  72.48MB/72.48MB
fe08d5d042ab: Loading layer  58.04MB/58.04MB
318be7aea8fc: Loading layer  3.584kB/3.584kB
Loaded image: nginx:latest

刪除nginx和centos6.9的映象,匯入剛才匯出的docker_nginx_centos.tar.gz
[root@inode3 ~]# docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
ansible/centos7-ansible   latest              688353a31fde        3 years ago         447MB

映象匯入
[root@inode3 ~]# docker image load -i /mnt/docker_nginx_centos.tar.gz 
f2cb0ecef392: Loading layer [==========================================>] 72.48MB/72.48MB
fe08d5d042ab: Loading layer [==========================================>] 58.04MB/58.04MB
318be7aea8fc: Loading layer [==========================================>] 3.584kB/3.584kB
Loaded image: nginx:latest
aaa5621d7c01: Loading layer [==========================================>] 202.4MB/202.4MB
Loaded image: centos:6.9

[root@inode3 ~]# docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
nginx                     latest              a1523e859360        6 days ago          127MB
centos                    6.9                 2199b8eb8390        11 months ago       195MB
ansible/centos7-ansible   latest              688353a31fde        3 years ago         447MB

七、檢視映象資訊

[root@inode3 ~]# docker image inspect nginx:latest
[
    {
        "Id": "sha256:a1523e859360df9ffe2b31a8270f5e16422609fe138c1636383efdc34b9ea2d6",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2020-02-26T20:02:15.724396212Z",
        "Container": "a7e44d480f0a875093e5f531cae42c2f24fc277d2ba4089fd8464f25eda77825",
        "ContainerConfig": {
            "Hostname": "a7e44d480f0a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.8",
                "NJS_VERSION=0.3.8",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
.....

八、更改映象的名稱

格式:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

[root@inode3 ~]# docker tag  nginx:latest nginx_1:v1
[root@inode3 ~]# docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
nginx                     latest              a1523e859360        6 days ago          127MB
nginx_1                   v1                  a1523e859360        6 days ago          127MB
centos                    6.9                 2199b8eb8390        11 months ago       195MB
ansible/centos7-ansible   latest              688353a31fde        3 years ago         447MB

九、映象加速

由於眾所周知的原因,國內從 Docker Hub 拉取映象速度緩慢,甚至中斷,此時可以配置Docker加速器(國內映象)來解決此問題。國內很多雲服務商都提供了國內加速器服務,主要分為兩種,一種需要建立登入賬號,另一種則不需要建立登入賬號。

需要登入賬號

大致等同於阿里雲的Docker加速器,主要環節也就是以下三步:

1. 註冊賬號,獲得專屬的映象加速地址。
2. 根據提示和系統型別,升級、配置並重啟Docker。
3. 實際操作,驗證配置是否正確。

不需要登入賬號(支援匿名pull)

對於使用systemd的系統(Ubuntu 16.04+、Debian 8+、CentOS 7+),可以建立 /etc/docker/daemon.json檔案,並寫入如下內容:

cat >> /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://dockerhub.azk8s.cn",
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com"
  ]
}
EOF
#可以同時配置多個映象加速器,加速器之間需要使用","分離
#重啟docker
systemctl daemon-reload
systemctl restart docker
一些支援匿名pull的國內映象
Docker-CN: https://registry.docker-cn.com
中科大映象: https://docker.mirrors.ustc.edu.cn
Azure中國映象: https://dockerhub.azk8s.cn

阿里雲的docker映象加速器需要去阿里雲官網申請賬號,在產品與服務---彈性計算---容器映象服務---映象加速器