zoukankan      html  css  js  c++  java
  • docker 2:Docker镜像增删改查

    在1中我们已经完成docker的安装,接下来安装镜像。我们安装一个centos7的镜像。

    首先我们可以将docker设置成开机自启动:

    [root@localhost ~]#  systemctl enable docker.service
    Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
    [root@localhost ~]# systemctl enable containerd.service
    Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /usr/lib/systemd/system/containerd.service.
    

      然后开始下载镜像,我这里下载一个centos7的镜像文件,如下:

    [root@localhost ~]# docker pull centos:centos7
    centos7: Pulling from library/centos
    2d473b07cdd5: Downloading [===>                                               ]  4.823MB/76.1MB
    

      下载之前也可以搜索一下看哪个镜像更合适,命令如下:

    [root@localhost ~]# docker search centos
    NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    centos                             The official build of CentOS.                   6484      [OK]       
    ansible/centos7-ansible            Ansible on Centos7                              133                  [OK]
    consol/centos-xfce-vnc             Centos container with "headless" VNC session…   127                  [OK]
    jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   117                  [OK]
    centos/systemd                     systemd enabled base container.                 97                   [OK]
    centos/mysql-57-centos7            MySQL 5.7 SQL database server                   87                   
    imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              58                   [OK]
    tutum/centos                       Simple CentOS docker image with SSH access      47                   
    kinogmt/centos-ssh                 CentOS with SSH                                 29                   [OK]
    pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   13                   
    guyton/centos6                     From official centos6 container with full up…   10                   [OK]
    nathonfowlie/centos-jre            Latest CentOS image with the JRE pre-install…   8                    [OK]
    centos/tools                       Docker image that has systems administration…   7                    [OK]
    drecom/centos-ruby                 centos ruby                                     6                    [OK]
    pivotaldata/centos                 Base centos, freshened up a little with a Do…   5                    
    pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   3                    
    darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
    

      由于dockerhub默认使用的是国外源,下载速度较慢,可以修改为国内源,方式如下,修改或者创建/etc/docker/daemon.json文件,添加上国内源即可。

    [root@localhost ~]# vi /etc/docker/daemon.json
    
    {
    "registry-mirrors": ["http://hub-mirror.c.163.com"]
    }
    

      目前常用的国内源有:

    Docker 官方中国区
    https://registry.docker-cn.com
    网易
    http://hub-mirror.c.163.com
    中国科技大学
    https://docker.mirrors.ustc.edu.cn
    阿里云
    https://pee6w651.mirror.aliyuncs.com
    

      然后下载完成,可以看到如下:

    [root@localhost ~]# docker pull centos:centos7
    centos7: Pulling from library/centos
    2d473b07cdd5: Pull complete 
    Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
    Status: Downloaded newer image for centos:centos7
    docker.io/library/centos:centos7
    [root@localhost ~]# docker images
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    hello-world   latest    d1165f221234   3 weeks ago    13.3kB
    centos        centos7   8652b9f0cb4c   4 months ago   204MB
    

      接下来我们可以运行这个容器:

    [root@localhost ~]# docker run -it centos:centos7 /bin/bash
    [root@c1c3d9b90171 /]# ls
    bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    
    参数说明:cc9cf968068e
    -i: 交互式操作。
    -t: 终端。
    centos:centos7: 这是指用 centos版本镜像为基础来启动容器。centos7表示用版本7的镜像运行,若不加centos7则默认使用centos:latest运行。
    /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
    

      删除镜像:

    [root@localhost ~]# docker rmi hello-world
    Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container cc9cf968068e is using its referenced image d1165f221234
    

      我们看到在删除hello-world的时候出现报错,说容器cc9cf968068e正在使用该镜像,所以在删除镜像之前需要删除该镜像创建的容器,然后再删除镜像即可。

    [root@localhost ~]# docker rm cc9cf968068e
    cc9cf968068e
    [root@localhost ~]# docker rmi hello-world
    Untagged: hello-world:latest
    Untagged: hello-world@sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
    Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
    Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd
    

      若是要查看所有容器的ID或者所有镜像的ID,并针对镜像ID做操作可以使用如下方式

    [root@localhost ~]# docker images -q    #查看所有镜像ID
    d1165f221234
    300e315adb2f
    8652b9f0cb4c
    [root@localhost ~]# docker ps -aq         #查看所有容器ID
    e63520d23e40
    c1c3d9b90171
    301f9596af4d
    c6763b64f608
    8ce62505290a
    deca3b2f0da9
    [root@localhost ~]# docker stop $(docker ps -aq)     #停止所有容器
    e63520d23e40
    c1c3d9b90171
    301f9596af4d
    c6763b64f608
    8ce62505290a
    deca3b2f0da9
    [root@localhost ~]# docker rm $(docker ps -aq)        #删除所有容器
    e63520d23e40
    c1c3d9b90171
    301f9596af4d
    c6763b64f608
    8ce62505290a
    deca3b2f0da9
    [root@localhost ~]# docker rmi $(docker images -q)    #删除全部镜像
    d1165f221234
    300e315adb2f
    8652b9f0cb4c
    

      

  • 相关阅读:
    在每个类声明之后、每个函数定义结束之后都要加空行。
    不提倡使用全局变量
    头文件中只存放“声明”而不存放“定义”
    用 #include “filename.h” 格式来引用非标准库的头文件
    用 #include <filename.h> 格式来引用标准库的头文件
    为了防止头文件被重复引用
    java Excel导入、自适应版本、将Excel转成List<map>对象
    selenium用java找到表格某一行某一列中含有特定文字的某个元素
    关于java中创建文件,并且写入内容
    java把一个文件的内容复制到另外一个文件
  • 原文地址:https://www.cnblogs.com/tortoise512/p/14602292.html
Copyright © 2011-2022 走看看