IT序号网

Docker 创建image知识解答

leader 2021年06月13日 程序员 150 0
 
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 
[root@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
 
总结

  1. 寻找获取新image
    • docker search centos
  2. 从docker hub 上pull下来
    • docker pull centos
  3. 启动一个容器
    • docker run  -t -i  centos /bin/bash
  4. 创建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 命令
  5. 给image增加标签
    • docker tag 7902e96eb014 sinatra:vv

发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

开始docker知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。