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:
1 2 | docker context create myvps --docker <span class="token string">"host=ssh://nguyen.huu.kim@myvps"</span> |
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:
1 2 3 4 5 | docker context use myvps myvps Current context is now <span class="token string">"myvps"</span> |
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.
1 2 | docker context create minikube --default-stack-orchestrator<span class="token operator">=</span>kubernetes --kubernetes config-file<span class="token operator">=</span>/home/nguyen.huu.kim/.kube/config --docker <span class="token assign-left variable">host</span><span class="token operator">=</span>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ì?
Ả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:
swarm
vàkubernetes
1 2 3 | 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
1 2 3 4 5 6 7 8 9 10 11 | Usage: docker context <span class="token function">ls</span> <span class="token punctuation">[</span>OPTIONS<span class="token punctuation">]</span> List contexts Aliases: ls, list Options: --format string Pretty-print contexts using a Go template -q, --quiet Only show context names |
Ví dụ
1 2 3 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Usage: docker context update <span class="token punctuation">[</span>OPTIONS<span class="token punctuation">]</span> CONTEXT Update a context Docker endpoint config: NAME DESCRIPTION from Copy named context<span class="token string">'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'</span>s Kubernetes endpoint configuration config-file Path to a Kubernetes config <span class="token function">file</span> context-override Overrides the context <span class="token builtin class-name">set</span> <span class="token keyword">in</span> the kubernetes config <span class="token function">file</span> namespace-override Overrides the namespace <span class="token builtin class-name">set</span> <span class="token keyword">in</span> the kubernetes config <span class="token function">file</span> Example: $ docker context update my-context --description <span class="token string">"some description"</span> --docker <span class="token string">"host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"</span> Options: --default-stack-orchestrator string Default orchestrator <span class="token keyword">for</span> stack operations to use with this context <span class="token punctuation">(</span>swarm<span class="token operator">|</span>kubernetes<span class="token operator">|</span>all<span class="token punctuation">)</span> --description string Description of the context --docker stringToString <span class="token builtin class-name">set</span> the docker endpoint <span class="token punctuation">(</span>default <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> --kubernetes stringToString <span class="token builtin class-name">set</span> the kubernetes endpoint <span class="token punctuation">(</span>default <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> |
Ví dụ
Cập nhật lại description
cho context có tên là minikube
:
1 2 | docker context update minikube --description <span class="token string">"Docker Host for Minikube"</span> |
Output:
1 2 3 | minikube Successfully updated context <span class="token string">"minikube"</span> |
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
1 2 3 4 5 6 7 8 9 10 | Usage: docker context <span class="token function">rm</span> CONTEXT <span class="token punctuation">[</span>CONTEXT<span class="token punctuation">..</span>.<span class="token punctuation">]</span> Remove one or <span class="token function">more</span> contexts Aliases: rm, remove Options: -f, --force Force the removal of a context <span class="token keyword">in</span> use |
Ví dụ
1 2 | docker context <span class="token function">rm</span> minikube |
Output:
1 2 3 | minikube Successfully deleted context <span class="token string">"minikube"</span> |
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
1 2 3 4 | Usage: docker context use CONTEXT Set the current docker context |
Ví dụ
1 2 | docker context use minikube |
Output:
1 2 3 | minikube Current context is now <span class="token string">"minikube"</span> |
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