Docker入门之image篇

  • 基本概念
  1. Image  镜像:只读模板
  2. Container  容器:从镜像创建的运行实例
  3. Repository  仓库:集中存放镜像文件的场所。分为公开仓库(Public)和私有仓库(Private)两种形式。最大的公开仓库是 Docker Hub,存放了数量庞大的镜像供用户下载。 国内的公开仓库包括 Docker Pool 等,可以提供大陆用户更稳定快速的访问。
  4. Registry  仓库注册服务器:仓库注册服务器上往往存放着多个仓库

 

  • ubuntu安装docker

1)通过系统自带包安装

  Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装。

$ sudo apt-get update

$ sudo apt-get install -y docker.io

$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

$ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

  如果使用操作系统自带包安装 Docker,目前安装的版本是比较旧的 0.9.1。 要安装更新的版本,可以通过 使用 Docker 源的方式。

 

 

2)通过Docker源安装最新版本

  要安装最新的 Docker 版本,首先需要安装 apt-transport-https 支持,之后通过添加源来安装。

$ sudo apt-get install apt-transport-https

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8 950F966E92D8576A8BA88D21E9

$ sudo bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/ docker.list"

$ sudo apt-get update

$ sudo apt-get install lxc-docker

 

 

  • 镜像 Image

1)获取:docker pull

  $ sudo docker pull ubuntu:12.04  

  该命令实际上相当于 $ sudo docker pull registry.hub.docker.com/ubuntu:12.04 命令,即从注册服务 器 registry.hub.docker.com 中的 ubuntu 仓库来下载标记为 12.04 的镜像。

 

2)列出:docker images

  $ sudo docker images

    例如下面的命令指定使用镜像ubuntu:14.04 来启动一个容器

    $ sudo docker run -t -i ubuntu:14.04 /bin/bash

    # 如果不指定具体的标记,则默认使用 latest 标记信息。

 

3)创建

  3.1)修改已有镜像来创建新的镜像

  创建镜像有很多方法,用户可以从 Docker Hub 获取已有镜像并更新,也可以利用本地文件系统创建一 个。

root@Ubuntu14:/# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/rep            v100                6929a35a93bc        3 minutes ago       137MB
hello-world         latest              e38bc07ac18e        7 weeks ago         1.85kB
ubuntu              15.10               9b9cb95443b5        22 months ago       137MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB

root@Ubuntu14:/# docker run -it ubuntu:
15.10 /bin/bash      # 先使用已有镜像启动容器,记住容器ID,稍后要用 root@06de4be84240:/# do sth. to make any change you want^C root@06de4be84240:/# exit exit
root@Ubuntu14:/# docker commit -m
"Sth. changed" -a "Karl" 06de4be84240 test/reposiroty:v1r2c30  # -m 来指定提交的说明信息,跟我们使用的版本控制工具一样; -a 可以指定更新的用户信息;之 后是用来创建镜像的容器的 ID;最后指定目标镜像的仓库名和 tag 信息。 sha256:a5f9f6ffa235a8496282a7b4da228a424d20e2e19579cd09c1d4928186c67ba4 root@Ubuntu14:/# docker images      # 查看新创建的镜像 REPOSITORY TAG IMAGE ID CREATED SIZE test/reposiroty v1r2c30 a5f9f6ffa235 10 seconds ago 137MB test/rep v100 6929a35a93bc 6 minutes ago 137MB hello-world latest e38bc07ac18e 7 weeks ago 1.85kB ubuntu 15.10 9b9cb95443b5 22 months ago 137MB training/webapp latest 6fae60ef3446 3 years ago 349MB root@Ubuntu14:/#

  

  3.2)利用Dockerfile来创建镜像

  使用 docker commit 来扩展一个镜像比较简单,但是不方便在一个团队中分享。我们可以使用 docker build 来创建一个新的镜像。为此,首先需要创建一个 Dockerfile,包含一些如何创建镜像的指令。

