Tuesday, August 29, 2023

Install Docker using Ansible Playbook

  DevOps. Ansible and Docker | JazzTeam Software Development Company

Ansible 

Create a docker.yml with below content

- hosts: localhost
  become: yes
  become_method: sudo
  become_flags: -s
  become_user: root

  tasks:
   - name: Install docker
     shell: |
      sudo apt-get update
      sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg \
        lsb-release
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
      echo \
      "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
      sudo apt-get update
      yes | sudo apt-get install docker-ce docker-ce-cli containerd.io
      sudo docker run hello-world

ansible@ansible-VirtualBox:~/Desktop$ ansible-playbook docker.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [localhost] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Install docker] *************************************************************************************************************************************************************************************
File '/usr/share/keyrings/docker-archive-keyring.gpg' exists. Overwrite? (y/N) y
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
fatal: [localhost]: FAILED! => changed=true
  cmd: |-
    sudo apt-get update
    sudo apt-get install  apt-transport-https  ca-certificates  curl  gnupg  lsb-release
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    yes | sudo apt-get install docker-ce docker-ce-cli containerd.io
    sudo docker run hello-world
  delta: '0:00:58.673958'
  end: '2021-04-23 21:45:50.452396'
  msg: non-zero return code
  rc: 125
  start: '2021-04-23 21:44:51.778438'
  stderr: |-
    E: Sub-process /usr/bin/dpkg returned an error code (1)
    docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
    See 'docker run --help'.
  stderr_lines: <omitted>
  stdout: |-
    Hit:1 https://download.docker.com/linux/ubuntu groovy InRelease
    Get:2 http://security.ubuntu.com/ubuntu groovy-security InRelease [110 kB]
    Hit:3 http://in.archive.ubuntu.com/ubuntu groovy InRelease
    Hit:4 http://in.archive.ubuntu.com/ubuntu groovy-updates InRelease
    Hit:5 http://in.archive.ubuntu.com/ubuntu groovy-backports InRelease
    Fetched 110 kB in 17s (6,602 B/s)
    Reading package lists...
    Reading package lists...
    Building dependency tree...
    Reading state information...
    lsb-release is already the newest version (11.1.0ubuntu2).
    lsb-release set to manually installed.
    curl is already the newest version (7.68.0-1ubuntu4.3).
    The following packages were automatically installed and are no longer required:
      docker-ce-rootless-extras docker-scan-plugin libslirp0 pigz slirp4netns
    Use 'sudo apt autoremove' to remove them.
    The following additional packages will be installed:
      dirmngr gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client gpg-wks-server
      gpgconf gpgsm gpgv
    Suggested packages:
      tor parcimonie xloadimage scdaemon
    The following NEW packages will be installed:
      apt-transport-https
    The following packages will be upgraded:
      ca-certificates dirmngr gnupg gnupg-l10n gnupg-utils gpg gpg-agent
      gpg-wks-client gpg-wks-server gpgconf gpgsm gpgv
    12 upgraded, 1 newly installed, 0 to remove and 239 not upgraded.
    Need to get 2,586 kB/2,733 kB of archives.
    After this operation, 214 kB of additional disk space will be used.
    Do you want to continue? [Y/n] Abort.
    Hit:1 https://download.docker.com/linux/ubuntu groovy InRelease
    Hit:2 http://in.archive.ubuntu.com/ubuntu groovy InRelease
    Hit:3 http://in.archive.ubuntu.com/ubuntu groovy-updates InRelease
    Get:4 http://security.ubuntu.com/ubuntu groovy-security InRelease [110 kB]
    Hit:5 http://in.archive.ubuntu.com/ubuntu groovy-backports InRelease
    Fetched 110 kB in 17s (6,496 B/s)
    Reading package lists...
    Reading package lists...
    Building dependency tree...
    Reading state information...
    Suggested packages:
      aufs-tools cgroupfs-mount | cgroup-lite
    The following NEW packages will be installed:
      containerd.io docker-ce docker-ce-cli
    0 upgraded, 3 newly installed, 0 to remove and 251 not upgraded.
    Need to get 0 B/94.5 MB of archives.
    After this operation, 425 MB of additional disk space will be used.
    Selecting previously unselected package containerd.io.
    (Reading database ... (Reading database ... 5%(Reading database ... 10%(Reading database ... 15%(Reading database ... 20%(Reading database ... 25%(Reading database ... 30%(Reading database ... 35%(Reading database ... 40%(Reading database ... 45%(Reading database ... 50%(Reading database ... 55%(Reading database ... 60%(Reading database ... 65%(Reading database ... 70%(Reading database ... 75%(Reading database ... 80%(Reading database ... 85%(Reading database ... 90%(Reading database ... 95%(Reading database ... 100%(Reading database ... 191898 files and directories currently installed.)
    Preparing to unpack .../containerd.io_1.4.4-1_amd64.deb ...
    Unpacking containerd.io (1.4.4-1) ...
    Selecting previously unselected package docker-ce-cli.
    Preparing to unpack .../docker-ce-cli_5%3a20.10.6~3-0~ubuntu-groovy_amd64.deb ...
    Unpacking docker-ce-cli (5:20.10.6~3-0~ubuntu-groovy) ...
    Selecting previously unselected package docker-ce.
    Preparing to unpack .../docker-ce_5%3a20.10.6~3-0~ubuntu-groovy_amd64.deb ...
    Unpacking docker-ce (5:20.10.6~3-0~ubuntu-groovy) ...
    Setting up containerd.io (1.4.4-1) ...
    Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
    Setting up docker-ce-cli (5:20.10.6~3-0~ubuntu-groovy) ...
    Setting up docker-ce (5:20.10.6~3-0~ubuntu-groovy) ...
    Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
    Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
    Job for docker.service failed because the control process exited with error code.
    See "systemctl status docker.service" and "journalctl -xe" for details.
    invoke-rc.d: initscript docker, action "start" failed.
    ● docker.service - Docker Application Container Engine
         Loaded: loaded (]8;;file://ansible-VirtualBox/lib/systemd/system/docker.service/lib/systemd/system/docker.service]8;;; enabled; vendor preset: enabled)
         Active: activating (auto-restart) (Result: exit-code) since Fri 2021-04-23 21:45:41 IST; 4ms ago
    TriggeredBy: [0;1;32m●[0m docker.socket
           Docs: ]8;;https://docs.docker.comhttps://docs.docker.com]8;;
        Process: 13286 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock [0;1;31m(code=exited, status=1/FAILURE)[0m
       Main PID: 13286 (code=exited, status=1/FAILURE)
 
    Apr 23 21:45:41 ansible-VirtualBox systemd[1]: [0;1;39m[0;1;31m[0;1;39mdocker.service: Main process exited, code=exited, status=1/FAILURE[0m
    Apr 23 21:45:41 ansible-VirtualBox systemd[1]: [0;1;38;5;185m[0;1;39m[0;1;38;5;185mdocker.service: Failed with result 'exit-code'.[0m
    Apr 23 21:45:41 ansible-VirtualBox systemd[1]: [0;1;31m[0;1;39m[0;1;31mFailed to start Docker Application Container Engine.[0m
    dpkg: error processing package docker-ce (--configure):
     installed docker-ce package post-installation script subprocess returned error exit status 1

    Processing triggers for man-db (2.9.3-2) ...
    Processing triggers for systemd (246.6-1ubuntu1) ...
    Errors were encountered while processing:
     docker-ce
  stdout_lines: <omitted>

PLAY RECAP ************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

Just re-ran the same playbook and docker installation completed successfully

ansible@ansible-VirtualBox:~/Desktop$ ansible-playbook docker.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [localhost] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Install docker] *************************************************************************************************************************************************************************************
File '/usr/share/keyrings/docker-archive-keyring.gpg' exists. Overwrite? (y/N) y
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
changed: [localhost] => changed=true
  cmd: |-
    sudo apt-get update
    sudo apt-get install  apt-transport-https  ca-certificates  curl  gnupg  lsb-release
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    yes | sudo apt-get install docker-ce docker-ce-cli containerd.io
    sudo docker run hello-world
  delta: '0:00:19.867569'
  end: '2021-04-23 21:46:42.643464'
  rc: 0
  start: '2021-04-23 21:46:22.775895'
  stderr: ''
  stderr_lines: <omitted>
  stdout: |-
    Get:1 http://security.ubuntu.com/ubuntu groovy-security InRelease [110 kB]
    Hit:2 http://in.archive.ubuntu.com/ubuntu groovy InRelease
    Hit:3 http://in.archive.ubuntu.com/ubuntu groovy-updates InRelease
    Hit:4 http://in.archive.ubuntu.com/ubuntu groovy-backports InRelease
    Hit:5 https://download.docker.com/linux/ubuntu groovy InRelease
    Fetched 110 kB in 6s (18.9 kB/s)
    Reading package lists...
    Reading package lists...
    Building dependency tree...
    Reading state information...
    lsb-release is already the newest version (11.1.0ubuntu2).
    lsb-release set to manually installed.
    curl is already the newest version (7.68.0-1ubuntu4.3).
    The following additional packages will be installed:
      dirmngr gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client gpg-wks-server
      gpgconf gpgsm gpgv
    Suggested packages:
      tor parcimonie xloadimage scdaemon
    The following NEW packages will be installed:
      apt-transport-https
    The following packages will be upgraded:
      ca-certificates dirmngr gnupg gnupg-l10n gnupg-utils gpg gpg-agent
      gpg-wks-client gpg-wks-server gpgconf gpgsm gpgv
    12 upgraded, 1 newly installed, 0 to remove and 239 not upgraded.
    1 not fully installed or removed.
    Need to get 2,586 kB/2,733 kB of archives.
    After this operation, 214 kB of additional disk space will be used.
    Do you want to continue? [Y/n] Abort.
    Hit:1 https://download.docker.com/linux/ubuntu groovy InRelease
    Hit:2 http://in.archive.ubuntu.com/ubuntu groovy InRelease
    Get:3 http://security.ubuntu.com/ubuntu groovy-security InRelease [110 kB]
    Hit:4 http://in.archive.ubuntu.com/ubuntu groovy-updates InRelease
    Hit:5 http://in.archive.ubuntu.com/ubuntu groovy-backports InRelease
    Fetched 110 kB in 1s (78.0 kB/s)
    Reading package lists...
    Reading package lists...
    Building dependency tree...
    Reading state information...
    containerd.io is already the newest version (1.4.4-1).
    docker-ce-cli is already the newest version (5:20.10.6~3-0~ubuntu-groovy).
    docker-ce is already the newest version (5:20.10.6~3-0~ubuntu-groovy).
    0 upgraded, 0 newly installed, 0 to remove and 251 not upgraded.
    1 not fully installed or removed.
    After this operation, 0 B of additional disk space will be used.
    Do you want to continue? [Y/n] Setting up docker-ce (5:20.10.6~3-0~ubuntu-groovy) ...
 
    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/
  stdout_lines: <omitted>

PLAY RECAP ************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

ansible@ansible-VirtualBox:~/Desktop$ sudo docker create busybox ping google.com
1b6968752edb96b23b14315e6a557421d48878c97e3e308a1c11a32ca1fa7c9e
ansible@ansible-VirtualBox:~/Desktop$ sudo docker start 1b6968752edb96b23b14315e6a557421d48878c97e3e308a1c11a32ca1fa7c9e
1b6968752edb96b23b14315e6a557421d48878c97e3e308a1c11a32ca1fa7c9e
ansible@ansible-VirtualBox:~/Desktop$ sudo docker ps -all
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS         PORTS     NAMES
1b6968752edb   busybox   "ping google.com"   20 seconds ago   Up 6 seconds             cool_grothendieck