设为首页 收藏本站
查看: 1069|回复: 0

[经验分享] Docker 创建image

[复制链接]

尚未签到

发表于 2017-12-6 10:12:29 | 显示全部楼层 |阅读模式
images 是containers的基础。每次使用docker run 命令都要指定image。



列出本地images








zane@zane-V:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
zane0306/docker-whale   latest              d09756981eeb        30 hours ago        275.1 MB
ubuntu                  latest              104bec311bcd        11 days ago         129 MB
hello-world             latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago       247 MB
training/webapp         latest              6fae60ef3446        19 months ago       348.8 MB




寻找获取新image





寻找一下centos images。




zane@zane-V:~$ docker search centos
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                 The official build of CentOS.                   2949      [OK]      
jdeathe/centos-ssh                     CentOS-6 6.8 x86_64 / CentOS-7 7.3.1611 x8...   51                   [OK]
nimmis/java-centos                     This is docker images of CentOS 7 with dif...   20                   [OK]
torusware/speedus-centos               Always updated official CentOS docker imag...   8                    [OK]
egyptianbman/docker-centos-nginx-php   A simple and highly configurable docker co...   6                    [OK]
nathonfowlie/centos-jre                Latest CentOS image with the JRE pre-insta...   5                    [OK]
centos/mariadb55-centos7                                                               3                    [OK]



docker pull 下载image




zane@zane-V:~$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos

45a2e645736c: Pull complete
Digest: sha256:c577af3197aacedf79c5a204cd7f493c8e07ffbce7f88f7600bf19c688c38799
Status: Downloaded newer image for centos:latest



启动一个容器




zane@zane-V:~$ docker run  -t -i  centos /bin/bash
[iyunv@a57fbae352e1 /]# ls
anaconda-post.log  dev  home  lib64       media  opt   root  sbin  sys  usr
bin                etc  lib   lost+found  mnt    proc  run   srv   tmp  var



training/sinatra下载




zane@zane-V:~$  docker pull training/sinatra
Using default tag: latest
latest: Pulling from training/sinatra

a3ed95caeb02: Pull complete
6e71c809542e: Pull complete
d196a7609355: Pull complete
08f6dff5acea: Pull complete
ce65532003d0: Pull complete
54bcaa4d1a10: Pull complete
8572ad96f6e1: Pull complete
Digest: sha256:03fc0cd265cbc28723e4efd446f9f2f37b4790cf9cc12f1b9203c79fb86b6772
Status: Downloaded newer image for training/sinatra:latest


创建自己的images





虽然training/sinatra ,image已经非常好了,但是可能我们要增加一些功能。

更新和创建images的两种方法

  • 更新容器,并提交更改结果到image
  • 使用Dockerfile 指定说明来创建一个image




更新和提交image



创建容器用来更新




zane@zane-V:~$ docker run -t -i training/sinatra /bin/bash
root@b9d598059fa9:/#


查看正在运行的容器




zane@zane-V:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b9d598059fa9        training/sinatra    "/bin/bash"         45 seconds ago      Up 44 seconds                           desperate_bose


在运行的容器中让我们先升级 Ruby




root@b9d598059fa9:/# apt-get install -y ruby2.0-dev ruby2.0
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
.......
.......


再来安装json




root@b9d598059fa9:/# gem2.0 install json
Fetching: json-2.0.2.gem (100%)
Building native extensions.  This could take a while...
Successfully installed json-2.0.2
Parsing documentation for json-2.0.2
unable to convert "\xD0" from ASCII-8BIT to UTF-8 for lib/json/ext/generator.so, skipping
unable to convert "\xD0" from ASCII-8BIT to UTF-8 for lib/json/ext/parser.so, skipping
Installing ri documentation for json-2.0.2
1 gem installed


完成更新之后,退出容器使用exit 命令。




root@b9d598059fa9:/# exit
exit
zane@zane-V:~$


提交

docker commit命令提交

-m : 说明更改

-a  :  更改作者



b9d598059fa9:更改的容器ID

sinatra:v2       :提交到sinatra image ,v2是tag;






zane@zane-V:~$ docker commit -m "Added json gem" -a "zane" b9d598059fa9 sinatra:v2
sha256:7902e96eb0144d64577348c02ff8accf939e5469c01df9a0911044122205b8c7

zane@zane-V:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
sinatra                 v2                  7902e96eb014        About a minute ago   471.2 MB
zane0306/docker-whale   latest              d09756981eeb        30 hours ago         275.1 MB
centos                  latest              67591570dd29        11 days ago          191.8 MB
ubuntu                  latest              104bec311bcd        11 days ago          129 MB
hello-world             latest              c54a2cc56cbb        5 months ago         1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago        247 MB
training/webapp         latest              6fae60ef3446        19 months ago        348.8 MB
training/sinatra        latest              49d952a36c58        2 years ago          447 MB


可以看到commit 之后,在本地出现了我们本地image



使用Dockerfile创建image



docker build 命令 读取Dockerfile 来创建image



创建目录以及Dockerfile




zane@zane-V:~$ mkdir sinatra
zane@zane-V:~$ cd sinatra/
zane@zane-V:~/sinatra$ touch Dockerfile

Dockerfile内容




# This is a comment
FROM ubuntu:14.04
MAINTAINER zane zane.zhang@zoom.us
RUN apt-get update && apt-get install -y ruby ruby-dev
RUN gem install sinatra


  • #: 注释
  • FROM

    • 以哪个image为基础


  • maintainer

    • 作者


  • RUN

    • 在基础image上做一些实质性操作






