获得更多资料欢迎进入我的网站或者 csdn或者博客园

昨天讲解了docker的安装与基本使用,今天给大家讲解下docker镜像的创建的方法,以及push到Docker Hub

docker安装请点击右边:ubuntu15.04下安装docker

修改已有镜像

启动已下载镜像

peace@peace:~$ sudo docker run -t -i ubuntu:12.04 /bin/bash
root@909634e032f9:/# cd /home
#进入home目录建立文件夹 1.c
root@909634e032f9:/home# mkdir 1.c
root@909634e032f9:/home# ll
​total 12
drwxr-xr-x 3 root root 4096 Jul 24 09:08 ./
drwxr-xr-x 35 root root 4096 Jul 24 09:08 ../
drwxr-xr-x 2 root root 4096 Jul 24 09:08 1.c/
#结束后使用exit退出
root@909634e032f9:/home# exit
exit

制作新镜像

上面容器的Id为:909634e032f9,制作镜像是需要这个号码;

现在我们的容器已经被我们改变了,使用 docker commit 命令来提交更新后的副本。

peace@peace:~$ sudo docker commit -m "Added 1.c" -a "peace" 909634e032f9 ouruser/ubuntu12:v1
de8f9f72833dec42389e726183735c91d722e0d4271bb436b5babb080c7d8615
#其中,-m 来指定提交的说明信息,跟我们使用的版本控制工具一样;-a 可以指定更新的用户信息;之后是用来创建镜像的容器的 ID;最后指定目标镜像的仓库名和 tag 信息。创建成功后会返回这个镜像的 ID 信息。
#使用docker images查看新创建的镜像
peace@peace:~$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ouruser/ubuntu12 v1 de8f9f72833d 3 minutes ago 175 MB
ubuntu 12.04 6d021018145f 2 weeks ago 134 MB
busybox latest 8c2e06607696 3 months ago

启动新镜像观察:

启动新镜像观察,进入home目录可以看到1.c文件夹

peace@peace:~$ sudo docker run -t -i ouruser/ubuntu12:v1 /bin/bash
root@34b2f52641d8:/# cd /home/
root@34b2f52641d8:/home# ll
total 12
drwxr-xr-x 3 root root 4096 Jul 24 09:08 ./
drwxr-xr-x 36 root root 4096 Jul 24 09:32 ../
drwxr-xr-x 2 root root 4096 Jul 24 09:08 1.c/

利用 Dockerfile 来创建镜像

利用docker commit修改镜像是比较简单的,但是不方便在一个团队中分享;我们可以使用docker build来创建一个新的镜像。下面讲解如何建立新的镜像:

新建立一个目录和dockerfile

peace@peace:~$ mkdir peace
peace@peace:~$ cd peace/
peace@peace:~/peace$ touch Dockerfile
peace@peace:~/peace$ ll
total 8
drwxrwxr-x 2 peace peace 4096 Jul 24 17:41 ./
drwxr-xr-x 38 peace peace 4096 Jul 24 17:41 ../
-rw-rw-r-- 1 peace peace 0 Jul 24 17:41 Dockerfile

编写Dockerfile

Dockerfile 基本的语法是

使用#来注释

FROM 指令告诉 Docker 使用哪个镜像作为基础

接着是维护者的信息

RUN开头的指令会在创建中运行,比如安装一个软件包,在这里使用 apt-get 来安装了一些软件

peace@peace:~/peace$ vim Dockerfile
#以ubuntu:12.04 为基础创建新的镜像
FROM ubuntu:12.04
MAINTAINER wang peace<w_peace12@163.com>
RUN apt-get -qq update
RUN apt-get -qqy install python2.7

编写完成之后可以使用docker build来生成镜像了:

语句:sudo docker build -t="ouruser/ubuntu14:v2" .

其中 -t 标记来添加 tag,指定新的镜像的用户信息。 “.” 是 Dockerfile 所在的路径(当前目录),也可以替换为一个具体的 Dockerfile 的路径。

可以看到 build 进程在执行操作。它要做的第一件事情就是上传这个 Dockerfile 内容,因为所有的操作都要依据 Dockerfile 来进行。 然后,Dockfile 中的指令被一条一条的执行。每一步都创建了一个新的容器,在容器中执行指令并提交修改(就跟之前介绍过的docker commit 一样)。当所有的指令都执行完毕之后,返回了最终的镜像 id。所有的中间步骤所产生的容器都被删除和清理了。

显示如下:

