Cara Install Docker di Ubuntu 20.04

Cara Install Docker di Ubuntu 20.04 – Docker adalah seperangkat produk platform-as-a-service yang digunakan untuk meluncurkan dan mengelola container. Developer menggunakan container Docker untuk mengembangkan dan menerapkan aplikasi karena mereka menyediakan lingkungan virtual yang terisolasi dan ringan.

Docker adalah platform sumber terbuka yang menggunakan virtualisasi tingkat OS untuk mengirimkan perangkat lunak dalam paket yang disebut container. Container adalah unit perangkat lunak yang mengemas kode dan semua dependensinya sehingga aplikasi berjalan dengan cepat dan andal dari satu lingkungan komputasi ke lingkungan komputasi lainnya.

Teknologi container Docker diluncurkan pada tahun 2013 yang memulai revolusi dalam pengembangan aplikasi – dengan mendemokrasikan container perangkat lunak.

Apa saja yang dibutuhkan untuk Install Docker di Ubuntu 20.04?

  • VPS/Cloud computing/Virtual machine
  • Ubuntu 20.04
  • SSH & User dengan sudo privilege

Test instalasi Docker ini diuji menggunakan AWS (Amazon Web Services) EC2, juga akan berfungsi dengan baik di layanan cloud lain dan di VPS atau server khusus yang menjalankan Ubuntu.

Install Docker di Ubuntu 20.04

Install Docker on Ubuntu
Docker Ubuntu

Ada dua opsi untuk menginstal Docker di Ubuntu:

  1. Install Docker menggunakan Docker Official Repository
  2. Install Docker menggunakan Default Repository

Install Docker menggunakan Docker Official Repository

Step 1: Update Package index

Sebelum memulai instalasi, seperti biasa kita membutuhkan akses ke server via SSH. Setelah login ke server via SSH, pastikan dan biasakan untuk melakukan update package index. Agar semua package yang terinstall diperbarui ke versi yang baru.

sudo apt update & sudo apt upgrade

Step 2: Download Dependencies

Sebagian besar software dependencies mungkin sudah terinstall otomatis, untuk memastikan install software dependencies dibawah ini.

Memberikan izin kepada package manager untuk mentransfer file dan data melalui https:

sudo apt install apt-transport-https

Memberikan izin system untuk memeriksa sertifikat keamanan:

sudo apt install ca-certificates

Install curl, tool untuk mentransfer data:

sudo apt install curl

Install software-properties-common:

sudo apt install software-properties-common

Step 3: Menambahkan Docker GPG Key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Step 4: Menambahkan Docker Repository

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

Setelah sukses menambahkan Docker Repository, selanjutnya melakukan perform update:

sudo apt update
sudo apt-cache policy docker-ce
docker-ce:
  Installed: (none)
  Candidate: 5:20.10.10~3-0~ubuntu-focal
  Version table:
     5:20.10.10~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.9~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker policy
docker-ce

Step 5: Install Docker terbaru

Setelah melakukan perform update atas repository yang baru maka lanjut untuk menginstall Docker:

sudo apt install docker-ce

Step 6: Verifikasi Instalasi Docker

Check versi Docker yang berhasil terinstall:

docker --version
Docker version 20.10.10, build b485636

Atau:

sudo docker version
Client:
 Version:           20.10.7                                    API version:       1.41
 Go version:        go1.13.8
 Git commit:        20.10.7-0ubuntu5~20.04.2
 Built:             Mon Nov  1 00:34:17 2021                   OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true                                      
Server:
 Engine:                                                        Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.8
  Git commit:       20.10.7-0ubuntu5~20.04.2                    Built:            Fri Oct 22 00:45:53 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.5-0ubuntu3~20.04.1                      GitCommit:
 runc:
  Version:          1.0.1-0ubuntu2~20.04.1
  GitCommit:
 docker-init:
  Version:          0.19.0
  GitCommit:
docker version
Docker version

Check apakah Docker sudah otomatis berjalan di system Ubuntu:

sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enab>
     Active: active (running) since Mon 2021-11-15 15:55:29 U>
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 15159 (dockerd)
      Tasks: 7
     Memory: 39.3M
     CGroup: /system.slice/docker.service
             └─15159 /usr/bin/dockerd -H fd:// --containerd=/>

Nov 15 15:55:29 ip-172-31-31-63 dockerd[15159]: time="2021-11>
Nov 15 15:55:29 ip-172-31-31-63 systemd[1]: Started Docker Ap>
Nov 15 15:55:29 ip-172-31-31-63 dockerd[15159]: time="2021-11>
lines 1-21/21 (END)
docker status
Docker status

Jika Docker tidak berjalan atau status Inactive (dead) maka kita perlu menjalankan dengan perintah:

sudo systemctl start docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; disa>
     Active: inactive (dead)
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
docker inactive
Docker inactive

Jika menginginkan Docker berjalan otomatis saat system dihidupkan atau saat reboot:

sudo systemctl enable docker

Install Docker menggunakan Default Repository

Step 1: Update Package index

Sebelum memulai instalasi, seperti biasa kita membutuhkan akses ke server via SSH. Setelah login ke server via SSH, pastikan dan biasakan untuk melakukan update package index. Agar semua package yang terinstall diperbarui ke versi yang baru.

sudo apt update & sudo apt upgrade

Step 2: Install Docker

Setelah melakukan perform update atas repository yang baru maka lanjut untuk menginstall Docker:

sudo apt install docker docker.io -y

Step 3: Verifikasi Instalasi Docker

docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2

Check apakah Docker sudah otomatis berjalan di system Ubuntu:

sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enab>
     Active: active (running) since Mon 2021-11-15 15:55:29 U>
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 15159 (dockerd)
      Tasks: 7
     Memory: 39.3M
     CGroup: /system.slice/docker.service
             └─15159 /usr/bin/dockerd -H fd:// --containerd=/>

Jika Docker tidak berjalan atau status Inactive (dead) maka kita perlu menjalankan dengan perintah:

sudo systemctl start docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; disa>
     Active: inactive (dead)
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
docker inactive
Docker inactive

Jika menginginkan Docker berjalan otomatis saat system dihidupkan atau saat reboot:

sudo systemctl enable docker

Menggunakan Docker di Ubuntu 20.04

Syntax dasar untuk perintah docker adalah:

sudo docker [option] [command] [argument]

Menjalankan Perintah Docker tanpa SUDO

Secara default, kita hanya dapat menggunakan perintah docker dengan hak akses root (menggunakan sudo diawalan perintah). Misalnya, jika kita mencoba menjalankan hello-world container, maka akan ditolak.

docker run hello-world 
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
docker permission denied
Permission denied

Jika kita perlu menjalankan perintah docker tanpa sudo, kita perlu menambahkan nama pengguna ke group Docker.

sudo usermod -aG docker <username>

Setelah menambah user dalam group Docker, keluar SSH (exit) dan masuk/login kembali ke SSH.

PicsArt 11 15 11.16.37
System restart required

Jika system memerlukan restart, maka restart dahulu system Ubuntu dengan perintah: sudo reboot, dan tunggu beberapa saat untuk login kembali ke SSH.

Menjalankan Perintah Docker

Untuk mengetahui informasi tentang Docker jalankan perintah ini:

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.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.10
 Storage Driver: overlay2
  Backing Filesystem: extfs
  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-g52b36a2
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.11.0-1021-aws
 Operating System: Ubuntu 20.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 969MiB
 Name: ip-172-31-31-63
 ID: LP3H:FHWS:3U7H:GKRV:RAAU:KVJO:EQ3M:KAVJ:4WW4:Q62Y:ZG3N:HPG6
 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

Sekarang kita bisa menjalankan perintah docker tanpa sudo.

Download Docker Images

Docker images adalah file yang berisi source code, libraries, dependencies, tools, file lainnya yang dibutuhkan container. Kita dapat membuat Docker images dengan Dockerfiles atau menggunakan yang sudah ada yang tersedia di Docker Hub.

Untuk download Docker Images yang baru bisa menggunakan perintah ini:

docker pull [image_name]

Jika kita tidak tahu nama pasti dari images tersebut, cari di repositori Docker dengan:

docker search <search_query>

Contoh saya akan mencari docker images nginx:

docker search nginx
docker search nginx
Docker search images

Setelah kita berhasil menjalankan perintah Docker tanpa sudo, selanjutnya kita menjalankan hello-world container dengan perintah ini:

docker run hello-world

Jika output yang kita dapatkan mirip dengan di bawah ini maka kita dapat mengakses dan download images dari Docker Hub.

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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/

Jalankan perintah di bawah ini untuk melihat images yang didownload:

docker images
docker show images

Setelah melihat images yang di download kita bisa langsung menjalankannya, contoh hello-world:

docker run hello-world 
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/
docker hello world success
Docker hello-world

Bekerja dengan Docker container

docker container
Docker containers

Untuk menjalankan container berdasarkan image Docker yang ada, gunakan perintah:

docker run [image_name]

Untuk menjalankan container dalam mode interaktif dan mengubah ke command prompt container, jalankan:

docker run -it [image_name]

Untuk melihat semua container yang aktif, gunakan perintah berikut:

docker ps

Untuk melihat semua container yang aktif dan tidak aktif, gunakan perintah berikut:

docker ps -a

Untuk melihat container terbaru jalan perintah berikut:

docker ps -l

Untuk memulai sebuah container docker, gunakan perintah docker start diikuti dengan Container ID atau Container Name.

docker start container-id/name

Demikian juga untuk menghentikan container yang sedang berjalan kita dapat menggunakan perintah docker stop diikuti dengan Container ID atau Container Name.

docker stop container-id/name

Untuk detail lebih lanjut tentang perintah docker gunakan perintah docker run help.

docker run help

Cara Uninstall Docker

Jika kita ingin menghapus Docker dari system Ubuntu jalankan perintah ini:

sudo apt purge docker docker.io
sudo apt autoremove

Kesimpulan

Dengan mengikuti tutorial/cara/metode diatas, kita dapat mempelajari Cara Install Docker di Ubuntu 20.04, baik instalasi menggunakan Default Repository maupun Official Repository dan juga menjalankan docker container dan juga menjalankan perintah tanpa sudo.

Semoga bermanfaat.

Tinggalkan komentar