• Docker 安装和基础用法


    Docker的三个基本概念

    从上图我们可以看到,Docker中包括三个基本的概念:

    • Image(镜像)
    • Container(容器)
    • Repository(仓库)

    1.Image (镜像)

    Docker镜像可以看作是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。

    镜像(Image)就是一堆只读层(read-only layer)的统一视角,也许这个定义有些难以理解,下面的这张图能够帮助读者理解镜像的定义。

     从左边我们看到了多个只读层,它们重叠在一起。除了最下面一层,其它层都会有一个指针指向下一层。这些层是Docker内部的实现细节,并且能够在主机的文件系统上访问到。统一文件系统 (union file system)技术能够将不同的层整合成一个文件系统,为这些层提供了一个统一的视角,这样就隐藏了多层的存在,在用户的角度看来,只存在一个文件系统。我们可以在图片的右边看到这个视角的形式。

    2.Container (容器)

    容器 (container)的定义和镜像 (image)几乎一模一样,也是一堆层的统一视角,唯一区别在于容器的最上面那一层是可读可写的。

     由于容器的定义并没有提及是否要运行容器,所以实际上,容器 = 镜像 + 读写层。

    3.Repository (仓库)

    Docker仓库是集中存放镜像文件的场所。镜像构建完成后,可以很容易的在当前宿主上运行,但是, 如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,Docker Registry(仓库注册服务器)就是这样的服务。有时候会把仓库 (Repository)和仓库注册服务器 (Registry)混为一谈,并不严格区分。

    仓库又可以分为两种形式:

    • public(公有仓库)
    • private(私有仓库)

     这张图展示了 Docker客户端、服务端和 Docker仓库(即 Docker Hub和 Docker Cloud),默认情况下Docker会在 Docker中央仓库寻找镜像文件,这种利用仓库管理镜像的设计理念类似于 Git,当然这个仓库是可以通过修改配置来指定的,甚至我们可以创建我们自己的私有仓库。

    安装docker

    卸载旧版本

    $ sudo yum remove docker 
                      docker-client 
                      docker-client-latest 
                      docker-common 
                      docker-latest 
                      docker-latest-logrotate 
                      docker-logrotate 
                      docker-engine

    安装 Docker Engine-Community

    使用 Docker 仓库进行安装

    在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker。

    设置仓库

    安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

    $ sudo yum install -y yum-utils 
      device-mapper-persistent-data 
      lvm2

    使用以下命令来设置稳定的仓库。

    $ sudo yum-config-manager 
        --add-repo 
        https://download.docker.com/linux/centos/docker-ce.repo

    安装 Docker Engine-Community

    安装最新版本的 Docker Engine-Community 和 containerd,或者转到下一步安装特定版本:

    $ sudo yum install docker-ce docker-ce-cli containerd.io

    要安装特定版本的 Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:

    1、列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

    $ yum list docker-ce --showduplicates | sort -r
    
    docker-ce.x86_64  3:18.09.1-3.el7                     docker-ce-stable
    docker-ce.x86_64  3:18.09.0-3.el7                     docker-ce-stable
    docker-ce.x86_64  18.06.1.ce-3.el7                    docker-ce-stable
    docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable

    2、通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1。

    $ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

    启动 Docker。

    $ sudo systemctl start docker

    通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。

    $ sudo docker run hello-world

    docker容器状态

    一个容器在某个时刻可能处于以下几种状态之一:

    • created:已经被创建 (使用 docker ps -a 命令可以列出)但是还没有被启动 (使用 docker ps 命令还无法列出)
    • running:运行中
    • paused:容器的进程被暂停了
    • restarting:容器的进程正在重启过程中
    • exited:上图中的 stopped 状态,表示容器之前运行过但是现在处于停止状态(要区别于 created 状态,它是指一个新创出的尚未运行过的容器)。可以通过 start 命令使其重新进入 running 状态
    • destroyed:容器被删除了,再也不存在了

    你可以在 docker inspect 命令的输出中查看其详细状态:

    "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 4597,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2016-09-16T08:09:34.53403504Z",
                "FinishedAt": "2016-09-16T08:06:44.365106765Z"
            }

    docker常用命令

    子命令分类子命令
    Docker环境信息 info、version
    容器生命周期管理 create、exec、kill、pause、unpause、start、stop、restart、run、rm
    镜像仓库命令 login、logout、pull 、push、search
    镜像管理 build、images、import、load、rmi、save、tag、commit
    容器运维操作 attach、export、inspect、port、ps、rename、stats、top、wait、cp、diff、update
    容器资源管理 volume、network
    系统日志信息 events、history、logs

    我们可以把Docker 的命令大概地分类如下:

    镜像操作:
        build     Build an image from a Dockerfile
        commit    Create a new image from a container's changes
        images    List images
        load      Load an image from a tar archive or STDIN
        pull      Pull an image or a repository from a registry
        push      Push an image or a repository to a registry
        rmi       Remove one or more images
        search    Search the Docker Hub for images
        tag       Tag an image into a repository
        save      Save one or more images to a tar archive (streamed to STDOUT by default)
        history   显示某镜像的历史
        inspect   获取镜像的详细信息
    
        容器及其中应用的生命周期操作:
        create    Create a new container (创建一个容器)        
        kill      Kill one or more running containers
        inspect   Return low-level information on a container, image or task
        pause     Pause all processes within one or more containers
        ps        List containers
        rm        Remove one or more containers (删除一个或者多个容器)
        rename    Rename a container
        restart   Restart a container
        run       Run a command in a new container (创建并启动一个容器)
        start     Start one or more stopped containers (启动一个处于停止状态的容器)
        stats     Display a live stream of container(s) resource usage statistics (显示容器实时的资源消耗信息)
        stop      Stop one or more running containers (停止一个处于运行状态的容器)
        top       Display the running processes of a container
        unpause   Unpause all processes within one or more containers
        update    Update configuration of one or more containers
        wait      Block until a container stops, then print its exit code
        attach    Attach to a running container
        exec      Run a command in a running container
        port      List port mappings or a specific mapping for the container
        logs      获取容器的日志    
        
        容器文件系统操作:
        cp        Copy files/folders between a container and the local filesystem
        diff      Inspect changes on a container's filesystem
        export    Export a container's filesystem as a tar archive
        import    Import the contents from a tarball to create a filesystem image
        
        Docker registry 操作:
        login     Log in to a Docker registry.
        logout    Log out from a Docker registry.
        
        Volume 操作
        volume    Manage Docker volumes
        
        网络操作
        network   Manage Docker networks
        
        Swarm 相关操作
        swarm     Manage Docker Swarm
        service   Manage Docker services
        node      Manage Docker Swarm nodes       
        
        系统操作:    
        version   Show the Docker version information
        events    Get real time events from the server  (持续返回docker 事件)
        info      Display system-wide information (显示Docker 主机系统范围内的信息)

     比较有意思的几个命令:

    root@devstack:/home/sammy# docker create --name web31 training/webapp python app.py  #创建名字为 web31 的容器
    7465f4cb7c49555af32929bd1bc4213f5e72643c0116450e495b71c7ec128502
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' web31 #其状态为 created
    created
    root@devstack:/home/sammy# docker start web31 #启动容器
    web31
    root@devstack:/home/sammy# docker exec -it web31 /bin/bash #在容器中运行 bash 命令
    
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' web31 #其状态为 running
    running
    root@devstack:/home/sammy# docker pause web31 #暂停容器
    web31
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' web31
    paused
    root@devstack:/home/sammy# docker unpause web31 #继续容器
    web31
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' web31
    running
    root@devstack:/home/sammy# docker rename web31 newweb31 #重命名
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' newweb31
    running
    root@devstack:/home/sammy# docker top newweb31 #在容器中运行 top 命令
    UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
    root                5009                4979                0                   16:28               ?                   00:00:00            python app.py
    root@devstack:/home/sammy# docker logs newweb31 #获取容器的日志
     * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    root@devstack:/home/sammy# docker stop newweb31 #停止容器
    newweb31
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' newweb31
    exited
    root@devstack:/home/sammy# docker rm newweb31 #删除容器
    newweb31
    root@devstack:/home/sammy# docker inspect --format='{{.State.Status}}' newweb31
    Error: No such image, container or task: newweb31
  • 相关阅读:
    旧贴-在 win7 / win8 下安装苹果系统 (懒人版)
    解决ios13摇一摇不能触发
    html+css面试合集
    Windows 2012 Server R2 添加用户
    Windows10专业版身份验证错误,可能由于CredSSP加密数据库修正
    STM32F4 7.STM32F4 独立看门狗
    STM32F4 6.STM32F4 外部中断
    STM32F4 5.STM32F4串口通讯
    STM32F4 4.STM32F4时钟系统
    STM32F4 3.GPIO按键输入,实现开关灯
  • 原文地址:https://www.cnblogs.com/lizhewei/p/11592258.html
Copyright © 2020-2023  润新知