peace@peace:~/peace$ sudo docker build -t="ouruser/ubuntu14:v2" .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:12.04
---> 6d021018145f
Step 1 : MAINTAINER wang peace<w_peace12@163.com>
---> Running in 5960929b225d
---> 6584aa0732af
Removing intermediate container 5960929b225d
Step 2 : RUN apt-get -qq update
---> Running in 5a3c28aeb0ce
---> d4280d89675e
Removing intermediate container 5a3c28aeb0ce
Step 3 : RUN apt-get -qqy install python2.7
---> Running in 32c0870c85e9
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libsqlite3-0.
(Reading database ... 7560 files and directories currently installed.)
Unpacking libsqlite3-0 (from .../libsqlite3-0_3.7.9-2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libexpat1.
Unpacking libexpat1 (from .../libexpat1_2.0.1-7.2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package libmagic1.
Unpacking libmagic1 (from .../libmagic1_5.09-2ubuntu0.6_amd64.deb) ...
Selecting previously unselected package file.
Unpacking file (from .../file_5.09-2ubuntu0.6_amd64.deb) ...
Selecting previously unselected package mime-support.
Unpacking mime-support (from .../mime-support_3.51-1ubuntu1.1_all.deb) ...
Selecting previously unselected package python2.7.
Unpacking python2.7 (from .../python2.7_2.7.3-0ubuntu3.8_amd64.deb) ...
Setting up libsqlite3-0 (3.7.9-2ubuntu1.1) ...
Setting up libexpat1 (2.0.1-7.2ubuntu1.1) ...
Setting up libmagic1 (5.09-2ubuntu0.6) ...
Setting up file (5.09-2ubuntu0.6) ...
Setting up mime-support (3.51-1ubuntu1.1) ...
update-alternatives: using /usr/bin/see to provide /usr/bin/view (view) in auto mode.
Setting up python2.7 (2.7.3-0ubuntu3.8) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
---> 41ed29b5662e
Removing intermediate container 32c0870c85e9
Successfully built 41ed29b5662e

使用docker images显示如下

peace@peace:~/peace$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ouruser/ubuntu14 v2 41ed29b5662e About a minute ago 182.1 MB
ouruser/ubuntu12 v1 de8f9f72833d About an hour ago 175 MB
ubuntu 12.04 6d021018145f 2 weeks ago 134 MB
busybox latest 8c2e06607696 3 months ago 2.433 MB

测试新的镜像

新的镜像中应该包含有python2.7。

peace@peace:~/peace$ sudo docker run -t -i ouruser/ubuntu14:v2 /bin/bash
root@e1511892bc91:/# python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>>

上传你的镜像

用户可以通过 docker push 命令,把自己创建的镜像上传到仓库中来共享。例如,用户在 Docker Hub 上完成注册后,可以推送自己的镜像到仓库中。例如我注册了:wpeace1212

注册步骤不介绍了。注册官网:请点击

push步骤如下:

1.改镜像名字

sudo docker  tag -f  41ed29b5662e wpeace1212/ubuntu

2.push 需要填写账号密码

sudo docker push wpeace1212/ubuntu
#先改动命名:找到ouruser/ubuntu14 的id
peace@peace:~/peace$ docker images
WARNING: Error loading config file:stat /home/peace/.docker/config.json: permission denied
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ouruser/ubuntu14 v2 41ed29b5662e 59 minutes ago 182.1 MB
#改动命名:username(你的账号)/随意
peace@peace:~/peace$ sudo docker tag -f 41ed29b5662e wpeace1212/ubuntu
peace@peace:~/peace$ docker images
WARNING: Error loading config file:stat /home/peace/.docker/config.json: permission denied
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ouruser/ubuntu14 v2 41ed29b5662e About an hour ago 182.1 MB
wpeace1212/ubuntu latest 41ed29b5662e About an hour ago 182.1 MB
ouruser/ubuntu12 v1 de8f9f72833d 2 hours ago 175 MB
ubuntu 12.04 6d021018145f 2 weeks ago 134 MB
busybox latest 8c2e06607696 3 months ago 2.433 MB
#上传到你的账号
peace@peace:~/peace$ sudo docker push wpeace1212/ubuntu
The push refers to a repository [wpeace1212/ubuntu] (len: 1)
41ed29b5662e: Image already exists
d4280d89675e: Image successfully pushed
6584aa0732af: Image successfully pushed
6d021018145f: Image already exists
50e9f95f98f1: Image successfully pushed
639d60300768: Image successfully pushed
093e01545ca5: Image successfully pushed
Digest: sha256:cf3ec66e332e8f322cfef46459f909ec0ada86de22307938b810e7667136e5a8

登陆到Docker Hub可以看大你的push镜像:

如下:

最新文章

  1. qemu 源码调试
  2. Mysql 关键字及保留字
  3. delphi 使用superobject实现jsonrpc的http远程调用 good
  4. Vertica对于所计算的时间SQL声明大全
  5. C# .NET中的 反射的应用
  6. MIP 与 AMP 合作进展(3月7日)
  7. VS 附加到进程 加载“附加进程”弹窗很慢
  8. jenkins乱码解决问题
  9. day-09内存管理
  10. python学习笔记(3)--turtle简单绘制
  11. hadoop datanode 启动出错
  12. P1593 因子和
  13. full GC触发的条件
  14. 42-2017蓝桥杯b java
  15. ural 1091. Tmutarakan Exams(容斥)
  16. python科学计算与可视化
  17. 《PHP发送邮件PHPMailer》系列分享专栏
  18. 使用thymeleaf实现div中加载html
  19. centos关闭sudo的ldap认证
  20. Spring--入门第二天

热门文章

  1. a标签:鼠标指针变成文本输入图形
  2. 用插件NPOI读写excel
  3. 3-java中String值为空字符串与null的判断方法
  4. 我使用的网址--Hadoop
  5. 算法Sedgewick第四版-第3章Searching-搜索总结
  6. Openssl genpkey命令
  7. pipeline 对部分特征进行处理
  8. 不错的silverlight教程
  9. LightOJ 1079 Just another Robbery (01背包)
  10. 状态压缩DP----HDU4049 Tourism Planning