#(1)新建一个目录和一个Dockerfile
root@Ubuntu14:~# mkdir test-dir root@Ubuntu14:~# ls compose_test get-docker.sh laradock test-dir root@Ubuntu14:~# cd test-dir/ root@Ubuntu14:~/test-dir# touch Dockerfile root@Ubuntu14:~/test-dir# vi Dockerfile root@Ubuntu14:~/test-dir# cat Dockerfile # This is a comment
FROM ubuntu:14.04 MAINTAINER Karl <karlkiller@icloud.com> RUN apt-get -qq update RUN apt-get -qqy install ruby ruby-dev RUN gem install sinatra root@Ubuntu14:~/test-dir#

'''
Dockerfile 基本的语法是
使用 # 来注释
FROM 指令告诉 Docker 使用哪个镜像作为基础
接着是维护者的信息
RUN 开头的指令会在创建中运行,比如安装一个软件包,在这里使用 apt-get 来安装了一些软件
'''
#(2)编写完成 Dockerfile 后可以使用 docker build 来生成镜像。

$ sudo docker build -t="ouruser/sinatra:v2" . Uploading context 2.56 kB Uploading context Step 0 : FROM ubuntu:14.04 ---> 99ec81b80c55 Step 1 : MAINTAINER Newbee <newbee@docker.com> ---> Running in 7c5664a8a0c1 ---> 2fa8ca4e2a13 Removing intermediate container 7c5664a8a0c1 Step 2 : RUN apt-get -qq update ---> Running in b07cc3fb4256 ---> 50d21070ec0c Removing intermediate container b07cc3fb4256 Step 3 : RUN apt-get -qqy install ruby ruby-dev ---> Running in a5b038dd127e Selecting previously unselected package libasan0:amd64. (Reading database ... 11518 files and directories currently installed.) Preparing to unpack .../libasan0_4.8.2-19ubuntu1_amd64.deb ... Setting up ruby (1:1.9.3.4) ... Setting up ruby1.9.1 (1.9.3.484-2ubuntu1) ... Processing triggers for libc-bin (2.19-0ubuntu6) ... ---> 2acb20f17878 Removing intermediate container a5b038dd127e Step 4 : RUN gem install sinatra ---> Running in 5e9d0065c1f7 . . . Successfully installed rack-protection-1.5.3 Successfully installed sinatra-1.4.5 4 gems installed ---> 324104cde6ad Removing intermediate container 5e9d0065c1f7 Successfully built 324104cde6ad

'''
其中:
-t 标记来添加 tag,指定新的镜像的用户信息。
“.” 是 Dockerfile 所在的路径(当前目录),也 可以替换为一个具体的 Dockerfile 的路径。
可以看到 build 进程在执行操作。它要做的第一件事情就是上传这个 Dockerfile 内容,因为所有的操作都 要依据 Dockerfile 来进行。
然后,Dockfile 中的指令被一条一条的执行。每一步都创建了一个新的容器,在容器中执行指令并提交修改(就跟之前介绍过的 docker commit 一样)。当所有的指令都执行完 毕之后,返回了最终的镜像 id。所有的中间步骤所产生的容器都被删除和清理了。
*注意一个镜像不能超过 127 层
'''
'''
此外,还可以利用 ADD 命令复制本地文件到镜像;
用 EXPOSE 命令来向外部开放端口;
用 CMD 命 令来描述容器启动后运行的程序等
'''
# put my local web site in myApp folder to /var/www
ADD myApp /var/www # expose httpd port EXPOSE 80 # the command to run CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
'''
还可以用 docker tag 命令来修改镜像的标签,实际上是给镜像添加新的标签
'''
root@Ubuntu14:~/test-dir# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 1ba1618e54b7
9 minutes ago 1.85kB test/reposiroty v1r2c30 a5f9f6ffa235 40 minutes ago 137MB test/rep v100 6929a35a93bc About an hour ago 137MB ubuntu 14.04 578c3e61a98c 11 hours ago 223MB hello-world latest e38bc07ac18e 7 weeks ago 1.85kB ubuntu 15.10 9b9cb95443b5 22 months ago 137MB training/webapp latest 6fae60ef3446 3 years ago 349MB root@Ubuntu14:~/test-dir# docker tag e38bc07ac18e new/hello:v3 root@Ubuntu14:~/test-dir# docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 1ba1618e54b7 10 minutes ago 1.85kB test/reposiroty v1r2c30 a5f9f6ffa235 41 minutes ago 137MB test/rep v100 6929a35a93bc About an hour ago 137MB ubuntu 14.04 578c3e61a98c 11 hours ago 223MB hello-world latest e38bc07ac18e 7 weeks ago 1.85kB new/hello v3 e38bc07ac18e 7 weeks ago 1.85kB ubuntu 15.10 9b9cb95443b5 22 months ago 137MB training/webapp latest 6fae60ef3446 3 years ago 349MB root@Ubuntu14:~/test-dir#

 

