【Docker】01 Docker安装与配置


文章目录

  • 一、Docker
  • 二、离线安装Docker
  • 三、联网安装Docker
    • 3.1 下载YUM软件库文件
    • 3.2 安装epel-release
    • 3.3 安装yum-utils
    • 3.4 设置镜像仓库
    • 3.5 查看docker-ce所有版本
    • 3.6 安装Docker
    • 3.7 启动Docker
    • 3.8 查看Docker信息
    • 3.9 启动第一个容器
    • 四、一些配置
      • 4.1 登录DockerHub
      • 4.2 镜像加速器

        一、Docker

        Docker,基于容器技术的轻量级虚拟化解决方案。

        Docker是容器引擎,把Linux的cgroup、namespace等容器底层技术进行封装抽象,为用户提供了创建和管理容器的便捷界面(包括命令行和API)。

        概念:镜像Image,容器Container,仓库Repository:

        • Docker镜像,是一个静态模板,不能直接修改,可以通过封装生成
        • Docker容器,基于Docker镜像运行启动的应用或系统
        • Docker仓库,存放Docker镜像的地方,常见分为公开仓库(public)和私有仓库(private)两种形式

          二、离线安装Docker

          Docker官网离线安装文档:Install daemon and client binaries on Linux

          安装步骤:

          1)官网下载Docker安装包:Index of linux/static/stable/x86_64/ 选取相应版本,这边选了docker-20.10.9.tgz

          2)解压到/usr/bin/

          [root@localhost ~]# tar -xzvf docker-20.10.9.tgz 
          docker/
          docker/containerd-shim-runc-v2
          docker/dockerd
          docker/docker-proxy
          docker/ctr
          docker/docker
          docker/runc
          docker/containerd-shim
          docker/docker-init
          docker/containerd
          [root@localhost ~]# ls
          anaconda-ks.cfg  docker  docker-20.10.9.tgz
          [root@localhost ~]# ls docker
          containerd       containerd-shim-runc-v2  docker   docker-init   runc
          containerd-shim  ctr                      dockerd  docker-proxy
          [root@localhost ~]# mv docker/* /usr/bin/
          

          3)启动服务

          [root@localhost ~]# dockerd &
          [1] 1673
          [root@localhost ~]# INFO[2024-01-05T17:11:32.398698200+08:00] Starting up                                  
          WARN[2024-01-05T17:11:32.403021500+08:00] could not change group /var/run/docker.sock to docker: group docker not found 
          INFO[2024-01-05T17:11:32.406919900+08:00] libcontainerd: started new containerd process  pid=1678
          INFO[2024-01-05T17:11:32.406979400+08:00] parsed scheme: "unix"                         module=grpc
          INFO[2024-01-05T17:11:32.406991700+08:00] scheme "unix" not registered, fallback to default scheme  module=grpc
          ...
          ...
          [root@localhost ~]# ps -ef | grep docker | grep -v grep
          root      1673  1625  0 17:11 pts/0    00:00:00 dockerd
          root      1678  1673  0 17:11 ?        00:00:00 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
          

          4)验证

          [root@localhost ~]# docker info
          Client:
           Context:    default
           Debug Mode: false
          Server:
           Containers: 0
            Running: 0
            Paused: 0
            Stopped: 0
           Images: 0
           Server Version: 20.10.9
           Storage Driver: overlay2
            Backing Filesystem: xfs
            Supports d_type: true
            Native Overlay Diff: true
            userxattr: false
           Logging Driver: json-file
           Cgroup Driver: cgroupfs
           Cgroup Version: 1
           Plugins:
            Volume: local
            Network: bridge host ipvlan macvlan null overlay
            Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
           Swarm: inactive
           Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
           Default Runtime: runc
           Init Binary: docker-init
           containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
           runc version: v1.0.2-0-g52b36a2d
           init version: de40ad0
           Security Options:
            seccomp
             Profile: default
           Kernel Version: 3.10.0-862.el7.x86_64
           Operating System: CentOS Linux 7 (Core)
           OSType: linux
           Architecture: x86_64
           CPUs: 1
           Total Memory: 509.6MiB
           Name: localhost.localdomain
           ID: VM36:UOEO:67ST:Y2EU:RVE6:TC37:SZ6W:UNND:PF22:CWT2:FWYG:XZMA
           Docker Root Dir: /var/lib/docker
           Debug Mode: false
           Registry: https://index.docker.io/v1/
           Labels:
           Experimental: false
           Insecure Registries:
            127.0.0.0/8
           Live Restore Enabled: false
           Product License: Community Engine
          

          5)注册服务

          kill掉前面手动启动的docker服务,

          在/usr/lib/systemd/system/目录下创建docker.service文件并添加内容:

          [root@localhost ~]# vi /usr/lib/systemd/system/docker.service
          [Unit]
          Description=Docker Application Container Engine
          Documentation=https://docs.docker.com
          After=network-online.target firewalld.service
          Wants=network-online.target
          [Service]
          Type=notify
          ExecStart=/usr/bin/dockerd
          ExecReload=/bin/kill -s HUP $MAINPID
          LimitNOFILE=infinity
          LimitNPROC=infinity
          LimitCORE=infinity
          TimeoutStartSec=0
          Delegate=yes
          KillMode=process
          Restart=on-failure
          StartLimitBurst=3
          StartLimitInterval=60s
          [Install]
          WantedBy=multi-user.target
          

          重新加载

          [root@localhost ~]# systemctl daemon-reload
          

          服务的相关操作:

          [root@localhost ~]# systemctl start docker
          [root@localhost ~]# systemctl stop docker
          [root@localhost ~]# systemctl status docker
          [root@localhost ~]# systemctl enable docker
          Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
          

          此时就可通过systemctl来启停Docker服务了。

          三、联网安装Docker

          3.1 下载YUM软件库文件

          wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
          

          3.2 安装epel-release

          yum install epel-release -y
          

          3.3 安装yum-utils

          在配置国内镜像仓库时,会遇到如下报错,此时就需要安装yum-utils。

          [root@server ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
          -bash: yum-config-manager: command not found
          
          yum install yum-utils -y
          

          3.4 设置镜像仓库

          设置镜像仓库,这边使用国内的阿里云镜像仓库:

          [root@server ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
          Loaded plugins: fastestmirror
          adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
          grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
          repo saved to /etc/yum.repos.d/docker-ce.repo
          

          3.5 查看docker-ce所有版本

          [root@server ~]# yum list docker-ce --show-duplicates
          Loaded plugins: fastestmirror
          Loading mirror speeds from cached hostfile
          * base: mirrors.aliyun.com
          * epel: mirrors.nju.edu.cn
          * extras: mirrors.aliyun.com
          * updates: mirrors.aliyun.com
          docker-ce-stable            | 3.5 kB  00:00:00     
          (1/2): docker-ce-stable/7/x86_64/updateinfo                                |   55 B  00:00:00     
          (2/2): docker-ce-stable/7/x86_64/primary_db                                |  62 kB  00:00:00     
          Available Packages
          docker-ce.x86_64                     17.03.0.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.2.ce-1.el7.centos                      docker-ce-stable
          docker-ce.x86_64                     17.03.3.ce-1.el7                             docker-ce-stable
          docker-ce.x86_64                     17.06.0.ce-1.el7.centos                      docker-ce-stable
          docker-ce.x86_64                     17.06.1.ce-1.el7.centos                      docker-ce-stable
          docker-ce.x86_64                     17.06.2.ce-1.el7.centos                      docker-ce-stable
          

          3.6 安装Docker

          [root@server ~]# yum install docker-ce
          

          3.7 启动Docker

          [root@server ~]# systemctl enable docker
          Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
          [root@server ~]# systemctl start docker
          [root@server ~]#
          

          3.8 查看Docker信息

          [root@server ~]# 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)
            scan: Docker Scan (Docker Inc., v0.8.0)
          Server:
          Containers: 0
            Running: 0
            Paused: 0
            Stopped: 0
          Images: 0
          Server Version: 20.10.7
          Storage Driver: overlay2
            Backing Filesystem: xfs
            Supports d_type: true
            Native Overlay Diff: true
            userxattr: false
          Logging Driver: json-file
          Cgroup Driver: cgroupfs
          Cgroup Version: 1
          ...
          
          # 安装完成后docker自动创建了docker组,为安全起见,将当前登录用户追加入到该组中
          [root@server~ ]# id root
          uid=0(root) gid=0(root) groups=0(root)
          [root@server~ ]# usermod -aG docker $USER
          [root@server~ ]# id root
          uid=0(root) gid=0(root) groups=0(root),993(docker)
          

          3.9 启动第一个容器

          [root@server ~]# docker run hello-world
          Unable to find image 'hello-world:latest' locally
          latest: Pulling from library/hello-world
          c1ec31eb5944: Pull complete 
          Digest: sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493
          Status: Downloaded newer image for hello-world:latest
          Hello from Docker!
          This message shows that your installation appears to be working correctly.
          To generate this message, Docker took the following steps:
           1. The Docker client contacted the Docker daemon.
           2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
              (amd64)
           3. The Docker daemon created a new container from that image which runs the
              executable that produces the output you are currently reading.
           4. The Docker daemon streamed that output to the Docker client, which sent it
              to your terminal.
          To try something more ambitious, you can run an Ubuntu container with:
           $ docker run -it ubuntu bash
          Share images, automate workflows, and more with a free Docker ID:
           https://hub.docker.com/
          For more examples and ideas, visit:
           https://docs.docker.com/get-started/
          

          四、一些配置

          4.1 登录DockerHub

          访问https://hub.docker.com/,先注册个账号,

          之后在本地端登录:

          [root@server ~]# docker login docker.io
          Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
          Username: asdfv1929
          Password:
          WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
          Configure a credential helper to remove this warning. See
          https://docs.docker.com/engine/reference/commandline/login/#credentials-store
          Login Succeeded
          [root@server ~]#
          

          登录完成后,此时便可拉取Docker Hub上自己仓库里的镜像。

          4.2 镜像加速器

          [root@server ~]# vi /etc/docker/daemon.json
          { "registry-mirrors": [
          	        "http://hub-mirror.c.163.com",
          	        "https://docker.mirrors.ustc.edu.cn"
          	    ]
          }
          [root@server ~]# systemctl daemon-reload
          [root@server ~]# systemctl restart docker
          

          ustc的链接已失效