zoukankan      html  css  js  c++  java
  • Docker基础用法

    Docker基础用法


    什么是Docker

    Docker是一个开源的应用容器引擎,基于go语言开发并遵循了Apache2.0协议开源。

    Docker可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的Linux服务器,也可以实现虚拟化。

    容器是完全使用沙箱机制,相互之间不会有任何接口(类iphone的app),并且容器开销极其低。


    Docker中的容器:

    • lxc --> libcontainer --> runC

    img


    OCI&OCF

    OCI

    Open Container-initiative

    • 由Linux基金会主导于2015年6月创立
    • 旨在围绕容器格式和运行时制定一个开放的工业化标准
    • 包含两个规格
      • 运行规范(runtime-spec)
      • 镜像规格(image-spec)

    OCF

    Open Container Format

    runC是一个命令行工具,用于根据OCI规范生成和运行容器

    • 容器作为runC的子进程启动,并且可以嵌入到各种其他系统中,而无需运行守护进程
    • runC是建立在libcontainer之上的,libcontainer是一种支持数百万Docker引擎安装的容器技术

    docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com


    docker架构

    img

    Registry为网上的开源镜像资源网站,相当于仓库。里面的图标相当于网站里每个不同的镜像资源,dockerhost就是我们装有docker的主机,client为客户端,里面为各种命令。其中docker bulid为创建镜像。这些命令相当于交给装有docker主机的服务。containers为镜像启动后的容器。客户端和主机可以在一台主机里,但仓库客户端主机一般不会在一台主机里。如果这台主机要运行镜像的话,会使用docker run先在本地lmages查找有没此镜像,如果有就直接拿来用,如果本地没有则使用docker pull从网上下载到本地来使用,然后生成容器。


    docker镜像与镜像仓库

    为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。

    img

    镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。


    docker对象

    使用docker时,您正在创建和使用镜像、容器、网络、卷、插件和其他对象。

    • 镜像(IMAGES)
      • 镜像是一个只读模板,其中包含创建docker容器的说明。
      • 通常,一个镜像基于另一个镜像,并进行了一些额外的定制。
      • 你可以创建自己的镜像,也可以只使用其他人创建并在注册表中发布的镜像。
    • 容器(CONTAINERS)
      • conntainer是映像的可运行实例。

      • 你可以使用docker API或CLI创建、运行、停止、移动或删除容器。

      • 你可以将容器连接到一个或多个网络,将存储连接到容器,甚至可以基于其当前状态创建新映像。


    安装及使用docker

    docker安装

    //配置yum源
    [root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
    [root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
    [root@localhost ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo
    [root@localhost ~]# sed -i 's|^gpgcheck=1|gpgcheck=0|' /etc/yum.repos.d/CentOS-Base.repo
    [root@localhost ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
    [root@localhost ~]# sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
    [root@localhost ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
    [root@localhost ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/epel*
    [root@localhost ~]# sed -i 's|^gpgcheck=1|gpgcheck=0|' /etc/yum.repos.d/epel*
    [root@localhost ~]# cd /etc/yum.repos.d/
    [root@localhost yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
    [root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
    [root@localhost yum.repos.d]# ls
    CentOS-Base.repo   epel-playground.repo       epel-testing.repo
    docker-ce.repo     epel.repo                  redhat.repo
    epel-modular.repo  epel-testing-modular.repo
    
    [root@localhost ~]# yum clean all
    [root@localhost ~]# yum makecache
    
    //安装docker
    [root@localhost ~]# yum -y install docker-ce
    
    //开启自起并且立即启动
    [root@localhost ~]# systemctl enable --now docker
    

    docker加速

    docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

    docker的加速有多种方式:

    • docker cn
    • 中国科技大学加速器
    • 阿里云加速器

    阿里云加速器使用方法

    打开浏览器,访问阿里云官网

    img

    登录之后点击右上角”控制台“,在点击左上角“产品与服务“

    img

    在“弹性计算“里找到“容器镜像服务“,

    设定密码后选择“镜像加速器”

    img

    这里会有一个加速器地址

    img

    在/etc/docker目录下新建文件daemon.json将加速器地址写入

    [root@localhost ~]# vim /etc/docker/daemon.json
    {
      "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]  //这里的xxx看自己的加速器地址
    }
    

    在重启服务就好了

    [root@localhost ~]# systemctl daemon-reload
    [root@localhost ~]# systemctl restart docker
    

    docker常用操作

    命令 功能
    docker search 在Docker Hub中搜索镜像
    docker pull 从注册表中提取镜像或存储库
    docker images 列出镜像
    docker create 创建新容器
    docker start 启动一个或多个停止的容器
    docker run 在新容器中运行命令
    docker attach 连接到正在运行的容器
    docker ps 列出容器
    docker logs 取出容器的日志
    docker restart 重新启动容器
    docker stop 停止一个或多个正在运行的容器
    docker kill 杀死一个或多个正在运行的容器
    docker rm 移除一个或多个容器
    docker exec 在正在运行的容器中运行命令
    docker info 显示系统范围的信息
    docker inspect 返回Docker对象的低级信息

    //使用pull命令拉网上的镜像,不加版本默认为最新
    [root@localhost ~]# docker pull ubuntu
    Using default tag: latest
    latest: Pulling from library/ubuntu
    da7391352a9b: Pull complete 
    14428a6d4bcd: Pull complete 
    2c2d948710f2: Pull complete 
    Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
    Status: Downloaded newer image for ubuntu:latest
    docker.io/library/ubuntu:latest
    
    //需要指定版本要使用:加指定版本
    [root@localhost ~]# docker pull httpd:2.4-alpine
    2.4-alpine: Pulling from library/httpd
    801bfaa63ef2: Pull complete 
    ac8f86b44b17: Pull complete 
    078b6c86de97: Pull complete 
    55f318a9c48a: Pull complete 
    5da5afdb6ea0: Pull complete 
    Digest: sha256:17e9cafb91cbe2388a685d74c3ee2084d3bfbd144f839a5f2245b7f8ef350564
    Status: Downloaded newer image for httpd:2.4-alpine
    docker.io/library/httpd:2.4-alpine
    
    
    //使用images查看本地的镜像
    [root@localhost ~]# docker images
    REPOSITORY   TAG          IMAGE ID       CREATED        SIZE
    httpd        2.4-alpine   5d779ff71c18   2 months ago   55.5MB
    ubuntu       latest       f643c72bc252   3 months ago   72.9MB
    
    //使用search命令查找网上的镜像httpd
    [root@localhost ~]# docker search httpd
    NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    httpd                                   The Apache HTTP Server Project                  3371      [OK]       
    centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   36                   
    centos/httpd                                                                            33                   [OK]
    polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4                    [OK]
    salim1983hoop/httpd24                   Dockerfile running apache config                2                    [OK]
    lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h…   1                    [OK]
    ······
    
    //使用create在一个镜像中创建容器
    [root@localhost ~]# docker create httpd:2.4-alpine
    1ea365c279834f8a92b5b1f2d86b94f02311bdb49f5d2c7cf27e07a23862d664
    
    //使用ps -a查看所有的容器
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE              COMMAND              CREATED          STATUS    PORTS     NAMES
    1ea365c27983   httpd:2.4-alpine   "httpd-foreground"   29 seconds ago   Created             peaceful_hermann
    
    //使用start使容器启动
    [root@localhost ~]# docker start 1ea365c27983
    1ea365c27983
    [root@localhost ~]# docker ps
    CONTAINER ID   IMAGE              COMMAND              CREATED              STATUS         PORTS     NAMES
    1ea365c27983   httpd:2.4-alpine   "httpd-foreground"   About a minute ago   Up 9 seconds   80/tcp    peaceful_hermann
    
    //使用restart重启容器
    [root@localhost ~]# docker restart 1ea365c27983
    1ea365c27983
    
    //使用stop停止容器
    [root@localhost ~]# docker stop 1ea365c27983
    1ea365c27983
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE              COMMAND              CREATED         STATUS                      PORTS     NAMES
    1ea365c27983   httpd:2.4-alpine   "httpd-foreground"   3 minutes ago   Exited (0) 10 seconds ago             peaceful_hermann
    
    //使用kill强制终止容器(不推荐使用)
    [root@localhost ~]# docker start 1ea365c27983
    1ea365c27983
    [root@localhost ~]# docker kill 1ea365c27983
    1ea365c27983
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE              COMMAND              CREATED         STATUS                       PORTS     NAMES
    1ea365c27983   httpd:2.4-alpine   "httpd-foreground"   4 minutes ago   Exited (137) 5 seconds ago             peaceful_hermann
    
    //使用rm删除容器(删除时容器应处于停止状态,若容器还在运行则删除失败,可以使用rm -f强制删除)
    [root@localhost ~]# docker rm 1ea365c27983
    1ea365c27983
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    //使用rmi删除镜像(删除镜像时应没有容器在运行,如果镜像里有容器在运行,仅只会把镜像的名称删除,镜像依旧存在且镜像里的容器还会继续运行)
    [root@localhost ~]# docker rmi httpd:2.4-alpine 
    Untagged: httpd:2.4-alpine
    Untagged: httpd@sha256:17e9cafb91cbe2388a685d74c3ee2084d3bfbd144f839a5f2245b7f8ef350564
    Deleted: sha256:5d779ff71c188aa6da896ffdab929ca0cb1d859dc74650e57b9d8ce2bb6debff
    Deleted: sha256:7ff2770ecf5a3570c9ba55503b58520ebac9d4a1d84e5eb3b693060d337cdd69
    Deleted: sha256:9a48afd4f32baa214eb8c5bdf7012db015d2b08f788e72a67034fb42154d5497
    Deleted: sha256:af27dad83ccfd6e69381c331c1065c5f3673f0ffdff9497561c54dbfad8c072a
    Deleted: sha256:1e8b3e8b01c784685f9aea947db87649956275ee322a06461deba37bf969ae91
    Deleted: sha256:777b2c648970480f50f5b4d0af8f9a8ea798eea43dbcf40ce4a8c7118736bdcf
    [root@localhost ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    ubuntu       latest    f643c72bc252   3 months ago   72.9MB
    
    //使用run可以自动进行创建和运行容器
    [root@localhost ~]# docker pull httpd
    Using default tag: latest
    latest: Pulling from library/httpd
    a076a628af6f: Pull complete 
    e444656f7792: Pull complete 
    0ec35e191b09: Pull complete 
    4aad5d8db1a6: Pull complete 
    eb1da3ea630f: Pull complete 
    Digest: sha256:2fab99fb3b1c7ddfa99d7dc55de8dad0a62dbe3e7c605d78ecbdf2c6c49fd636
    Status: Downloaded newer image for httpd:latest
    docker.io/library/httpd:latest
    [root@localhost ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    httpd        latest    683a7aad17d3   6 weeks ago    138MB
    ubuntu       latest    f643c72bc252   3 months ago   72.9MB
    [root@localhost ~]# docker run httpd:latest 
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
    [Thu Feb 25 11:46:18.130204 2021] [mpm_event:notice] [pid 1:tid 140492191659136] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
    [Thu Feb 25 11:46:18.137559 2021] [core:notice] [pid 1:tid 140492191659136] AH00094: Command line: 'httpd -D FOREGROUND'
    (动不了,另外开一个终端)
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND              CREATED              STATUS              PORTS     NAMES
    67548e4ccd40   httpd:latest   "httpd-foreground"   About a minute ago   Up About a minute   80/tcp    nifty_jennings
    (用rm -f强制删除)
    [root@localhost ~]# docker rm -f 67548e4ccd40
    67548e4ccd40
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    //run加上-d使其在后台运行
    [root@localhost ~]# docker run -d httpd:latest 
    0f9f08236683ff604887e085062bda172453b416a229cd172bb696d24769e9d3
    [root@localhost ~]# docker ps
    CONTAINER ID   IMAGE          COMMAND              CREATED         STATUS         PORTS     NAMES
    0f9f08236683   httpd:latest   "httpd-foreground"   3 seconds ago   Up 3 seconds   80/tcp    condescending_germain
    
    //使用logs查看容器的日志
    [root@localhost ~]# docker logs 0f9f08236683
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
    [Thu Feb 25 11:50:10.567819 2021] [mpm_event:notice] [pid 1:tid 139657529361536] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
    [Thu Feb 25 11:50:10.568016 2021] [core:notice] [pid 1:tid 139657529361536] AH00094: Command line: 'httpd -D FOREGROUND'
    
    //使用inspect查看容器的各种信息,比如IP
    [root@localhost ~]# docker inspect 0f9f08236683
    ······
                "Networks": {
                    "bridge": {
                        "IPAMConfig": null,
                        "Links": null,
                        "Aliases": null,
                        "NetworkID": "9172bfd1c3c5fd4e07acda40d893b11802643875d4e05f922bad874a3188ed52",
                        "EndpointID": "868ad886be3824a6bb6b683b3fa325bb6e130be1f06bdb7f22e89f5fe0814d85",
                        "Gateway": "172.17.0.1",
                        "IPAddress": "172.17.0.2",
                        "IPPrefixLen": 16,
                        "IPv6Gateway": "",
                        "GlobalIPv6Address": "",
                        "GlobalIPv6PrefixLen": 0,
                        "MacAddress": "02:42:ac:11:00:02",
                        "DriverOpts": null
    					······
    
    //使用info查看整个docker的信息,比如正在运行的容器
    [root@localhost ~]# docker info
    Client:
     Context:    default
     Debug Mode: false
     Plugins:
      app: Docker App (Docker Inc., v0.9.1-beta3)
      buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
    ······
    
    //使用attach进入到容器的内部,但不能操作且退出时容器也会停止,不推荐使用。
    [root@localhost ~]# docker attach 0f9f08236683
    
    (另开一个终端访问它)
    [root@localhost ~]# curl 172.17.0.2
    <html><body><h1>It works!</h1></body></html>
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS          PORTS     NAMES
    0f9f08236683   httpd:latest   "httpd-foreground"   10 minutes ago   Up 32 seconds   80/tcp    condescending_germain
    
    (内部出现访问信息)
    172.17.0.1 - - [25/Feb/2021:12:00:12 +0000] "GET / HTTP/1.1" 200 45
    
    (Ctrl+C终止容器运行)
    ^C[Thu Feb 25 12:01:58.399252 2021] [mpm_event:notice] [pid 1:tid 139709888066688] AH00491: caught SIGTERM, shutting down
    
    //使用exec -it指定交互模式进入容器,比如/bin/bash或/bin/sh,由此可以实现操作且退出时容器不会停止
    [root@localhost ~]# docker exec -it 0f9f08236683 /bin/bash
    root@0f9f08236683:/usr/local/apache2# ls
    bin  build  cgi-bin  conf  error  htdocs  icons  include  logs	modules
    root@0f9f08236683:/usr/local/apache2# cd conf/
    root@0f9f08236683:/usr/local/apache2/conf# ls
    extra  httpd.conf  magic  mime.types  original
    root@0f9f08236683:/usr/local/apache2/conf# exit
    exit
    [root@localhost ~]# docker ps
    CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS         PORTS     NAMES
    0f9f08236683   httpd:latest   "httpd-foreground"   20 minutes ago   Up 2 minutes   80/tcp    condescending_germain
    

    img

  • 相关阅读:
    AttributeUsage属性
    LINQ基本子句
    js 变动首页参与人数
    C#控件的闪烁问题解决方法总结
    AttributeUsage
    MVC3 js+flash图片滚动
    通过写后台权限领悟到的东西
    删除同辈元素并添加指定元素的类
    JS产生两个数之间的随机数
    Web Service 学习笔记
  • 原文地址:https://www.cnblogs.com/yuqinghao/p/14451802.html
Copyright © 2011-2022 走看看