docker build 创建image






zane@zane-V:~/sinatra$ docker build -t zane/sinatra:v1 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:14.04
14.04: Pulling from library/ubuntu

16da43b30d89: Pulling fs layer
1840843dafed: Pulling fs layer
91246eb75b7d: Pulling fs layer
7faa681b41d7: Waiting
97b84c64d426: Waiting


-t 参数给image名称 zane/sinatra,以及标签 v1

不要忘记 . 命令,它告诉docker build 在本目录寻找调用Dockerfile。








zane@zane-V:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
zane/sinatra            v1                  c0e79450b1a1        About a minute ago   322.4 MB
sinatra                 v2                  7902e96eb014        21 minutes ago       471.2 MB
zane0306/docker-whale   latest              d09756981eeb        31 hours ago         275.1 MB
centos                  latest              67591570dd29        11 days ago          191.8 MB
ubuntu                  latest              104bec311bcd        11 days ago          129 MB
ubuntu                  14.04               3f755ca42730        11 days ago          188 MB
hello-world             latest              c54a2cc56cbb        5 months ago         1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago        247 MB
training/webapp         latest              6fae60ef3446        19 months ago        348.8 MB
training/sinatra        latest              49d952a36c58        2 years ago          447 MB




设置image的标签








zane@zane-V:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
sinatra                 v2                  7902e96eb014        18 minutes ago      471.2 MB
zane0306/docker-whale   latest              d09756981eeb        31 hours ago        275.1 MB
centos                  latest              67591570dd29        11 days ago         191.8 MB
ubuntu                  latest              104bec311bcd        11 days ago         129 MB
ubuntu                  14.04               3f755ca42730        11 days ago         188 MB
hello-world             latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago       247 MB
training/webapp         latest              6fae60ef3446        19 months ago       348.8 MB
training/sinatra        latest              49d952a36c58        2 years ago         447 MB


给sinatra增加一个tag vv




zane@zane-V:~$ docker tag 7902e96eb014 sinatra:vv

zane@zane-V:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE B
sinatra                 v2                  7902e96eb014        19 minutes ago      471.2 MB
sinatra                 vv                  7902e96eb014        19 minutes ago      471.2 MB
zane0306/docker-whale   latest              d09756981eeb        31 hours ago        275.1 MB
centos                  latest              67591570dd29        11 days ago         191.8 MB
ubuntu                  latest              104bec311bcd        11 days ago         129 MB
ubuntu                  14.04               3f755ca42730        11 days ago         188 MB
hello-world             latest              c54a2cc56cbb        5 months ago        1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago       247 MB
training/webapp         latest              6fae60ef3446        19 months ago       348.8 MB
training/sinatra        latest              49d952a36c58        2 years ago         447 MB


当然也可以给指定不同的repository名称,以及tag名;






zane@zane-V:~$ docker tag 7902e96eb014 zane-sinatra:vv
zane@zane-V:~$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
sinatra                 v2                  7902e96eb014        21 minutes ago       471.2 MB
sinatra                 vv                  7902e96eb014        21 minutes ago       471.2 MB
zane-sinatra            vv                  7902e96eb014        21 minutes ago       471.2 MB
zane0306/docker-whale   latest              d09756981eeb        31 hours ago         275.1 MB
centos                  latest              67591570dd29        11 days ago          191.8 MB
ubuntu                  latest              104bec311bcd        11 days ago          129 MB
ubuntu                  14.04               3f755ca42730        11 days ago          188 MB
hello-world             latest              c54a2cc56cbb        5 months ago         1.848 kB
docker/whalesay         latest              6b362a9f73eb        19 months ago        247 MB
training/webapp         latest              6fae60ef3446        19 months ago        348.8 MB
training/sinatra        latest              49d952a36c58        2 years ago          447 MB




image摘要




$ docker images --digests | head REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE ouruser/sinatra latest sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf 5db5f8471261 11 hours ago 446.7 MB



可以用来指定要拉下来的是哪个版本



push image 到 Docker Hub




zane@zane-V:~$ docker push zane/sinatra
The push refers to a repository [docker.io/zane/sinatra]
e9170f93385d: Preparing
d375b10aba64: Preparing
4fcb79d431cc: Preparing
4375cecd293e: Preparing
738d3f35b582: Preparing
53edc9780c07: Waiting
bc224b1b676d: Waiting
unauthorized: authentication required



删除本地image






zane@zane-V:~$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Deleted: sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc
Deleted: sha256:a02596fdd012f22b03af6ad7d11fa590c57507558357b079c3e8cebceb4262d7





总结



  • 寻找获取新image

    • docker search centos


  • 从docker hub 上pull下来

    • docker pull centos


  • 启动一个容器

    • docker run  -t -i  centos /bin/bash


  • 创建image两种方式



      • 更新容器,并提交更改结果到image

        • a.运行容器
        • b.安装新软件如:ruby,python,json
        • c.提交

          • docker commit -m "Added json gem" -a "zane" b9d598059fa9 sinatra:v2
          • -m :说明,-a:作者,
          • 将容器b9d598059fa9的更改提交到sinatra 镜像的V2标签




      • 使用Dockerfile,创建新image

        • 编写dockerfile
        • 运行build 命令






  • 给image增加标签

    • docker tag 7902e96eb014 sinatra:vv



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-421115-1-1.html 上篇帖子: MySQL 使用 SSL 连接(附 Docker 例子) 下篇帖子: 《Spring Cloud与Docker微服务架构实战》配套代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表