zoukankan      html  css  js  c++  java
  • Docker容器介绍

    Docker容器介绍

    docker三个基本概念

    镜像(Image)容器(Container)仓库(Repository)解释来自w3cschool

    Docker 镜像
    Docker 镜像就是一个只读的模板。
    
    例如:一个镜像可以包含一个完整的 ubuntu 操作系统环境,里面仅安装了 Apache 或用户需要的其它应用程序。
    
    镜像可以用来创建 Docker 容器。
    
    Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,用户甚至可以直接从其他人那里下载一个已经做好的镜像来直接使用。
    
    Docker容器的运用
    Docker 利用容器来运行应用。
    
    容器是从镜像创建的运行实例。它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全的平台。
    
    可以把容器看做是一个简易版的 Linux 环境(包括root用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。
    
    *注:镜像是只读的,容器在启动的时候创建一层可写层作为最上层。
    
    Docker仓库
    仓库是集中存放镜像文件的场所。有时候会把仓库和仓库注册服务器(Registry)混为一谈,并不严格区分。实际上,仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)。
    
    仓库分为公开仓库(Public)和私有仓库(Private)两种形式。
    
    最大的公开仓库是 Docker Hub,存放了数量庞大的镜像供用户下载。 国内的公开仓库包括 Docker Pool 等,可以提供大陆用户更稳定快速的访问。
    
    当然,用户也可以在本地网络内创建一个私有仓库。
    
    当用户创建了自己的镜像之后就可以使用 push 命令将它上传到公有或者私有仓库,这样下次在另外一台机器上使用这个镜像时候,只需要从仓库上 pull 下来就可以了。
    

    系统要求

    Docker 对CentOS的版本:
    CentOS 7 (64-bit)
    CentOS 6.5  (64-bit)或者更高的版本
    前提条件:
    Docker 运行在CentOS 7 上,要求系统为64位、系统内核为3.10以上
    Docker 运行在CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为2.6.32-431 或者更高版本。
    

    国内源安装docker-ce

    参考清华源的操作手册:
    https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/
    操作步骤:
    yum remove docker docker-common docker-selinux docker-engine
    安装依赖包
    yum install -y yum-utils device-mapper-persistent-data lvm2
    
    wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
    
    sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
    
    yum makecache fast
    
    yum install docker-ce
    启动docker服务
    systemctl daemon-reload
    systemctl restart docker  #启动docker
    systemctl enable docker    # 加入开机自启动
    查看docker版本
    docker version
    docker  info
    

    国内远镜像加速配置

    [root@docker01 ~]# cat /etc/docker/daemon.json 
    {
        "registry-mirrors": ["http://hub-mirror.c.163.com"]
    }
    [root@docker01 ~]# systemctl restart docker
    第二种
    阿里云Docker-hub
    https://cr.console.aliyun.com/cn-hangzhou/mirrors
    
    mkdir -p /etc/docker
    tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://uoggbpok.mirror.aliyuncs.com"]
    }
    EOF
    
    systemctl daemon-reload
    systemctl restart docker
    	  	  
    或者:
    vim   /etc/docker/daemon.json
    
    	{
    		 "registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"]
    	}	
    

    docker基本命令

    [root@docker ~]# docker --help
    Usage:
    docker [OPTIONS] COMMAND [arg...]
           docker daemon [ --help | ... ]
           docker [ --help | -v | --version ]
    
    A
    self-sufficient runtime for containers.
    
    Options:
          --config string      Location of client config files (default "/root/.docker")    #客户端配置文件的位置
      -D, --debug=false               Enable debug mode  #启用Debug调试模式
      -H, --host=[]                   Daemon socket(s) to connect to  #守护进程的套接字(Socket)连接
      -h, --help=false                Print usage  #打印使用
      -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")    #设置日志级别
          --tls                Use TLS; implied by --tlsverify
          --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")    #信任证书签名CA
          --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")    #TLS证书文件路径
          --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")    #TLS密钥文件路径
          --tlsverify          Use TLS and verify the remote    #使用TLS验证远程
      -v, --version            Print version information and quit    #打印版本信息并退出
    
    Commands:
        attach    Attach to a running container  #当前shell下attach连接指定运行镜像
        build     Build an image from a Dockerfile  #通过Dockerfile定制镜像
        commit    Create a new image from a container's changes  #提交当前容器为新的镜像
        cp    Copy files/folders from a container to a HOSTDIR or to STDOUT  #从容器中拷贝指定文件或者目录到宿主机中
        create    Create a new container  #创建一个新的容器,同run 但不启动容器
        diff    Inspect changes on a container's filesystem  #查看docker容器变化
        events    Get real time events from the server  #从docker服务获取容器实时事件
        exec    Run a command in a running container   #在已存在的容器上运行命令
        export    Export a container's filesystem as a tar archive  #导出容器的内容流作为一个tar归档文件(对应import)
        history    Show the history of an image  #展示一个镜像形成历史
        images    List images  #列出系统当前镜像
        import    Import the contents from a tarball to create a filesystem image  #从tar包中的内容创建一个新的文件系统映像(对应export)
        info    Display system-wide information  #显示系统相关信息
        inspect    Return low-level information on a container or image  #查看容器详细信息
        kill    Kill a running container  #kill指定docker容器
        load    Load an image from a tar archive or STDIN  #从一个tar包中加载一个镜像(对应save)
        login    Register or log in to a Docker registry    #注册或者登陆一个docker源服务器
        logout    Log out from a Docker registry  #从当前Docker registry退出
        logs    Fetch the logs of a container  #输出当前容器日志信息
        pause    Pause all processes within a container   #暂停容器
        port    List port mappings or a specific mapping for the CONTAINER  #查看映射端口对应的容器内部源端口
        ps    List containers  #列出容器列表
        pull    Pull an image or a repository from a registry  #从docker镜像源服务器拉取指定镜像或者库镜像
        push    Push an image or a repository to a registry  #推送指定镜像或者库镜像至docker源服务器
        rename    Rename a container  #重命名容器
        restart    Restart a running container  #重启运行的容器
        rm    Remove one or more containers  #移除一个或者多个容器
        rmi    Remove one or more images  #移除一个或多个镜像(无容器使用该镜像才可以删除,否则需要删除相关容器才可以继续或者-f强制删除)
        run    Run a command in a new container  #创建一个新的容器并运行一个命令
        save    Save an image(s) to a tar archive    #保存一个镜像为一个tar包(对应load)
        search    Search the Docker Hub for images  #在docker hub中搜索镜像
        start    Start one or more stopped containers   #启动容器
        stats    Display a live stream of container(s) resource usage statistics  #统计容器使用资源
        stop    Stop a running container  #停止容器
        tag         Tag an image into a repository  #给源中镜像打标签
        top       Display the running processes of a container #查看容器中运行的进程信息
        unpause    Unpause all processes within a container  #取消暂停容器
        version    Show the Docker version information   #查看容器版本号
        wait         Block until a container stops, then print its exit code  #截取容器停止时的退出状态值
    
    Run 'docker COMMAND --help' for more information on a command.  #运行docker命令在帮助可以获取更多信息
    

    Docker镜像管理

    1.镜像相关命令

    搜索镜像
    选择镜像建议:
    1.优先选择官方的
    2.选择星星多的
    docker search centos  #搜索所有的centos的docker镜像
    [root@yzhe ~]# docker search centos
    NAME (名称)                              DESCRIPTION  描述                                   STARS  下载次数             OFFICIAL  官方          AUTOMATED自动化
    centos                             The official build of CentOS.                   5967                [OK]                
    ansible/centos7-ansible            Ansible on Centos7                              128                                     [OK]
    jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos - …   114                                     [OK]
    consol/centos-xfce-vnc             Centos container with "headless" VNC session…   114                                     [OK]
    centos/mysql-57-centos7            MySQL 5.7 SQL database server                   75                                      
    imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              58                                      [OK]
    tutum/centos                       Simple CentOS docker image with SSH access      46                                      
    centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   43                                      
    kinogmt/centos-ssh                 CentOS with SSH                                 29                                      [OK]
    pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   11                                      
    guyton/centos6                     From official centos6 container with full up…   10                                      [OK]
    drecom/centos-ruby                 centos ruby                                     6                                       [OK]
    centos/tools                       Docker image that has systems administration…   6                                       [OK]
    pivotaldata/centos                 Base centos, freshened up a little with a Do…   4                                       
    darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
    pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   3                                       
    mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
    pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   3                                       
    miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
    indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
    mcnaughton/centos-base             centos base image                               1                                       [OK]
    blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
    pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                                       
    pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                                       
    smartentry/centos                  centos with smartentry                          0                                       [OK]
    
    
    docker search 参数说明:
    --automated=true I false:仅显示自动创建的镜像,默认为否; 
    --no-trunc=true | false:输出信息不截断显示,默认为否;
    -s,--stars=X:指定仅显示评价为指定星级以上的镜像,默认为 0,即输出所有镜像。
    例如,搜索所有自动创建的评价为 3+的带 nginx 关键字的镜像,如下所示:
    
    [root@yzhe ~]# docker search --automated -s 3 nginx 
    
    docker search 其它用法
    
    获取镜像
    docker    pull    [选项]    [Docker    Registry地址]<仓库名>:<标签>
    docker pull centos #获取centos镜像
    docker pull busybox
    docker pull busybox:1.29
    
    
    查看镜像docker images或者docker image ls
    [root@yzhe ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              latest              470671670cac        3 months ago        237MB
    centos              centos7.5.1804      cf49811e3cdb        13 months ago       200MB
    
    

    删除镜像docker rmi

    docker    rmi    [选项]    <镜像1>    [<镜像2>    ...]
    使用docker rmi 删除本地镜像,后面可以跟镜像ID或者标签。(删除镜像之前先用docker rm 删除依赖于这个镜像的所有容器)。注意docker rm 命令是移除容器。
    docker rmi centos
    [root@docker ~]# docker image rm -f `docker image ls -q`
    
    导出镜像
    docker save centos > /tmp/docker_centos.tar    #导出docker镜像到本地
    docker image save nginx > /tmp/docker_nginx.tar    #导出docker镜像到本地
    

    导入镜像

    导入镜像 docker load
    从本地文件中导入docker镜像库
    [root@yzhe ~]# docker load < /tmp/docker_centos.tar    #导入本地镜像到docker镜像库
    [root@yzhe ~]# docker image load < /tmp/docker_nginx.tar  #新版本也可以用这种方法导入
    [root@yzhe ~]# docker image ls  #查看导入的情况
    

    给镜像打标签 docker tag

    [root@yzhe ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              centos7.5.1804      cf49811e3cdb        13 months ago       200MB
    [root@yzhe ~]# docker tag centos:centos7.5.1804 centos:7.5
    [root@yzhe ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              7.5                 cf49811e3cdb        13 months ago       200MB
    centos              centos7.5.1804      cf49811e3cdb        13 months ago       200MB
    [root@yzhe ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              7.5                 cf49811e3cdb        13 months ago       200MB
    centos              centos7.5.1804      cf49811e3cdb        13 months ago       200MB
    [root@yzhe ~]# docker rmi centos:7.5
    Untagged: centos:7.5
    [root@yzhe ~]# docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos              centos7.5.1804      cf49811e3cdb        13 months ago       200MB
    
    

    2.容器相关命令

    启动容器
    docker run -d -p 80:80 nginx 
    docker run --name mydocker -t -i centos /bin/bash
    

    停止容器

    docker stop 
    

    查看容器

    docker ps 
    docker ps -a 
    docker ps -q
    docker ps -aq
    

    导入和导出容器

    导出容器
    如果要导出本地某个容器,可以使用 docker export 命令,导出容器是指导出一个已经创建的容器到一个文件,不管此时这个容器是否处于运行状态。export后面跟需要导出的 容器ID 或者 容器名字 都可以
    
    [root@server ~]# docker export mydocker > centos.tar  #导出一个容器,也可以使用docker export -o centos.tar mydocker。 -o 指定导出的名字
    导入容器
    
    导出的文件又可以使用 docker import 命令导入变成镜像,例如
    
    [root@server ~]# cat centos.tar | docker import - test/centos:7.3
    sha256:a8375a86d721a718d70dc99a49005d70ce6a7b65423c1bbd1ce34b23ec787aa9
    [root@server ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    test/centos         7.3                 a8375a86d721        7 seconds ago       202MB
    

    进入容器

    docker exec会分配一个新的终端tty
    docker exec -it 容器ID  /bin/bash 
    docker attach会使用同一个终端
    docker attach 容器ID
    

    删除容器

    删除单个容器
    
    docker rm 容器ID
    批量删除容器
    
    docker stop $(docker ps -q)
    docker rm $(docker ps -aq)
    

    Docker网络管理

    随机映射端口

    docker run -P 
    

    指定映射端口

    -p 80:80 -p 443:443
    -p 宿主机IP:宿主机端口:容器端口
    

    如果想多个容器使用8080端口,可以通过添加多个IP地址实现

    ifconfig eth0:1 10.0.1.13 up
    docker run -d -p 10.0.1.11:8080:80 nginx:latest
    docker run -d -p 10.0.1.13:8080:80 nginx:latest
    

    进入容器里修改站点目录,然后访问测试

    docker exec -it bdb2a4e7e24d /bin/bash
    echo "web01" > /usr/share/nginx/html/index.html
    docker exec -it 31c1de138dda /bin/bash
    echo "web02" > /usr/share/nginx/html/index.html
    

    访问测试:

    [root@docker01 ~]# curl 10.0.1.11:8080
    web02
    [root@docker01 ~]# curl 10.0.1.13:8080
    web01
    
  • 相关阅读:
    微信开发 之 开启开发模式
    微信公众号开发 之 编辑模式使用
    分析各种Android设备屏幕分辨率与适配
    【面向对象设计模式】 适配器模式 (二)
    重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改
    【Android 应用开发】Android资源文件
    java 创建并写入文件
    隐藏 HttpClient 在console的日志
    HOW TO CHANGE THE DEFAULT KEY-VALUE SEPARATOR OF A MAPREDUCE JOB
    java 时间戳转换
  • 原文地址:https://www.cnblogs.com/carson-yz/p/12812597.html
Copyright © 2011-2022 走看看