Berikut langkah untuk install docker menggunakan ansible sebagai tools otomasi dengan debian 11.07.
- Server Debian OS for remote host
- Server Debian OS for docker1 node
- install ansible di debian 11.07 (remote host) dan jalankan perintah di bawah ini
sudo apt update
sudo apt install ansible - generate private dan public key kemudian copikan ssh key ke dalam host server
sudo ssh-keygen
ssh-copy-id [email protected] - Buat file host inventory dengan menjalankan perintah di bawah ini :
sudo nano /etc/ansible/hosts - Tambahkan baris konfigurasi pada file hosts di bawah ini :
[servers]
docker1 ansible_host=172.17.100.18simpan file tersebut
- Jalankan perintah untuk melakukan inventory list
ansible-inventory –list -y - Lakukan ansible test connect to hosts, dengan perintah di bawah ini
ansible all -m ping -u root - Jalankan docker di target dengan ansible playbook yml, buat file docker-ansible.yml yang berisi di bawah ini :
- name: install ke docker1
hosts: docker1
remote_user: docker1
become: yes
tasks:
- name: Install Docker Engine
apt:
name: docker.io
state: present
- name: Install Docker CLI
apt:
name: docker-compose
state: present
- name: Start Docker service
service:
name: docker
state: started
enabled: true
- name: Pull nginx Docker image
docker_image:
name: nginx
source: pull
- name: Create volume
docker_volume:
name: nginx-data
driver: local
- name: Run nginx Docker container
docker_container:
name: nginx
image: nginx
state: started
ports:
- "80:80"
volumes:
-“nginx-data:/usr/share/nginx/html:ro”
10. Jalankan playbook dengan perintah
ansible-playbook docker-ansible.yml11. jika error seperti di bawah ini tambahkan konfigurasi sudoers pada direktori /etc/sudoers.d/docker1
remote@remote:~$ ansible-playbook docker-ansible.yml
PLAY [install ke docker1] ***********************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
fatal: [docker1]: FAILED! => {“msg”: “Missing sudo password”}
PLAY RECAP **************************************************************************************************************
docker1 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
sudo nano /etc/sudoers.d/docker1
docker1 ALL=(ALL) NOPASSWD: ALL
12. Kemudian jalankan kembali perintah ansible playbook
ansible-playbook docker-ansible.yml
remote@remote:~$ ansible-playbook docker-ansible.yml
PLAY [install ke docker1] ***********************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [docker1]
TASK [Install Docker Engine] ********************************************************************************************
changed: [docker1]
TASK [Install Docker CLI] ***********************************************************************************************
changed: [docker1]
TASK [Start Docker service] *********************************************************************************************
ok: [docker1]
TASK [Pull nginx Docker image] ******************************************************************************************
changed: [docker1]
TASK [Create volume] ****************************************************************************************************
changed: [docker1]
TASK [Run nginx Docker container] ***************************************************************************************
[DEPRECATION WARNING]: The container_default_behavior option will change its default value from “compatibility” to
“no_defaults” in community.general 3.0.0. To remove this warning, please specify an explicit value for it now. This
feature will be removed from community.general in version 3.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
changed: [docker1]
PLAY RECAP **************************************************************************************************************
docker1 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
13. kita lakukan check docker yang sudah terinstalasi di host/target docker1
systemctl status docker.service
docker1@docker1:/$ systemctl status docker.service
● docker.service – Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-09-19 12:30:20 WIB; 3min 4s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 2670 (dockerd)
Tasks: 16
Memory: 276.3M
CPU: 12.423s
CGroup: /system.slice/docker.service
├─2670 /usr/sbin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock
└─5858 /usr/sbin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 80 -container-ip 172.18.0.2 -conta
14. lakukan cek container apakah sudah up atau belum di host docker1
sudo docker container ls -a
docker1@docker1:/$ sudo docker container ls -la
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edc5fcb52b99 nginx “/docker-entrypoint.…” 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp nginx
15. lakukan tes halaman website dengan alamat IP docker1 dari komputer remote
curl http://172.17.100.18
remote@remote:~$ curl http://172.17.100.18
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href=”http://nginx.org/”>nginx.org</a>.<br/>
Commercial support is available at
<a href=”http://nginx.com/”>nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
remote@remote:~$
