4.docker学习之镜像

镜像

我们知道,我们想在Windows操作系统上跑Linux,需要安装一个虚拟机程序,然后下载一个Linux镜像,在该虚拟机程序中创建一个虚拟机,并使用该镜像安装对应的Linux操作系统,安装好之后,即可在Windows系统下跑虚拟机中的Linux系统。此时,我们发现,这里所说的镜像,类似于操作系统的安装包,这里所提到的镜像中包含了对应的操作系统。这是传统镜像的概念
Docker中,镜像文件不会很大,有人说:“Docker中的每个镜像都包含了一个Ubuntu系统。”,但事实是这样的吗?如果镜像中包含了操作系统,为何有的镜像文件如此小?如果镜像中没有包含操作系统,那么为何镜像中又有对应的开发环境?所以说,Docker的镜像非常神奇,通过研究发现,Docker的镜像中并不会独立包含一个完整的操作系统,并且镜像中拥有对应的开发与运行环境。所以,使用Docker技术,可以很方便实现开发环境的快速、批量部署。
 

获取镜像

要想使用Docker镜像,最简单的方法就是获取别人已经做好的Docker镜像,这个过程简称镜像的获取。
开启docker
[root@206 ~]# systemctl start docker

下载镜像

[root@206 ~]# docker pull ubuntu:17.10
17.10: Pulling from library/ubuntu
06d6d7dd14f0: Pull complete 
7afd309907db: Pull complete 
151009f8900b: Pull complete 
36547d3d8f4e: Pull complete 
320476e1abe2: Pull complete 
Digest: sha256:0fda3973dca01bb6797c8f84f7728730e3760fff0360b213bb1a8f2a65492967
Status: Downloaded newer image for ubuntu:17.10
他下载的镜像地址是https://hub.docker.com/  17.10是ubuntu的版本号
一个镜像是由多层组成的,所以他下载了好几个层
 
如果中途提示被其他客户端占用,导致无法下载,就重启docker  systemctl stop docker
然后重新获取镜像
 

搜索镜像

所谓镜像搜寻,即镜像搜索。如果我们想在docker index中搜索Docker的镜像,那么我们可以使用镜像搜索的功能。
搜索所有名称包含python的镜像
automated 是否能自动化构建
[root@206 ~]# docker search python
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
python                         Python is an interpreted, interactive, obj...   1843      [OK]       
kaggle/python                  Docker image for Python scripts run on Kaggle   62                   [OK]
google/python                  Please use gcr.io/google-appengine/python ...   35                   [OK]
vimagick/python                mini python                                     3                    [OK]
dockershelf/python             Repository for docker images of Python. Te...   3                    [OK]
pandada8/alpine-python         An alpine based python image                    3                    [OK]
beevelop/nodejs-python         Node.js with Python                             2                    [OK]
lucidfrontier45/python-uwsgi   Python with uWSGI                               2                    [OK]
tsuru/python                   Image for the Python platform in tsuru PaaS.    2                    [OK]
colstrom/python                Docker on Python, with pip!                     1                    [OK]
webhippie/python               Docker images for python                        1                    [OK]
lcgc/python                    The base image for applications written in...   1                    [OK]
orbweb/python                  Python image                                    1                    [OK]
1science/python                Python Docker images based on Alpine Linux      1                    [OK]
allanlei/python                Python Images                                   0                    [OK]
croscon/python                 Python image for Croscon                        0                    [OK]
kirigamico/python              Base Docker image which contains Python, G...   0                    [OK]
ceecko/python                  Python image                                    0                    [OK]
bynd/python                    Debian-based Python image                       0                    [OK]
samtayuk/python                Python with bower, less and sass                0                    [OK]
funkygibbon/python             Minimal python based on funkygibbon/ubuntu      0                    [OK]
1maa/python                    Python images                                   0                    [OK]
panubo/python                  Latest python versions built from source        0                    [OK]
mediadesignpractices/python    Base python build with useful preinstalls       0                    [OK]
codekoalas/python              Runs python scripts every 5 minutes after ...   0                    [OK]

筛选可以自动化构建的

