zoukankan      html  css  js  c++  java
  • 第4章 Docker常用操作

    第4章 Docker常用操作

    4.1. 安装启动docker
        4.1.1. 安装环境 
        4.1.2. 安装docker
            4.1.2.1. 使用官方安装脚本自动安装
            4.1.2.2. CentOS 7 (使用yum安装,推荐)
            4.1.2.3. CentOS 7 (使用二进制安装)
            4.1.2.4. Ubuntu 14.04 16.04 (使用apt-get进行安装)
        4.1.3. 启动docker  
            4.1.3.1. 配置docker镜像加速
            4.1.3.2. 配置清空防火墙规则
            4.1.3.3. 启动docker服务
    4.2. 镜像操作
        4.2.1. 搜索官方仓库镜像
        4.2.2. 拉去镜像
        4.2.3. 导出镜像
        4.2.4. 删除镜像
        4.2.5. 导入镜像
        4.2.6. 查看镜像的详细信息
    4.3. 容器操作
        4.3.1. 启动容器
        4.3.2. 停止运行的容器
        4.3.3. 激活关闭的容器
        4.3.4. 查看容器的详细信息
        4.3.5. 删除容器
        4.3.6. 对运行的容器执行指定命令exec
        4.3.7. 查询容器内部日志
    

    4.1. 安装启动docker

    4.1.1. 安装环境

    依赖的基础环境

    • 64位CPU;
    • Linux kernel(内核) 3.10+
    • Linux kernel cgroups and namespaces

    查询自己服务器的环境

    # 使用的服务器版本
    [root@localhost ~]# cat /etc/redhat-release
    CentOS Linux release 7.4.1708 (Core)
    
    # 内核版本
    [root@localhost ~]# uname -r
    3.10.0-693.el7.x86_64
    
    # ip地址
    [root@localhost ~]# hostname -I
    192.168.100.61 10.0.2.4 172.17.0.1
    
    

    4.1.2. 安装docker

    Docker CE是免费的Docker产品的新名称,Docker CE 包含了完整的Docker平台,非常适合开发人员和运维团队构建容器APP。

    4.1.2.1. 使用官方安装脚本自动安装

    实际上就是下载一个安装脚本,再执行安装(不推荐,因为不能选择版本安装)

    [root@localhost ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    

    4.1.2.2. CentOS 7 (使用yum安装,推荐)

    # step 1: 安装必要的一些系统工具
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    
    # Step 2: 添加软件源信息
    sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
    # Step 3: 更新并安装Docker-CE
    sudo yum makecache fast
    sudo yum -y install docker-ce
    
    # Step 4: 开启Docker服务
    sudo service docker start
    
    # 注意:
    # 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
    # vim /etc/yum.repos.d/docker-ee.repo
    #   将[docker-ce-test]下方的enabled=0修改为enabled=1
    #
    # 安装指定版本的Docker-CE:
    # Step 1: 查找Docker-CE的版本:
    # yum list docker-ce.x86_64 --showduplicates | sort -r
    #   Loading mirror speeds from cached hostfile
    #   Loaded plugins: branch, fastestmirror, langpacks
    #   docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable
    #   docker-ce.x86_64            17.03.1.ce-1.el7.centos            @docker-ce-stable
    #   docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable
    #   Available Packages
    # Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)
    # sudo yum -y install docker-ce-[VERSION]
    

    4.1.2.3. CentOS 7 (使用二进制安装)

    # 到 https://download.docker.com/linux/static/stable/x86_64/ 页面下载自己需要版本的发布包:
    # 这次安装文档版本 docker-18.03.1-ce
    # 下载安装
    [root@localhost ~]# mkdir /data
    [root@localhost ~]# wget -P /data/ https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz
    [root@localhost ~]# cd /data/
    [root@localhost ~]# tar -xvf docker-18.03.1-ce.tgz
    
    # 配置启动脚本
    [root@localhost ~]# vim /etc/systemd/system/docker.service
    [Unit]
    Description=Docker Application Container Engine
    Documentation=http://docs.docker.io
    
    [Service]
    Environment="PATH=/data/docker/:/bin:/sbin:/usr/bin:/usr/sbin"
    EnvironmentFile=-/run/flannel/docker
    ExecStart=/data/docker/dockerd --log-level=error $DOCKER_NETWORK_OPTIONS
    ExecReload=/bin/kill -s HUP $MAINPID
    Restart=on-failure
    RestartSec=5
    LimitNOFILE=infinity
    LimitNPROC=infinity
    LimitCORE=infinity
    Delegate=yes
    KillMode=process
    
    [Install]
    WantedBy=multi-user.target
    
    # 配置生效环境变量,方便使用docker命令
    [root@localhost ~]# vim /etc/profile.d/docker.sh
    export PATH=/data/docker:$PATH
    [root@localhost ~]# source /etc/profile.d/docker.sh
    
    # 配置docker命令补齐脚本
    [root@localhost ~]# wget -O /usr/share/bash-completion/completions/docker https://raw.githubusercontent.com/alonghub/Docker/master/Resource/docker
    
    # 配置dockerfile 语法高亮脚本
    [root@localhost ~]# wget -O /usr/share/vim/vimfiles/doc/dockerfile.txt https://raw.githubusercontent.com/alonghub/Docker/master/Resource/dockerfile.txt
    [root@localhost ~]# wget -O /usr/share/vim/vimfiles/ftdetect/dockerfile.vim https://raw.githubusercontent.com/alonghub/Docker/master/Resource/dockerfile2.vim
    [root@localhost ~]# wget -O /usr/share/vim/vimfiles/syntax/dockerfile.vim https://raw.githubusercontent.com/alonghub/Docker/master/Resource/dockerfile3.vim
    

    4.1.2.4. Ubuntu 14.04 16.04 (使用apt-get进行安装)

    # step 1: 安装必要的一些系统工具
    sudo apt-get update
    sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
    
    # step 2: 安装GPG证书
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    
    # Step 3: 写入软件源信息
    sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    
    # Step 4: 更新并安装Docker-CE
    sudo apt-get -y update
    sudo apt-get -y install docker-ce
    
    # 安装指定版本的Docker-CE:
    # Step 1: 查找Docker-CE的版本:
    # apt-cache madison docker-ce
    #   docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
    #   docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
    
    # Step 2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.1~ce-0~ubuntu-xenial)
    sudo apt-get -y install docker-ce=[VERSION]
    

    4.1.2.5. 安装效验

    [admin@localhost ~]$ sudo docker version
    Client: Docker Engine - Community
     Version:           19.03.6
     API version:       1.40
     Go version:        go1.12.16
     Git commit:        369ce74a3c
     Built:             Thu Feb 13 01:29:29 2020
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          19.03.6
      API version:      1.40 (minimum version 1.12)
      Go version:       go1.12.16
      Git commit:       369ce74a3c
      Built:            Thu Feb 13 01:28:07 2020
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.2.10
      GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
     runc:
      Version:          1.0.0-rc8+dev
      GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
     docker-init:
      Version:          0.18.0
      GitCommit:        fec3683
    

    4.1.3. 启动docker

    4.1.3.1. 配置docker镜像加速

    多种加速方式:

    • docker cn
    • 阿里云加速器
    • 中国科技大学
    # docker cn 加速
    [root@along ~]# mkdir -p /etc/docker
    [root@along ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://registry.docker-cn.com"]
    }
    EOF
    
    
    # 阿里云镜像加速器
    使用加速器可以提升获取Docker官方镜像的速度
    我的加速器地址:https://qf9s09fg.mirror.aliyuncs.com
    # 获取加速器地址方式:
    阿里云加速器获取地址: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
    # 配置镜像加速器
    针对Docker客户端版本大于1.10.0的用户 可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
    
    sudo mkdir -p /etc/docker
    udo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://qf9s09fg.mirror.aliyuncs.com"]
    }
    EOF
    

    4.1.3.2. 配置清空防火墙规则

    [root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
    [root@localhost ~]# /usr/sbin/iptables -F && /usr/sbin/iptables -X && /usr/sbin/iptables -F -t nat && /usr/sbin/iptables -X -t nat
    [root@localhost ~]# /usr/sbin/iptables -P FORWARD ACCEPT
    

    4.1.3.3. 启动docker服务

    # 重新加载docker启动配置
    [root@localhost ~]# systemctl daemon-reload
    
    # 将docker设为开机自启
    [root@localhost ~]# systemctl enable docker
    
    # 启动docker服务
    [root@localhost ~]# systemctl start docker
    
    # 查看docker版本
    [root@localhost ~]# docker version
    

    4.2. 镜像操作

    4.2.1. 搜索官方仓库镜像

    [root@localhost ~]# docker search nginx
    NAME                               DESCRIPTION                                     STARS               OFFICIAL     AUTOMATED
    nginx                              Official build of Nginx.                        12749               [OK]
    jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1748     [OK]
    
    # 搜索结果解释:
    NAME            # 镜像名称
    DESCRTIPTION    # 镜像说明
    STARS           # 点赞数量
    OFFICIAL        # 是否是官方的
    AUTOMAATED      # 是否自动构建的
    

    4.2.2. 拉取镜像

    # 拉取镜像
    [root@localhost ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    68ced04f60ab: Pull complete
    28252775b295: Pull complete
    a616aa3b0bf2: Pull complete
    Digest: sha256:2539d4344dd18e1df02be842ffc435f8e1f699cfc55516e2cf2cb16b7a9aea0b
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
    [root@localhost ~]# docker pull busybox
    Using default tag: latest
    latest: Pulling from library/busybox
    bdbbaa22dec6: Pull complete
    Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
    Status: Downloaded newer image for busybox:latest
    docker.io/library/busybox:latest
    
    # 查看当前主机镜像列表
    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              6678c7c2e56c        13 hours ago        127MB
    busybox             latest              6d5fcfe5ff17        2 months ago        1.22MB
    

    4.2.3. 导出镜像

    [root@localhost ~]# docker image save busybox > docker-busybox.tar.gz
    [root@localhost ~]# ls docker-busybox.tar.gz
    docker-busybox.tar.gz
    
    [root@localhost ~]# docker image save -o /mnt/busybox_nginx.tar.gz busybox:latest nginx:latest
    [root@localhost ~]# ls /mnt/busybox_nginx.tar.gz
    /mnt/busybox_nginx.tar.gz
    [root@localhost ~]#
    
    -o      # 指定到处镜像的位置;
    可以同时到处多个镜像;为一个文件;
    指定.tar.gz 可以导出并压缩。
    

    4.2.4. 删除镜像

    [root@localhost ~]# docker rmi busybox
    Untagged: busybox:latest
    Untagged: busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
    Deleted: sha256:6d5fcfe5ff170471fcc3c8b47631d6d71202a1fd44cf3c147e50c8de21cf0648
    Deleted: sha256:195be5f8be1df6709dafbba7ce48f2eee785ab7775b88e0c115d8205407265c5
    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              6678c7c2e56c        13 hours ago        127MB
    

    4.2.5. 导入镜像

    [root@localhost ~]# docker image load -i docker-busybox.tar.gz
    195be5f8be1d: Loading layer [==================================================>]  1.437MB/1.437MB
    Loaded image: busybox:latest
    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              6678c7c2e56c        13 hours ago        127MB
    busybox             latest              6d5fcfe5ff17        2 months ago        1.22MB
    

    4.2.6. 查看镜像的详细信息

    [root@localhost ~]# docker image inspect nginx
    

    4.3. 容器操作

    4.3.1. 启动容器

    格式:Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

    options常用命令选项

    -t      # 打开一个终端,像使用交换机一样使用容器;
    -i      # 交互式访问
    --name  # 容器名称
    --network   # 指定网络
    --rm    # 容器一停,自动删除
    -d      # 剥离与当前终端的关系;否则会一直占据终端
    -p      # 端口映射,将容器内服务的端口映射在宿主机的指定端口
        -p <container port>
        -p <hostport>:<container port>
        -p <hostip>:<hostport>:<container port>
    
    # 运行一个容器
    [root@localhost ~]# docker run --name web1 -d -p 8888:80 nginx:latest
    d5548ea39d8e4098d0c8e055743083eb29b4105a9753b154c7e7eb246606480a
    # 查看docker映射端口
    [root@localhost ~]# docker port web1
    80/tcp -> 0.0.0.0:8888
    
    # 在运行容器时,交互式进入容器
    [root@localhost ~]# docker run --name b1 -it busybox /bin/sh
    / # exit
    
    # 显示运行的容器
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS          NAMES
    d5548ea39d8e        nginx:latest        "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:8888->80/tcp   web1
    # 查询所有容器
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS                  NAMES
    059c97d6d13d        busybox             "/bin/sh"                About a minute ago   Exited (0) 59 seconds ago                   b1
    d5548ea39d8e        nginx:latest        "nginx -g 'daemon of…"   2 minutes ago        Up 2 minutes                0.0.0.0:8888->80/tcp   web1
    

    4.3.2. 停止运行的容器

    # docker stop 关闭运行的容器
    # docker kill 杀死运行的容器
    
    [root@localhost ~]# docker kill web1
    web1
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS  NAMES
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
    059c97d6d13d        busybox             "/bin/sh"                2 minutes ago       Exited (0) 2 minutes ago                b1
    d5548ea39d8e        nginx:latest        "nginx -g 'daemon of…"   3 minutes ago       Exited (137) 7 seconds ago                web1
    

    4.3.3. 激活关闭的容器

    # docker start
    [root@localhost ~]# docker start web1
    web1
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS          NAMES
    d5548ea39d8e        nginx:latest        "nginx -g 'daemon of…"   4 minutes ago       Up 1 second         0.0.0.0:8888->80/tcp   web1
    

    4.3.4. 查看容器的详细信息

    [root@localhost ~]# docker inspect web1
    # 查询容器的IP
    [root@localhost ~]# docker inspect web1|grep "IPAddress"
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    # 通过容器IP,在宿主机上访问服务
    [root@localhost ~]# curl 172.17.0.2
    # 映射到宿主机的端口是8888
    [root@localhost ~]# curl 127.0.0.1:8888
    

    4.3.5. 删除容器

    # 先关闭容器,再删除容器
    [root@localhost ~]# docker stop web1
    web1
    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS  NAMES
    [root@localhost ~]# docker rm web1
    web1
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS         NAMES
    059c97d6d13d        busybox             "/bin/sh"           6 minutes ago       Exited (0) 6 minutes ago         b1
    
    # 删除所有容器,-f强制删除
    [root@localhost ~]# docker rm -f `docker ps -a -q`
    059c97d6d13d
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS  NAMES
    

    4.3.6. 对运行的容器执行指定命令exec

    [root@localhost ~]# docker run --name web1 -d nginx
    454a6dedba7ddbd0d1a2b65af63b97b73e4c276d23a04d7b2695f14d61e3b861
    # 交互式进入容器
    [root@localhost ~]# docker exec -it web1 /bin/bash
    root@454a6dedba7d:/# ls /
    bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var
    

    4.3.7. 查询容器内部日志

    [root@localhost ~]# curl 172.17.0.2
    [root@localhost ~]# docker logs web1
    172.17.0.1 - - [05/Mar/2020:07:06:00 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
    
  • 相关阅读:
    博客园美化-SimpleMemor
    Java多线程-synchronized与ReentrantLock
    springboot中删除@SessionAttributes注解的属性
    SSM整合笔记
    Spring中xml和注解方式使用AOP
    Mysql 数据库基本操作
    Mysql 二进制包安装
    named piped tcp proxy 下载
    docker容器中日志文件过大处理方法
    自动做bond的脚本
  • 原文地址:https://www.cnblogs.com/HsLM/p/12420480.html
Copyright © 2011-2022 走看看