BÀI VIẾT

Hướng dẫn cài đặt Docker trên Ubuntu 22.04

3.6K
author Nguyễn Ngọc Đang - 2022-08-11 15:16:16 (GMT+7)

Giới thiệu

Docker là mã nguồn mở nó cho phép tạo các môi trường độc lập và tách biệt để khởi chạy và phát triển ứng dụng và môi trường này được gọi là container. Khi cần deploy lên bất kỳ server nào chỉ cần run container của Docker thì application của chúng ta sẽ được khởi chạy ngay lập tức.

Các thành phần chính của Docker

  • Docker Engine: dùng để tạo ra Docker image và chạy Docker container.
  • Docker Hub: dịch vụ lưu trữ giúp chứa các Docker image.
  • Docker Machine: tạo ra các Docker engine trên máy chủ.
  • Docker Compose: chạy ứng dụng bằng cách định nghĩa cấu hình các Docker container thông qua tệp cấu hình
  • Docker image: một dạng tập hợp các tệp của ứng dụng, được tạo ra bởi Docker engine. Nội dung của các Docker image sẽ không bị thay đổi khi di chuyển. Docker image được dùng để chạy các Docker container.
  • Docker container: một dạng runtime của các Docker image, dùng để làm môi trường chạy ứng dụng.

Cập nhật hệ thống

Để đảm bảo rằng tất cả các phụ thuộc của docker đều được cập nhật, hãy thực thi lệnh bên dưới:

bash (root)
apt-get update && apt-get upgrade

Trong quá trình cập nhật hệ thống chúng ta chờ từ 5 phút đến 15 phút.

Cài đặt Docker

Để cài đặt Docker version mới nhất, các chúng ta nên cài đặt trực tiếp từ kho lưu trữ của Docker.

Cài đặt các gói phụ thuộc cho phép sử dụng HTTPS.

bash (root)
apt install apt-transport-https ca-certificates curl software-properties-common -y

Tiếp theo thêm key GPG của kho lưu trữ Docker.

bash (root)
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Sau đó, thêm kho lưu trữ Docker của Ubuntu 22.04.

bash (root)
echo "deb [arch=$(dpkg --print-architecture) 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

Tiến hành, cập nhật packages và cài đặt Docker từ kho lưu trữ.

bash (root)
apt update && apt-cache policy docker-ce && apt install docker-ce -y

Sau khi cài đặt Docker thành công, tiến hành kiểm tra trạng thái của Docker.

bash (root)
systemctl status docker

Kết quả trả về như sau:

Kết quả
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-08-06 12:31:33 +07; 1min 24s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 8001 (dockerd)
      Tasks: 8
     Memory: 29.2M
        CPU: 655ms
     CGroup: /system.slice/docker.service
             └─8001 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Aug 06 12:31:32 vps-02 dockerd[8001]: time="2022-08-06T12:31:32.783931259+07:00" level=info msg="scheme \"unix\" not register>
Aug 06 12:31:32 vps-02 dockerd[8001]: time="2022-08-06T12:31:32.784170292+07:00" level=info msg="ccResolverWrapper: sending u>
Aug 06 12:31:32 vps-02 dockerd[8001]: time="2022-08-06T12:31:32.784325364+07:00" level=info msg="ClientConn switching balance>
Aug 06 12:31:32 vps-02 dockerd[8001]: time="2022-08-06T12:31:32.844609852+07:00" level=info msg="Loading containers: start."
Aug 06 12:31:33 vps-02 dockerd[8001]: time="2022-08-06T12:31:33.271984296+07:00" level=info msg="Default bridge (docker0) is >
Aug 06 12:31:33 vps-02 dockerd[8001]: time="2022-08-06T12:31:33.443664403+07:00" level=info msg="Loading containers: done."
Aug 06 12:31:33 vps-02 dockerd[8001]: time="2022-08-06T12:31:33.507955480+07:00" level=info msg="Docker daemon" commit=a89b84>
Aug 06 12:31:33 vps-02 dockerd[8001]: time="2022-08-06T12:31:33.508198952+07:00" level=info msg="Daemon has completed initial>
Aug 06 12:31:33 vps-02 systemd[1]: Started Docker Application Container Engine.
Aug 06 12:31:33 vps-02 dockerd[8001]: time="2022-08-06T12:31:33.562513375+07:00" level=info msg="API listen on /run/docker.so>
lines 1-22/22 (END)

Chúng ta có thể sử dụng lệnh sau để có thể xem các thông tin về Docker:

bash (root)
docker info
Kết quả
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 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: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
 runc version: v1.1.2-0-ga916309
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.15.0-35-generic
 Operating System: Ubuntu 22.04.1 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 3.834GiB
 Name: vps-02
 ID: AT2S:DTPB:EP3L:GXIG:73CT:A5J3:FX4G:PDHU:B6BF:SUT2:BJPL:UVNZ
 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

Để có thể tải Docker Images chúng ta sẽ thực thi lệnh sau:

bash (root)
docker run hello-world

Kết quả trả về như sau có nghĩa là chúng ta có thể truy cập và tải xuống images từ Docker Hub.

Kết quả
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:53f1bbee2f52c39e41682ee1d388285290c5c8a76cc92b42687eecf38e0af3f0
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/

Để kiểm tra các docker img hiện có trên máy chủ của chúng ta thực thi lệnh sau:

bash (root)
docker images
Kết quả
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   10 months ago   13.3kB

Tổng Kết.

Qua bài viết hướng dẫn trên chúng ta hoàn tất việc cài đặt Docket trên Ubuntu 22.04. Hy vọng các bạn thành công trong việc cài đặt cũng như sử dụng vào trong thực tế, công việc.


Bạn có làm được theo hướng dẫn này không?

Bài liên quan

Hướng dẫn cài đặt phần mềm thống kê web Umami trên Ubuntu 20.04
Hướng dẫn cài đặt phần mềm thống kê web Umami trên Ubuntu 20.04
Hướng dẫn cài đặt phần mềm thống kê web Ackee trên Ubuntu 20.04
Hướng dẫn cài đặt phần mềm thống kê web Ackee trên Ubuntu 20.04

Object Storage

Chuẩn S3, không giới hạn băng thông

Object Storage

Thuê Server Riêng

Chi phí thấp, cấu hình cao

Thuê Server Riêng
Thông tin tác giả
Bình luận

Tính năng đang được phát triển

Đang tải bình luận
Liên hệ chúng tôi