Docker 安装使用安装最新版本的 Docker 客户端:Docker 引擎 - 社区

Docker installation using installs latest version of Docker Client: Docker Engine - Community

我正在使用以下 ansible 剧本在目标机器上安装 docker(特定版本)。当我在不使用 ansible(使用 shell 脚本)的情况下安装 docker 时,它可以完美运行并在使用

检查时提供以下输出
sudo docker version

Client: Docker Engine - Community
 Version:           19.03.15
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        99e3ed8919
 Built:             Sat Jan 30 03:16:51 2021
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.15
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       99e3ed8919
  Built:            Sat Jan 30 03:15:20 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

当我使用以下 ansible 剧本安装时:

- hosts: all
  become: true
  tasks:
    - name: Update apt Cache
      apt:
        update_cache: yes
        force_apt_get: yes
    - name: Wait for APT Lock
      shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done;
    - name: install dependencies
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          #- gnupg-agent
          - software-properties-common
        state: present
    - name: add GPG
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present
    - name: aggiungi repository docker
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
        state: present
    - name: installing docker-ce=5:19.03.15~3-0~ubuntu-bionic docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic containerd.io
      apt:
        name: "{{item}}"
        state: present
      loop:
        - docker-ce=5:19.03.15~3-0~ubuntu-bionic
        - docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic
        - containerd.io
    - name: assicurati che docker sia attivo
      service:
        name: docker
        state: started
        enabled: yes
    - name: Install docker-compose
      get_url:
        url: https://github.com/docker/compose/releases/download/1.25.4/docker-compose-Linux-x86_64
        dest: /usr/local/bin/docker-compose
        mode: "u+x,g+x"
    - name: Install docker-machine
      get_url:
        url: https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-Linux-x86_64
        dest: /usr/local/bin/docker-machine
        mode: "u+x,g+x"
    # - name: Copy docker machine from /tmp/docker-machine /usr/local/bin/docker-machine
    #   copy:
    #     src: /tmp/docker-machine
    #     dest: /usr/local/bin/docker-machine
  handlers:
    - name: restart docker
      service:
        name: docker
        state: restarted

当我 运行

使用上面的 ansible 剧本后
sudo docker version

Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.40
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:08 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.15
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       99e3ed8919
  Built:            Sat Jan 30 03:15:20 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

使用 ansible 剧本 latest/newer 版本的客户端时:Docker 引擎 - 正在安装社区。这导致我出现以下错误

    failed: [projects-biz4] (item=docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic) => 
{"ansible_loop_var": "item", "cache_update_time": 1628165553, "cache_updated": false, 
"changed": false, "item": "docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic", "msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force-confdef\" -o \"Dpkg::Options::=--force-
confold\"      install 'docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic'' failed: E: Packages were downgraded and -y was used without --allow-downgrades.\n", "rc": 100, "stderr": "E: 
Packages were downgraded and -y was used without --allow-downgrades.\n", "stderr_lines": ["E: Packages were downgraded and -y was used without --allow-downgrades."], "stdout": 
"Reading package lists...\nBuilding dependency tree...\nReading state information...\nThe following package was automatically installed and is no longer required:\n  docker-scan-
plugin\nUse 'sudo apt autoremove' to remove it.\nThe following packages will be DOWNGRADED:\n  docker-ce-cli\n0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 
14 not upgraded.\n", "stdout_lines": ["Reading package lists...", "Building dependency 
tree...", "Reading state information...", "The following package was automatically installed 
and is no longer required:", "  docker-scan-plugin", "Use 'sudo apt autoremove' to remove it.", "The following packages will be DOWNGRADED:", "  docker-ce-cli", "0 upgraded, 0 newly 
installed, 1 downgraded, 0 to remove and 14 not upgraded."]}

谁能帮帮我!! 提前致谢。

这里的问题是您在循环安装包。这意味着 apt 正在考虑每个包的依赖关系 独立 .

如果您在 shell 提示符下 运行 apt install docker-ce=5:19.03.15~3-0~ubuntu-bionic,您会看到它引入 docker-ce-cli 版本 5:20.10.8~3-0~ubuntu-bionic。这正是您的剧本中发生的事情。通常,您永远不应该在循环中调用 apt(或 yumdnf)。

要安装所需的版本,您需要在一个事务中安装所有包:

    - name: installing docker-ce=5:19.03.15~3-0~ubuntu-bionic docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic containerd.io
      apt:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
          - docker-ce=5:19.03.15~3-0~ubuntu-bionic
          - docker-ce-cli=5:19.03.15~3-0~ubuntu-bionic
          - containerd.io

这将允许 apt 使用您指定的版本正确解析依赖关系。