Docker image 鏡像介紹

操做鏡像

使用 docker 命令行操做 docker 鏡像php

獲取鏡像

使用「docker pull +鏡像名稱」從網絡上下載image鏡像mysql

core@localhost ~ $ docker pull

Usage: docker pull NAME[:TAG]

Pull an image or a repository from the registry

core@localhost ~/php $ docker  pull ubuntu
#pull +鏡像的名稱會下載該鏡像集的laste tag的鏡像
Pulling repository ubuntu
2185fd50e2ca: Pulling dependent layers
9cbaf023786c: Pulling dependent layers
3db9c44f4520: Pulling dependent layers
a9561eb1b190: Pulling dependent layers
195eb90b5349: Pulling dependent layers
463ff6be4238: Pulling dependent layers
c5881f11ded9: Pulling dependent layers
511136ea3c5a: Download complete
97fd97495e49: Downloading [=====>                                             ] 6.868 MB/67.5 MB 10m20s
.....
core@localhost ~/php $ docker  pull ubuntu:14.04
#表示下載該鏡像集中14.04 tag的鏡像
core@localhost ~/php $ docker  pull dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g
#下載其它非官方倉庫中的鏡像,通常站點會給出具體的 pull 命令
Pulling repository dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g
ba16d5d5e1aa: Pulling image (latest) from dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g, endpoint: http://dl.dockerpool.com:5000ba16d5d5e1aa: Download complete
8dbd9e392a96: Download complete
#這一串數字是表示文件系統的層次,docker 的鏡像和容器就是這一層一層的文件系統組成的
215be6e94fbb: Download complete
ef2887b77b73: Download complete
97774de1565b: Download complete
c6a02636680f: Download complete
2ae911074081: Download complete
e1787c817b10: Download complete
5e312dc5fae8: Download complete
cb35ce95b58c: Download complete
5f0e28679c8e: Download complete
...

列出鏡像

使用「docker images」列出本地宿主主機上擁有的image鏡像git

core@localhost ~ $ docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
base/163                                                latest              468d347c06bc        28 hours ago        249.1 MB
test/supervisord                                        latest              468d347c06bc        28 hours ago        249.1 MB
ubuntu                                                  14.04               1357f421be38        4 days ago          192.7 MB
dl.dockerpool.com:5000/ubuntu                           14.04               1357f421be38        4 days ago          192.7 MB
dl.dockerpool.com:5000/mysql                            5.7                 e95cbb9f48ea        6 days ago          258.6 MB
mysql                                                   5.7                 e95cbb9f48ea        6 days ago          258.6 MB
mysql                                                   latest              9a09222edf60        6 days ago          235.6 MB
tutum/lamp                                              latest              4b32789c7d66        5 weeks ago         469.8 MB
tutum/tomcat                                            8.0                 866eb07a675e        6 weeks ago         539.4 MB
tutum/tomcat                                            latest              02e84f04100e        6 weeks ago         539.4 MB
dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g   latest              ba16d5d5e1aa        7 months ago        2.388 GB
此處共同擁有5列。分別表示鏡像的名稱、鏡像的tag標記、鏡像的惟一 image id、建立時間、大小

建立鏡像

建立鏡像的方法有 2 種:web

1. 從文件系統導入sql

眼下可用的文件系統主要是openvz的模板docker

比方:下載了一個ubuntu14.04的鏡像 cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import - ubuntu:14.04 而後用docker images看下:ubuntu

core@localhost ~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               05ac7c0b9383        17 seconds ago      215.5 MB

就多了一個咱們的ubuntu鏡像tomcat

2. 從 dockerfile 建立bash

dockerfile的內容後面章節具體介紹網絡

3. 從現有的容器 commit 提交到一個新的 image

core@localhost ~ $ docker commit

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

Create a new image from a container's changes

  -a, --author=""     Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  #填寫做者信息
  -m, --message=""    Commit message
  #填寫提交信息
  -p, --pause=true    Pause container during commit
  #假設容器還在執行,先暫停容器
core@localhost ~ $ docker run -ti ubuntu:14.04 /bin/bash
#建立一個僅僅有bash程序的容器
root@a925cb40b3f0:/# touch test
#在容器中創建一個文件
root@a925cb40b3f0:/# exit
#退出
exit
core@localhost ~ $ docker commit a92 for_test
#將剛纔的容器提交爲一個叫 for_test 的鏡像,這裏咱們使用容器的 id 來指定咱們要提交的容器。也可以使用容器的名字。他們都是惟一的
9e9c814023bcffc3e67e892a235afe61b02f66a947d2747f724bd317dda02f27
#返回新鏡像的 id
core@localhost ~ $ docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
for_test                                                latest              9e9c814023bc        4 seconds ago       192.7 MB

刪除鏡像

使用「docker rmi + 鏡像 id」刪除鏡像,當還有容器使用該鏡像的時候是沒法刪除的。

core@localhost ~ $ docker rmi

Usage: docker rmi IMAGE [IMAGE...]

Remove one or more images

  -f, --force=false    Force removal of the image
  --no-prune=false     Do not delete untagged parents