[root@206 ~]# docker search --automated  python

筛选收藏数3以上的

[root@206 ~]# docker search -s 3  python

筛选级别为2以上的

[root@206 ~]# docker search --stars=2  python

显示完整描述信息

[root@206 ~]# docker search --no-trunc  python

创建镜像

创建镜像的方式主要有3种:
1、根据已有的修改
2、使用Dockerfile
3、使用本地模版导入
 
查看本地镜像
[root@206 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              17.10               073e7b409b9b        36 hours ago        89.6 MB
方法一:基于已有的镜像,创建一个新的镜像
   基于ubuntu:17.10镜像运行一个容器
[root@206 ~]# docker run -ti ubuntu:17.10 /bin/bash
root@23a9ebc23cc5:/#

23a9ebc23cc5是容器的id

我们在运行的这个容器中进行修改

root@23a9ebc23cc5:/# mkdir test

然后退出

root@23a9ebc23cc5:/# exit
将修改好的容器重新变成一个镜像,记得加上之前的容器id,和新的images名称版本
-m 描述信息
-a 用户
[root@206 ~]# docker commit -m "this is a new images" -a "root" 23a9ebc23cc5 newubuntu:8888

查看镜像,发现多了一个

[root@206 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newubuntu           8888                036d21b62a12        2 minutes ago       89.6 MB
ubuntu              17.10               073e7b409b9b        37 hours ago        89.6 MB
方法二:本地模板中导入镜像
模板下载地址
比如我们随便在该网页找了一个,然后复制链接地址
[root@206 ~]# wget http://download.openvz.org/template/precreated/suse-13.1-x86-minimal.tar.gz

生成镜像

[root@206 ~]# cat suse-13.1-x86-minimal.tar.gz | docker import - newsuse:99999

看一下,又多了一个

[root@206 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newsuse             99999               7e4ff5f67461        4 seconds ago       156 MB
newubuntu           8888                036d21b62a12        19 minutes ago      89.6 MB
ubuntu              17.10               073e7b409b9b        37 hours ago        89.6 MB

方法三:使用Dockerfile

[root@206 test]# pwd
/test
[root@206 test]# touch Dockerfile
FROM newubuntu:8888
MAINTAINER root
RUN touch a.txt
RUN mkdir test1
注意,Dockerfile这个文件名固定的.
运行这个文件,也可以-t="newubuntu:8089" 加上版本号信息
[root@206 test]# docker build -t="newubuntuutu1" /test
[root@206 test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newubuntuutu1       latest              3d077336fcf6        5 seconds ago       89.6 MB
newsuse             99999               7e4ff5f67461        16 minutes ago      156 MB
newubuntu           8888                036d21b62a12        36 minutes ago      89.6 MB
ubuntu              17.10               073e7b409b9b        37 hours ago        89.6 MB

删除镜像

[root@206 test]# docker rmi newubuntuutu1

没有指定tag的时候,删除的是tag为latest的镜像,如果有个名为newubuntuutu1但是tag是其他的时候,这个镜像是不能删除的,除非删除的时候指定tag

[root@206 test]# docker rmi newsuse:99999
如果有容器正在使用该镜像,应该先关闭容器,然后删除镜像
.比如我们先启动一个容器
[root@206 test]# docker run -ti newubuntuutu1:latest /bin/bash

然后删除这个镜像,提示不能删除

[root@206 test]# docker rmi  newubuntuutu1:latest
Error response from daemon: conflict: unable to remove repository reference "newubuntuutu1:latest" (must force) - container e9a6eeb12f6e is using its referenced image 3d077336fcf6

我们需要先关闭容器 容器id为e9a6eeb12f6e我们只需要写前四位

[root@206 test]# docker rm e9a6

然后再执行docker rmi  newubuntuutu1:latest即可

镜像信息的查看

[root@206 test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              cbf64eb48a64        18 minutes ago      89.6 MB
newsuse             99999               7e4ff5f67461        32 minutes ago      156 MB
newubuntu           8888                036d21b62a12        52 minutes ago      89.6 MB
ubuntu              17.10               073e7b409b9b        37 hours ago        89.6 MB
REPOSITORY 仓库名
TAG 版本号
IMAGE ID 镜像id号

镜像的存与载

镜像的存出也叫做导出,即镜像备份,会是把现在在操作系统中运行的镜像备份一份出来。
镜像的载入也叫做导入,即镜像备份恢复,会把之前备份的镜像备份文件重新导入进操作系统运行
备份
[root@206 test]# docker save -o ubuntu-bak.tar ubuntu:17.10
[root@206 test]# ls
Dockerfile  ubuntu-bak.tar

删除这个镜像

[root@206 test]# docker rmi ubuntu:17.10

载入

[root@206 test]# docker load --input ubuntu-bak.tar 
Loaded image: ubuntu:17.10

写时复制机制

写时复制机制(copy-on-write),是一种在Linux中引入的只有进程空间的各段内容要发生变化时,才把父进程的内容复制给子进程的一种处理机制。

要创建一个子进程,通常的做法是

 

 写时复制机制(copy-on-write)中,通常情况的做法是(只要父进程相应段不改变,就这么做):

 

 写时复制机制(copy-on-write)中,当父进程相应段要更改,才:

 

 Docker中,采用写时复制机制来创建根文件系统,这样的话不会导致庞大的内存的开销,所以可以节省大量内存和硬盘空间,可以让Docker的部署更方便快捷。

docker hub

 Docker Hub是Docker官方维护的一个远程Docker仓库,这个地方主要用来存储一些公开发布的镜像。我们可以使用Docker Hub来找到很多别人做好并发布在Docker Hub的镜像来使用,或者,我们也可以把自己做的镜像公开发布出去,并且存储在Docker Hub。
Docker Hub的网址是:https://hub.docker.com/
我们之前学过的docker search命令、docker pull命令,都是在Docker Hub中进行操作的
Docker Hub非常方便,但是由于Docker Hub在国外(我们可以看一下),所以其对于我们国内的用户来说,有时传输速度不如国内的服务器快。那么怎么解决慢这个问题呢?
解决的方法不止一个,我们在此给大家讲解解决方案之一,使用Docker Mirror来解决。是这样的:
       当我们获取一个镜像的时候,首先尝试从Docker Mirror服务器(Docker Mirror服务器上会缓存很多Docker镜像)上获取,假如获取到,由于服务器在国内,那么速度将会快很多,假如获取不         到,自动从国外的Docker Hub中获取。所以,不会影响使用者正常的获取镜像操作,使用了Docker Mirror后的速度只会>=原速度。 
 
https://www.daocloud.io/提供了一个docker Mirror的服务
注册完之后点击这里的加速器

 

 

 

[root@206 test]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://69e9963d.m.daocloud.io

镜像分发

 

所谓镜像的分发,简单地来理解,即是镜像的发布。比如,您创建了一个自己的镜像,想发布出去,给别人使用,那么这个过程就叫做镜像的分发
 
广义上镜像的分发主要包括两种方式:
   1.把镜像对应的压缩文件发布出去(比如github、网盘、自己的网络服务器等),别人可以下载该文件并恢复为镜像。
   2.把镜像上传到仓库中,供别人使用。其中上传到的仓库类型包括公有仓库和私有仓库两种

 

安装git,并配置密钥
[root@206 test]# yum -y install git
[root@206 test]# ssh-keygen -t rsa -C "centos"
[root@206 test]# cd ~/.ssh/
[root@206 .ssh]# ls
id_rsa  id_rsa.pub

复制id_rsa.pub的内容,并打开github 的setting配置

 

 

 

  github新建一个仓库,拉取到本地

 

[root@206 /]# git clone git@github.com:itliucheng/docker-test1.git

将之前save的tar文件放入

[root@206 /]# cp /test/ubuntu-bak.tar docker-test1/

提交到github

[root@206 docker-test1]# git add ubuntu-bak.tar
[root@206 docker-test1]# git commit -m "add a images"
[root@206 docker-test1]# git push origin master

之后别人可以下载该文件并恢复为镜像

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted on 2017-05-24 15:12  itliucheng  阅读(980)  评论(1编辑  收藏  举报