4.docker學習之鏡像

鏡像

咱們知道,咱們想在Windows操做系統上跑Linux,須要安裝一個虛擬機程序,而後下載一個Linux鏡像,在該虛擬機程序中建立一個虛擬機,並使用該鏡像安裝對應的Linux操做系統,安裝好以後,便可在Windows系統下跑虛擬機中的Linux系統。此時,咱們發現,這裏所說的鏡像,相似於操做系統的安裝包,這裏所提到的鏡像中包含了對應的操做系統。這是傳統鏡像的概念
Docker中,鏡像文件不會很大,有人說:「Docker中的每一個鏡像都包含了一個Ubuntu系統。」,但事實是這樣的嗎?若是鏡像中包含了操做系統,爲什麼有的鏡像文件如此小?若是鏡像中沒有包含操做系統,那麼爲什麼鏡像中又有對應的開發環境?因此說,Docker的鏡像很是神奇,經過研究發現,Docker的鏡像中並不會獨立包含一個完整的操做系統,而且鏡像中擁有對應的開發與運行環境。因此,使用Docker技術,能夠很方便實現開發環境的快速、批量部署。
 

獲取鏡像

要想使用Docker鏡像,最簡單的方法就是獲取別人已經作好的Docker鏡像,這個過程簡稱鏡像的獲取。
開啓docker
[root@206 ~]# systemctl start docker

下載鏡像node

[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]

篩選能夠自動化構建的python

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

篩選收藏數3以上的git

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

篩選級別爲2以上的github

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

顯示完整描述信息web

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

建立鏡像

建立鏡像的方式主要有3種:
一、根據已有的修改
二、使用Dockerfile
三、使用本地模版導入
 
查看本地鏡像
[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是容器的iddocker

咱們在運行的這個容器中進行修改ubuntu

root@23a9ebc23cc5:/# mkdir test

而後退出vim

root@23a9ebc23cc5:/# exit
將修改好的容器從新變成一個鏡像,記得加上以前的容器id,和新的images名稱版本
-m 描述信息
-a 用戶
[root@206 ~]# docker commit -m "this is a new images" -a "root" 23a9ebc23cc5 newubuntu:8888

查看鏡像,發現多了一個centos

[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

以後別人能夠下載該文件並恢復爲鏡像

相關文章
相關標籤/搜索