core@localhost ~ $ docker images
REPOSITORY                                              TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
for_test                                                latest              60734a0ee3d6        3 seconds ago       192.7 MB
base/163                                                latest              468d347c06bc        29 hours ago        249.1 MB
test/supervisord                                        latest              468d347c06bc        29 hours ago        249.1 MB
ubuntu                                                  14.04               1357f421be38        4 days ago          192.7 MB
dl.dockerpool.com:5000/ubuntu                           14.04               1357f421be38        4 days ago          192.7 MB
dl.dockerpool.com:5000/mysql                            5.7                 e95cbb9f48ea        6 days ago          258.6 MB
mysql                                                   5.7                 e95cbb9f48ea        6 days ago          258.6 MB
mysql                                                   latest              9a09222edf60        6 days ago          235.6 MB
tutum/lamp                                              latest              4b32789c7d66        5 weeks ago         469.8 MB
tutum/tomcat                                            8.0                 866eb07a675e        6 weeks ago         539.4 MB
tutum/tomcat                                            latest              02e84f04100e        6 weeks ago         539.4 MB
dl.dockerpool.com:5000/alexeiled/docker-oracle-xe-11g   latest              ba16d5d5e1aa        7 months ago        2.388 GB
core@localhost ~ $ docker rmi 9a0
#當咱們刪除鏡像 9a0 即 mysql:latest 鏡像時。它刪除了這個鏡像所附帶的所有aufs層
Untagged: mysql:latest
Deleted: 9a09222edf600a03ea48bd23cfa363841e45a8715237e3a58cb0167f0e8bad54
Deleted: 4daeda4ad839a152a3b649672bd5135977d7f81866d3bc0e16d0af3f65cc8af6
Deleted: cf07a411bf0883bd632940e8108dac49c64456a47f7390507de5685bbd6daf85
Deleted: 4f513746df18b222a07bb8d76d4b6d29752ce5dcb69bfad0ce92e6c1449a3821
Deleted: 228ecd435c8a29d25b77999036701a27f2d67874c915bb8eb9fb175b1f98aa60
Deleted: 37e4b3932afa186924a09eab332bc8ebec3aac8bac074314ed9a2d1e94547f50
Deleted: 898883ccfcee705e440547e30e240cb025c12410d7c9e4d2bcb11973ba075975
Deleted: 0a09ddcf99b7fd8fcb3525c41b54696038ecf13677f4459f1c98c742ffa60ab2
Deleted: 35bc8591e39be5089265a093e234d13a4b155a01d2ab9e8904eafa81664fb597
Deleted: 857e856e4481d59ee88a4cdedd9aaf855666bd494fa38506e6788361c0af4cda
core@localhost ~ $ docker ps
#使用 -ps 參數可以看到眼下有上個容器在執行。當中一個容器是以tutum/lamp:laste鏡像啓動的
CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS                                          NAMES
9cb2e45814e0        tutum/lamp:latest                  "/run.sh"              4 hours ago         Up 4 hours          0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   loving_feynman
e3c136d76b44        tutum/tomcat:8.0                   "/run.sh"              6 hours ago         Up 6 hours          0.0.0.0:80->8080/tcp                           tomcat001
fe9e65aaf58c        dl.dockerpool.com:5000/mysql:5.7   "/entrypoint.sh mysq   6 hours ago         Up 6 hours          3306/tcp                                       db001,tomcat001/tomysql
core@localhost ~ $ docker rmi 4b3
#當咱們試圖刪除tutum/lamp:laste 鏡像時,提示咱們眼下還有容器在使用該鏡像,沒法刪除。如需刪除則需要中止容器並用-f 參數刪除鏡像
Error response from daemon: Conflict, cannot delete 4b32789c7d66 because the running container 9cb2e45814e0 is using it (docker untagged the image), stop it and use -f to force
2014/10/15 08:23:59 Error: failed to remove one or more images

搜尋鏡像

使用「docker search + keyword」搜索共享的鏡像。默認搜索官方倉庫的鏡像,搜索私有倉庫的語法在私有倉庫章節具體介紹。

core@localhost ~ $ docker search

Usage: docker search TERM

Search the Docker Hub for images

  --automated=false    Only show automated builds
  --no-trunc=false     Don't truncate output
  -s, --stars=0        Only displays with at least x stars
#通常使用不帶參數的搜尋就能夠,比方要搜尋mysqlkeyword的 image
core@localhost ~ $ docker search mysql
#返回的信息有5列。分別表明鏡像集名稱,鏡像的描寫敘述。被收藏的次數,時候屬於官方出品,時候支持本身主動建立
NAME                                          DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                                         MySQL is a widely used, open-source relati...   193       [OK]
tutum/mysql                                   MySQL Server image - listens in port 3306....   69                   [OK]
orchardup/mysql                                                                               36                   [OK]
tutum/lamp                                    LAMP image - Apache listens in port 80, an...   30                   [OK]
tutum/wordpress                               Wordpress Docker image - listens in port 8...   24                   [OK]
paintedfox/mariadb                            A docker image for running MariaDB 5.5, a ...   19                   [OK]
dockerfile/mysql                              Trusted automated MySQL (http://dev.mysql....   11                   [OK]
anapsix/gitlab-ci                             GitLab-CI Continuous Integration in Docker...   11                   [OK]
centurylink/drupal                            Drupal docker image without a DB included ...   10                   [OK]
google/mysql                                  MySQL server for Google Compute Engine          10                   [OK]
stenote/docker-lemp                           MySQL 5.六、PHP 5.五、Nginx、Memcache                9                    [OK]

上傳鏡像

使用「docker push +keyword 」上傳鏡像到官方倉庫,上傳私有倉庫的語法在私有倉庫章節具體介紹。

core@localhost ~ $ docker push

Usage: docker push NAME[:TAG]

Push an image or a repository to the registry
core@localhost ~ $ docker push base/163
The push refers to a repository [base/163] (len: 1)
Sending image list

Please login prior to push:
Username: waitfish
Password:
Email: xxx@xxx.com
第一次上傳需要填寫在dockerhub註冊的賬號信息

不少其它內容請關注 http://www.dockerpool.com

相關文章
相關標籤/搜索