+15

Docker Context: Tính năng cực kỳ hữu ích nhưng 90% anh em thường bị bỏ qua

Mayfest2023

Hello hello, xin chào tất cả ae. 👋 👋 👋 Chào mừng ae đã quay lại với kênh Viblo của mình. Ae đã vào đây thì thả một comment chào nhau cái cho vui nhé.

Overview

Hồi mới dùng Docker, mình muốn thao tác gì đó trên server đều phải chạy lệnh SSH để access vào server. Mình để ý thấy rất rất nhiều anh em dev cũng giống như mình! Dù đã tìm hiểu, biết cách sử dụng Docker để dựng môi trường phát triển nhưng lại vô tình bỏ qua một tính năng cực kỳ hữu ích. Đó chính là docker context.

Case study

Trước hết hãy xem qua một số ví dụ sử dụng docker context phổ biến nhé.

1. Remote Docker Context qua SSH

Mình sẽ tạo context có tên myvps như sau:

docker context create myvps --docker "host=ssh://nguyen.huu.kim@myvps"

Sau đó, sử dụng context trên để chạy lệnh Docker CLI ngay dưới local mà không phải truy cập vào server nữa:

docker context use myvps

myvps
Current context is now "myvps"

2. Remote Docker Context với Kubernetes

Thí dụ mình sẽ tạo Context để kết nối Docker CLI tới Docker Host là K8s Node chạy Docker. Ở đây mình sử dụng Minikube làm ví dụ minh họa.

docker context create minikube --default-stack-orchestrator=kubernetes --kubernetes config-file=/home/nguyen.huu.kim/.kube/config --docker host=tcp://172.17.0.2:2376

Sử dụng context vừa tạo trên sẽ giúp mình chạy lệnh Docker CLI trên máy local mà không phải truy cập SSH vào server nữa. Vậy Docker Context là gì? Hãy cùng mình tìm hiểu nhé!

Docker Context là gì?

image.png

Ảnh 1: Docker Architecture - Nguồn: Docker

Chúng ta thao tác với Docker bằng cách lệnh chạy trên terminal thông qua bộ command gọi là Docker CLI. Docker CLI sẽ chuyển đổi command lại thành các lời gọi API tương ứng tới Docker daemon. Tất cả những thông tin mô tả về cách kết nối tới tới API (hay docker host) được tập hợp lại trong một thành phần gọi là Docker Context.

Thông tin được lưu trữ

Các thông tin được nắm giữ bởi một Docker Context gồm:

  • Name: Tên của context
  • Description: Mô tả về context
  • Docker Endpoint: URL để kết nối tới Docker daemon (bao gồm cả thông tin về TLS)
  • Kubernetes Endpoint: URL để kết nối tới Kubernetes cluster (trong trường hợp dùng k8s)
  • Orchestrator: Loại kiến trúc đang chạy trên nền Docker, có 2 giá trị lựa chọn: swarmkubernetes
NAME         DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT                 ORCHESTRATOR
default      Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                       swarm

Mặc định, sau khi cài đặt Docker trên Linux. Một cấu hình context có tên là default sẽ được thiết lập. Docker CLI sẽ đọc thông tin từ context để có thông tin kết nối.

Nếu không được add thêm các context khác, thì ngầm hiểu Docker CLI sẽ chỉ tương tác được với Docker daemon trên cùng một máy chủ. Tức Docker máy nào thì chỉ máy đó truy cập được theo context default.

Câu lệnh thường dùng

Liệt kê context đang có

  • Dùng để xem tất cả các context đã được lưu trên máy
  • Các tham số thường ít cần dùng đến, chỉ cần nhớ docker context ls là được

Command line

Usage:  docker context ls [OPTIONS]

List contexts

Aliases:
  ls, list

Options:
      --format string   Pretty-print contexts using a Go template
  -q, --quiet           Only show context names

Ví dụ

NAME         DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT                 ORCHESTRATOR
default      Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                       swarm

1. Thêm context mới

  • Dùng để tạo mới một context trên máy

Command line

docker context create [OPTIONS] CONTEXT

Create a context

Docker endpoint config:

NAME                DESCRIPTION
from                Copy named context's Docker endpoint configuration
host                Docker endpoint on which to connect
ca                  Trust certs signed only by this CA
cert                Path to TLS certificate file
key                 Path to TLS key file
skip-tls-verify     Skip TLS certificate validation

Kubernetes endpoint config:

NAME                 DESCRIPTION
from                 Copy named context's Kubernetes endpoint configuration
config-file          Path to a Kubernetes config file
context-override     Overrides the context set in the kubernetes config file
namespace-override   Overrides the namespace set in the kubernetes config file

Example:

$ docker context create my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"

Options:
      --default-stack-orchestrator string   Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)
      --description string                  Description of the context
      --docker stringToString               set the docker endpoint (default [])
      --from string                         create context from a named context
      --kubernetes stringToString           set the kubernetes endpoint (default [])

Trong đó có 2 phần cấu hình endpoint khác nhau tùy theo orchestrator mà chúng ta đang cấu hình:

  • Docker endpoint config - Các thông tin cần khi cấu hình theo Swarm
  • Kubernetes endpoint config - Các thông tin cần khi cấu hình theo Kubernetes

Ví dụ

Mình sẽ để cập cụ thể cách tạo context với mỗi loại trong phần tiếp theo (bên dưới).

2. Sửa một context

  • Dùng để chỉnh sửa, cập nhật thông tin cho một context đã tồn tại trên máy
  • Các tham số giống hệt với lệnh docker context create

Command line

Usage:  docker context update [OPTIONS] CONTEXT

Update a context

Docker endpoint config:

NAME                DESCRIPTION
from                Copy named context's Docker endpoint configuration
host                Docker endpoint on which to connect
ca                  Trust certs signed only by this CA
cert                Path to TLS certificate file
key                 Path to TLS key file
skip-tls-verify     Skip TLS certificate validation

Kubernetes endpoint config:

NAME                 DESCRIPTION
from                 Copy named context's Kubernetes endpoint configuration
config-file          Path to a Kubernetes config file
context-override     Overrides the context set in the kubernetes config file
namespace-override   Overrides the namespace set in the kubernetes config file

Example:

$ docker context update my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"

Options:
      --default-stack-orchestrator string   Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)
      --description string                  Description of the context
      --docker stringToString               set the docker endpoint (default [])
      --kubernetes stringToString           set the kubernetes endpoint (default [])

Ví dụ

Cập nhật lại description cho context có tên là minikube:

docker context update minikube --description "Docker Host for Minikube"

Output:

minikube
Successfully updated context "minikube"

3. Xóa một context

  • Dùng để xóa bỏ một context đã tồn tại trên máy
  • Cách tham số đi kèm thường ít cần dùng, chỉ cần nhờ docker context rm là được

Command line

Usage:  docker context rm CONTEXT [CONTEXT...]

Remove one or more contexts

Aliases:
  rm, remove

Options:
  -f, --force   Force the removal of a context in use

Ví dụ

docker context rm minikube

Output:

minikube
Successfully deleted context "minikube"

4. Chuyển context

  • Dùng để kích hoạt context từ inactive -> active (không dùng -> đang dùng)
  • Tại một thời điểm chỉ một context được sử dụng

Command line

Usage:  docker context use CONTEXT

Set the current docker context

Ví dụ

docker context use minikube

Output:

minikube
Current context is now "minikube"

5. Others

Ngoài ra còn có một số command khác nhưng ít khi phải dùng, các bạn có thể nghiên cứu thêm:

  • docker context import
  • docker context export
  • docker context inspect

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí