Install Docker on CentOS 7

Install docker di centos 7

  • Update Packet repository
yum update -y
  • Install paket dependency
yum -y install lvm2 device-mapper device-mapper-persistent-data device-mapper-event device-mapper-libs device-mapper-event-libs
  • Download dan tambahkan repository docker
wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
  • Install Docker CE
yum install docker-ce -y
  • Jalankan docker dan aktifkan service docker agar ketika boot dapat autostart, pastika statas docker active (running).
systemctl start docker

systemctl enable docker

systemctl status docker
  • Cek versi Docker yang terinstall
docker -v

Docker version 20.10.8, build 3967b7d
  • Akan ada interface baru dengan name docker0 dan ip address: 172.17.0.1/16 dan akan ada rule iptables untuk docker
  • Menjalankan docker-ce tanpa sudo atau root user, masukan nama user linux
usermod -aG docker nama_user_linux

Dasar-dasar docker

  • Jalankan docker hello-world
docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0
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/
  • Melihat docker image yang sudah di download di local
docker images

REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
hello-world       latest    feb5d9fea6a5   26 hours ago   13.3kB
prom/prometheus   latest    47dac5755d84   10 days ago    193MB
  • Mencari docker image di internet
docker search centos

NAME                     DESCRIPTION                             STARS  OFFICIAL   AUTOMATED
centos                   The official build of CentOS.           6774   [OK]
ansible/centos7-ansible  Ansible on Centos7                      135               [OK]
consol/centos-xfce-vnc   Centos container with "headless" VNC…   130               [OK]
jdeathe/centos-ssh       OpenSSH / Supervisor / EPEL/IUS/SCL …   120               [OK]
  • Download docker image
docker pull centos

Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest


docker images

REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
hello-world       latest    feb5d9fea6a5   27 hours ago   13.3kB
centos            latest    5d0da3dc9764   9 days ago     231MB
prom/prometheus   latest    47dac5755d84   10 days ago    193MB
  • Menjalankan docker image
docker run -it centos

[root@bfe2f78d53fb /]# uname -a
Linux bfe2f78d53fb 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

[root@bfe2f78d53fb /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
  • Melihat docker yang sedang aktif
docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED      STATUS          PORTS                                       NAMES
722fc8cee550   47dac5755d84   "/bin/prometheus --c…"   7 days ago   Up 42 minutes   0.0.0.0:9092->9090/tcp, :::9092->9090/tcp   adoring_margulis
dcec640b8555   47dac5755d84   "/bin/prometheus --c…"   7 days ago   Up 42 minutes   0.0.0.0:9091->9090/tcp, :::9091->9090/tcp   quirky_kepler

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED      STATUS          PORTS                                       NAMES
722fc8cee550   47dac5755d84   "/bin/prometheus --c…"   7 days ago   Up 42 minutes   0.0.0.0:9092->9090/tcp, :::9092->9090/tcp   adoring_margulis
dcec640b8555   47dac5755d84   "/bin/prometheus --c…"   7 days ago   Up 42 minutes   0.0.0.0:9091->9090/tcp, :::9091->9090/tcp   quirky_kepler
  • Melihat docker yang sedang aktif dan tidak aktif
docker ps -a

CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                          PORTS                                             NAMES
bfe2f78d53fb   centos            "/bin/bash"              3 minutes ago    Exited (0) About a minute ago                                                     pensive_kowalevski
bb0129db8f55   prom/prometheus   "/bin/prometheus --c…"   3 minutes ago    Exited (0) 3 minutes ago                                                          elastic_golick
2fa7f95a6674   centos            "/bin/bash"              4 minutes ago    Exited (0) 4 minutes ago                                                          agitated_mirzakhani
57823d2b5295   hello-world       "/hello"                 22 minutes ago   Exited (0) 22 minutes ago                                                         priceless_feynman
722fc8cee550   47dac5755d84      "/bin/prometheus --c…"   7 days ago       Up 43 minutes                   0.0.0.0:9092->9090/tcp, :::9092->9090/tcp         adoring_margulis
dcec640b8555   47dac5755d84      "/bin/prometheus --c…"   7 days ago       Up 43 minutes                   0.0.0.0:9091->9090/tcp, :::9091->9090/tcp         quirky_kepler
  • Menjalankan docker di background proses
docker run -it -d centos
3c5badf505eac7acada0218ba31a1ce49dcfa04cf538f49b2ef2194893eedb1e

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS          PORTS                                       NAMES 
3c5badf505ea   centos         "/bin/bash"              3 seconds ago   Up 1 second                                                 charming_jennings
722fc8cee550   47dac5755d84   "/bin/prometheus --c…"   7 days ago      Up 47 minutes   0.0.0.0:9092->9090/tcp, :::9092->9090/tcp   adoring_margulis
dcec640b8555   47dac5755d84   "/bin/prometheus --c…"   7 days ago      Up 47 minutes   0.0.0.0:9091->9090/tcp, :::9091->9090/tcp   quirky_kepler
  • Masuk docker kedalam bash pada docker yang sedang aktif
docker exec -it 3c5badf505ea /bin/bash

[root@3c5badf505ea /]# uname -a
Linux 3c5badf505ea 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
  • Menonaktifkan docker yang sedang running
docker kill 3c5badf505ea
3c5badf505ea

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED      STATUS          PORTS                                       NAMES
722fc8cee550   47dac5755d84   "/bin/prometheus --c…"   7 days ago   Up 51 minutes   0.0.0.0:9092->9090/tcp, :::9092->9090/tcp   adoring_margulis
dcec640b8555   47dac5755d84   "/bin/prometheus --c…"   7 days ago   Up 51 minutes   0.0.0.0:9091->9090/tcp, :::9091->9090/tcp   quirky_kepler
  • Menghapus docker yang sedang tidak aktif
docker rm 3c5badf505ea
  • Menjalankan docker di background dengan custom port
docker run -d -p 9093:9090 prom/prometheus
80bb5e3ac6ce7e92e73e311addb084fca864c895ec8cd02b9262476a4427642e

docker ps

CONTAINER ID   IMAGE             COMMAND                  CREATED           STATUS          PORTS                                       NAMES
80bb5e3ac6ce   prom/prometheus   "/bin/prometheus --c…"   5 seconds ago Up  4 seconds       0.0.0.0:9093->9090/tcp, :::9093->9090/tcp   great_kalam
722fc8cee550   47dac5755d84      "/bin/prometheus --c…"   7 days ago        Up 55 minutes   0.0.0.0:9092->9090/tcp, :::9092->9090/tcp   adoring_margulis
dcec640b8555   47dac5755d84      "/bin/prometheus --c…"   7 days ago        Up 55 minutes   0.0.0.0:9091->9090/tcp, :::9091->9090/tcp   quirky_kepler
  • Menjalankan docker menjadi autostart saat boot
docker update --restart always 80bb5e3ac6ce

80bb5e3ac6ce
  • Restart container docker
docker restart 80bb5e3ac6ce

80bb5e3ac6ce
  • Menghentikan docker yang sedang running
docker stop 80bb5e3ac6ce

80bb5e3ac6ce
  • Hapus docker image, pastikan tidak aada docker yang menggunakan image sebelum di hapus, docker ps -a
docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
hello-world       latest    feb5d9fea6a5   27 hours ago   13.3kB
centos            latest    5d0da3dc9764   9 days ago     231MB
prom/prometheus   latest    47dac5755d84   10 days ago    193MB

docker rmi 5d0da3dc9764

Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59

docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
hello-world       latest    feb5d9fea6a5   27 hours ago   13.3kB
prom/prometheus   latest    47dac5755d84   10 days ago    193MB

Install Docker Compose

  • Versi terbaru docker compose bisa dilihat disini: https://github.com/docker/compose/releases
  • Install Docker Compose, ganti huruf v2.17.0 sesuai dengan versi yang ingin di install
sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • Tambahkan executable permission dan buat symbolic link ke file /usr/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  • Cek versi Docker Compose
docker-compose --version
Docker Compose version v2.17.0

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.