zoukankan      html  css  js  c++  java
  • Docker

    Docker 简介

     

     

    1.什么是Docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux 机器上。使用Docker可以让每个应用彼此相互隔离,在同一台机器上同时运行多个容器,他们彼此之间共享同一个操作系统。Docker的优势在于,它可以在更细的粒度上进行资源的管理,比虚拟化技术性能更高,更加节约资源。下面是普通虚拟化技术和Docker的对比。

     

    2.Docker能做什么,解决了什么问题

    简化配置

             虚拟机的最大好处是能在你的硬件设施上运行各种配置不一样的平台(软件、系统),Docker在降低额外开销的情况下提供了同样的功能。它能让你将运行环境和配置放在代码中然后部署,同一个Docker的配置可以在不同的环境中使用,这样就降低了硬件要求和应用环境之间耦合度。

    提高开发和布署效率

      在软件开发过程中,我们都想把两件事做好,

             》开发环境和和投产环境一至

             》快速布署

             为了达到第一个目标我们要把程序跑在不同系统的服务器上,用大量的时间去测试功能是否完整。Docker可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的镜像,同一个镜像可以在不同的环境中使用,从而统一环境。

             Docker构建好镜像后,可以快速布署到任何流行的 Linux 机器上,不用再次配置和安装应用所需的依赖。最重要的是启动一个容器是秒级别的!

    隔离应用

             一台服务器上可能会跑不同的应用,每个应用都有自己的依赖。容易因为依赖的不两只造成冲突,Docker能把每个应用和他的依赖打包成独立的单元,做到资源隔离。

    其他的优点这里就不再做说明。

    Docker 三大核心概念

    镜像 - Docker images

    容器 - Docker containers

    仓库 - Docker repository

    Image

    Docker 镜像是 Docker 容器运行时的只读模板,每一个镜像由一系列的层 (layers) 组成。我们可以把Image理解成 windows系统安装光盘,你不能对安装盘是只读的,只有安装好系统后才能保存和操作数据。

    container

             容器是Image的一个实例,有点像面向对象里new 一个实例。容器类似于一个轻量级的沙盒,Docker利用容器来运行和隔离应用。容器从Image启动时,Docker会在镜像的最上层创建一个可写层,镜像本身保持不变。

    Repository

             Docker 仓库是存放镜像的地方。不要把Docker的仓库和注册服务器Registry混淆。注册服务器是存放仓库的地方,每个仓库存放某一类不同标签Tag的镜像。

     

             Docker 仓库也有公有和私有的概念。公有的 Docker 仓库名字是 Docker Hub。Docker Hub 提供了庞大的镜像集合供使用。这些镜像可以是自己创建,或者在别人的镜像基础上创建。如果你的镜像为私有镜像,不想发到公网上,可以自己在本地创建一个私有的镜像仓库。

    Install Docker

    https://www.docker.com/

    官网上有各种平台的详细安装过程,这里只描述Centos7平台下的安装过程。

    Docker需要安装在64位系统上,Linux内核不能低于3.10。通过 uname - r可以查看你的Liunx内核版本

     

    官方给出了两种方案安装Docker Engine,我们使用yun包管理进行安装。

    1.切换root权限。

    2.确保你的yum包是最新的

    $ sudo yum update

    3.添加 yum repo

    $ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'

    [dockerrepo]

    name=Docker Repository

    baseurl=https://yum.dockerproject.org/repo/main/centos/7/

    enabled=1

    gpgcheck=1

    gpgkey=https://yum.dockerproject.org/gpg

    EOF

    4.安装Docker

    $ sudo yum install docker-engine

    5.启动Docker

    $ sudo service docker start

    6.在容器内运行测试镜像,来检查Docker是否安装正确。

    $ sudo docker run hello-world

     

    为了能在系统启动时运行Docker,执行下面的命令

    $ sudo chkconfig docker on

    Docker基本用法

    Docker的默认服务器是hub.docker.com 他目前是最大的镜像网站,由于国情原因下载上边的镜像,实在是慢的让人受不了,可以搜一下国内的镜像网站我是用的这个(不是给这个网站做广告,确实挺下载快多了)https://hub.tenxcloud.com/

    拉取镜像

    $ docker pull centos # 获取 centos 官方镜像

    指定国内的镜像地址

    $ docker pull index.tenxcloud.com/tenxcloud/centos:centos7  #指定国内的镜像下载地址

    后边的:centos7是tag,表明我想下载的版本是cent0s7

    查看现有镜像

    docker images

     

    运行container

    我们来运行下载好的centos7

    docker run -i -t 6e75 /bin/bash

    docker run       - 运行一个容器

    -t                        - 分配一个(伪)tty

    -i                        - 交互模式

    6e75                - Image Id 我只输入了前几位,

                                - 只要能标识出唯一就行

    /bin/bash         - 运行命令 bash shell

     

    运行完命令后我们就已经进入了centos7容器,可以执行linux命令试一下。

    如果要退出输入exit即可。

    退出后容器也相应停止,如果想要在后台继续运行可以加可选参数 -d

    docker run -i -t -d 6e75 /bin/bash

    查看哪些容器在运行

    docker ps # 会列出在运行的

     

    在已存在的容器上运行命令

    如上边截图我们的centos7容器在运行,想在这个容器里运行命令可以用exec命令

    docker exec -t -i 875c /bin/bash

     

    Docker 命令帮助

    $ sudo docker   # docker 命令帮助

     

    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 the containers filesystem to the host path

                  # 从容器中拷贝指定文件或者目录到宿主机中

        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 an existing container        # 在已存在的容器上运行命令

        export    Stream the contents of a container as a tar archive  

                  # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

        history   Show the history of an image                  # 展示一个镜像形成历史

        images    List images                                   # 列出系统当前镜像

        import    Create a new filesystem image from the contents of a tarball 

                  # 从tar包中的内容创建一个新的文件系统映像[对应 export]

        info      Display system-wide information               # 显示系统相关信息

        inspect   Return low-level information on a container   # 查看容器详细信息

        kill      Kill a running container                      # kill 指定 docker 容器

        load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]

        login     Register or Login to the docker registry server  

                  # 注册或者登陆一个 docker 源服务器

        logout    Log out from a Docker registry server         # 从当前 Docker registry 退出

        logs      Fetch the logs of a container                 # 输出当前容器日志信息

        port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT

                  # 查看映射端口对应的容器内部源端口

        pause     Pause all processes within a container        # 暂停容器

        ps        List containers                               # 列出容器列表

        pull      Pull an image or a repository from the docker registry server

                  # 从docker镜像源服务器拉取指定镜像或者库镜像

        push      Push an image or a repository to the docker registry server

                  # 推送指定镜像或者库镜像至docker源服务器

        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 to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]

        search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像

        start     Start a stopped containers                    # 启动容器

        stop      Stop a running containers                     # 停止容器

        tag       Tag an image into a repository                # 给源中镜像打标签

        top       Lookup the running processes of a container   # 查看容器中运行的进程信息

        unpause   Unpause a paused container                    # 取消暂停容器

        version   Show the docker version information           # 查看 docker 版本号

        wait      Block until a container stops, then print its exit code  

                  # 截取容器停止时的退出状态值

  • 相关阅读:
    Verilog HDL Test Bench
    配置maven仓库
    mac上卸载oracle jdk 1.8.0_31
    Mac系统安装jdk和maven
    ActiveX的AssemblyInof.cs文件 IObjectSafety  接口
    C#破解dll
    Web Api 转
    dynamic
    无焦点窗体(转载)
    Linux操作系统基础知识part4
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5828424.html
Copyright © 2011-2022 走看看