4)上传

'''
用户可以通过 docker push 命令,把自己创建的镜像上传到仓库中来共享。
例如,用户在 Docker Hub 上完成注册后,可以推送自己的镜像到仓库中
'''
$ sudo docker push ouruser/sinatra The push refers to a repository [ouruser/sinatra] (len:
1) Sending image list Pushing repository ouruser/sinatra (3 tags)

 

5)存出/载入/移除

root@Ubuntu14:~/test-dir# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              14.04               7fde2af6297a        3 minutes ago       0B
<none>              <none>              1ba1618e54b7        19 minutes ago      1.85kB
test/reposiroty     v1r2c30             a5f9f6ffa235        About an hour ago   137MB
test/rep            v100                6929a35a93bc        About an hour ago   137MB
ubuntu              <none>              578c3e61a98c        11 hours ago        223MB
new/hello           v3                  e38bc07ac18e        7 weeks ago         1.85kB
hello-world         latest              e38bc07ac18e        7 weeks ago         1.85kB
ubuntu              15.10               9b9cb95443b5        22 months ago       137MB
training/webapp     latest              6fae60ef3446        3 years ago         349MB
#   如果要导出镜像到本地文件,可以使用 docker save 命令
root@Ubuntu14:~/test-dir# docker save -o new-hello_v3.tar new/hello:v3 root@Ubuntu14:~/test-dir# ls Dockerfile new-hello_v3.tar
# 如果要导出镜像到本地文件,可以使用 docker save 命令,可以使用"--input"或"<" root@Ubuntu14:~/test-dir# docker load --input new-hello_v3.tar Loaded image: new/hello:v3 root@Ubuntu14:~/test-dir# docker load < new-hello_v3.tar Loaded image: new/hello:v3 root@Ubuntu14:~/test-dir#
# 如果要移除本地的镜像,可以使用 docker rmi 命令。注意 docker rm 命令是移除容器
# *注意:在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器
root@Ubuntu14:~/test-dir# docker rmi new/hello Error: No such image: new/hello root@Ubuntu14:~/test-dir# docker rmi new/hello:v3 Untagged: new/hello:v3 root@Ubuntu14:~/test-dir# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu
14.04 7fde2af6297a 7 minutes ago 0B <none> <none> 1ba1618e54b7 23 minutes ago 1.85kB test/reposiroty v1r2c30 a5f9f6ffa235 About an hour ago 137MB test/rep v100 6929a35a93bc About an hour ago 137MB ubuntu <none> 578c3e61a98c 11 hours ago 223MB hello-world latest e38bc07ac18e 7 weeks ago 1.85kB ubuntu 15.10 9b9cb95443b5 22 months ago 137MB training/webapp latest 6fae60ef3446 3 years ago 349MB root@Ubuntu14:~/test-dir#

 

转载于:https://www.cnblogs.com/karl-python/p/9145919.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值