zoukankan      html  css  js  c++  java
  • Title

    Docker 镜像使用

    当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载

    • 1、管理和使用本地 Docker 主机镜像
    • 2、创建镜像

    列出镜像列表

    使用 docker images 来列出本地主机上的镜像

    tianbao@tianbao:~$ sudo docker images
    [sudo] password for tianbao: 
    REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
    thespaghettidetective_tasks    latest              02d25e3a394d        3 months ago        667MB
    thespaghettidetective_web      latest              02d25e3a394d        3 months ago        667MB
    thespaghettidetective_ml_api   latest              e3b814125b31        3 months ago        1.69GB
    redis                          5.0-alpine          58084f18c7ec        5 months ago        29.7MB
    

    各个选项说明:

    • REPOSITORY:表示镜像的仓库源
    • TAG:镜像的标签
    • IMAGE ID:镜像ID
    • CREATED:镜像创建时间
    • SIZE:镜像大小

    同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,使用 REPOSITORY:TAG 来定义不同的镜像

    我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下

    tianbao@tianbao:~$ sudo docker run -t -i ubuntu:15.10 /bin/bash
    root@13c7f2861f9e:/# 
    

    参数说明:

    • -i: 交互式操作。
    • -t: 终端。
    • ubuntu:15.10: 这是指用 ubuntu 15.10 版本镜像为基础来启动容器。
    • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash

    查找镜像

    。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像

    tianbao@tianbao:~$ sudo docker search httpd
    [sudo] password for tianbao: 
    NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    httpd                                   The Apache HTTP Server Project                  3247                [OK]                
    centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   36                                      
    centos/httpd                                                                            32                                      [OK]
    arm32v7/httpd                           The Apache HTTP Server Project                  9                                       
    polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4                                       [OK]
    

    NAME: 镜像仓库源的名称

    DESCRIPTION: 镜像的描述

    OFFICIAL: 是否 docker 官方发布

    stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。

    AUTOMATED: 自动构建。

    删除镜像

    docker rmi hello-world
    

    创建镜像

    当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

    • 1、从已经创建的容器中更新镜像,并且提交这个镜像
    • 2、使用 Dockerfile 指令来创建一个新的镜像

    更新镜像

    tianbao@tianbao:~$ docker run -t -i ubuntu:15.10 /bin/bash
    运行的容器内使用 apt-get update 命令进行更新
    在完成操作之后,输入 exit 命令来退出这个容器
    此时 ID 为 e6018f3274d9 的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本
    tianbao@tianbao:~$ sudo docker commit -m="has update" -a="tianbao" e6018f3274d9 tianbao/ubuntu:v2
    [sudo] password for tianbao: 
    sha256:a14eeb86cfbae1fb59419af4c8cc692f47670ad4ae9c4a260f6d34200de2855b
    tianbao@tianbao:~$ 
    

    各个参数说明:

    • -m: 提交的描述信息
    • -a: 指定镜像作者
    • e6018f3274d9:容器 ID
    • tianbao/ubuntu:v2: 指定要创建的目标镜像名

    我们可以使用 docker images 命令来查看我们的新镜像 tianbao/ubuntu:v2

    构建镜像

    使用命令 docker build , 从零开始来创建一个新的镜像

    查看Dockerfile

    tianbao@tianbao:~$ cat Dockerfile 
    FROM    centos:6.7
    MAINTAINER      Fisher "fisher@sudops.com"
    
    RUN     /bin/echo 'root:123456' |chpasswd
    RUN     useradd runoob
    RUN     /bin/echo 'tianbao:123456' |chpasswd
    RUN     /bin/echo -e "LANG="en_US.UTF-8"" >/etc/default/local
    EXPOSE  22
    EXPOSE  80
    CMD     /usr/sbin/sshd -D
    tianbao@tianbao:~$ 
    

    每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。

    第一条FROM,指定使用哪个镜像源

    RUN 指令告诉docker 在镜像内执行命令,安装了什么

    然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像

    tianbao@tianbao:~$ sudo docker build -t tianbao/centos:6.7 /opt/dockerfile_dir
    Sending build context to Docker daemon  2.048kB
    Step 1/9 : FROM    centos:6.7
    6.7: Pulling from library/centos
    cbddbc0189a0: Pull complete 
    Digest: sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1
    Status: Downloaded newer image for centos:6.7
     ---> 9f1de3c6ad53
    Step 2/9 : MAINTAINER      Fisher "fisher@sudops.com"
     ---> Running in 0ac1b8e2d526
    Removing intermediate container 0ac1b8e2d526
     ---> a3cd127c7514
    Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd
     ---> Running in dd0c94e37d33
    Removing intermediate container dd0c94e37d33
     ---> f86f7db7dc2b
    Step 4/9 : RUN     useradd tianbao
     ---> Running in 6017e761b09a
    Removing intermediate container 6017e761b09a
     ---> 53b877f544d1
    Step 5/9 : RUN     /bin/echo 'tianbao:123456' |chpasswd
     ---> Running in 52c21215fa5a
    Removing intermediate container 52c21215fa5a
     ---> 9c7ecc783848
    Step 6/9 : RUN     /bin/echo -e "LANG="en_US.UTF-8"" >/etc/default/local
     ---> Running in c0b1697e57a6
    Removing intermediate container c0b1697e57a6
     ---> 7e766d8ff22f
    Step 7/9 : EXPOSE  22
     ---> Running in 645a00b0ae00
    Removing intermediate container 645a00b0ae00
     ---> 67acca2f66e4
    Step 8/9 : EXPOSE  80
     ---> Running in 905531ba2c8c
    Removing intermediate container 905531ba2c8c
     ---> 848624472080
    Step 9/9 : CMD     /usr/sbin/sshd -D
     ---> Running in f8d08bd4c990
    Removing intermediate container f8d08bd4c990
     ---> 120002ff3d8a
    Successfully built 120002ff3d8a
    Successfully tagged tianbao/centos:6.7
    tianbao@tianbao:~$ 
    

    参数说明:

    • -t :指定要创建的目标镜像名
    • . :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

    关于Sending build context to Docker daemon 数据很大的问题

    Sending build context to Docker daemon 4.309 GB
    

    Docker client会发送Dockerfile同级目录下的所有文件到Dockerdaemon

    解决的方式就是之前第一句话说的,在一个新的文件夹中进行build这样子问题就不存在了

    第二种方式:在Dockerfile同级目录下添加一个 .dockerignore 文件

    在 .dockerignore 中添加需要忽略的文件或者文件夹即可

    使用docker images 查看创建的镜像已经在列表中存在,镜像ID为120002ff3d8a

    tianbao@tianbao:~$ sudo docker images 
    REPOSITORY                     TAG                 IMAGE ID            CREATED              SIZE
    tianbao/centos                 6.7                 120002ff3d8a        About a minute ago   191MB
    tianbao/ubuntu                 v2                  a14eeb86cfba        2 hours ago          137MB
    httpd                          latest              3dd970e6b110        4 weeks ago          138MB
    

    我们可以使用新的镜像来创建容器

    tianbao@tianbao:~$ sudo docker run -t -i tianbao/centos:6.7  /bin/bash
    [root@a67c12f04793 /]# id tianbao
    uid=500(tianbao) gid=500(tianbao) groups=500(tianbao)
    [root@a67c12f04793 /]# 
    

    从上面看到新镜像已经包含我们创建的用户 tianbao

    设置镜像标签

    使用 docker tag 命令,为镜像添加一个新的标签

    tianbao@tianbao:~$ sudo docker tag 120002ff3d8a tianbao/centos:dev
    tianbao@tianbao:~$ sudo docker images
    REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
    tianbao/centos                 6.7                 120002ff3d8a        5 minutes ago       191MB
    tianbao/centos                 dev                 120002ff3d8a        5 minutes ago       191MB
    

    docker tag 镜像ID,这里是 120002ff3d8a,用户名称、镜像源名(repository name)和新的标签名(tag)

  • 相关阅读:
    jfinal使用配置文件注意事情
    在项目中集成jetty server
    JFinal快速上手及注意事项
    表单处理的方案与注意事项(servlet)
    java四大会话技术
    servlet过滤器简化版
    python 生成器
    python 深复制和浅复制
    python 获取路径
    python selenium定位总结(转)
  • 原文地址:https://www.cnblogs.com/guotianbao/p/13959475.html
Copyright © 2011-2022 走看看