Skip to content

Commit 84bd4b4

Browse files
committed
post: docker context
1 parent f8b809e commit 84bd4b4

File tree

43 files changed

+2061
-185
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2061
-185
lines changed

content/posts/2025-11-14_python-fastapi-logs.TO_DO.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

content/posts/2025-11-21_python-asyncio-02-2.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,53 @@ asyncio.run(main())
364364
建议是使用 `get_running_loop`,在没有事件循环的时候会抛出一个报错,从而避免“惊喜”。
365365

366366
## Using debug mode
367+
368+
asyncio 提供了 debug mode,在这种模式下若协程运行操作 100 毫秒,将会看到几条有用的信息。
369+
此外,如果不 await coroutine 将会抛出报错,这样就能知道何时正确抛出 await。
370+
371+
### Using asyncio.run
372+
373+
`asyncio.run` 函数有一个 debug 参数,默认是 `False`,可以将其设置为 `True` 来开启调试模式。
374+
375+
```Python
376+
asyncio.run(coroutine(), debug=True)
377+
```
378+
379+
调试模式可以通过参数 `-X dev` 实现
380+
381+
```shell
382+
python3 -X dev program.py
383+
```
384+
385+
或者通过`python asyncio debug`环境变量实现
386+
387+
```shell
388+
PYTHONASYNCIODEBUG=1 python3 program.py
389+
```
390+
391+
在调试模式下,如果一个协程运行时间过程,就会看到提示信息。
392+
可以使用下面 CPU-bound 代码来测试调试模式
393+
394+
```Python
395+
import asyncio
396+
from util import async_timed
397+
398+
@async_timed()
399+
async def cpu_bound_work() -> int:
400+
counter = 0
401+
for _ in range(100000000):
402+
counter += 1
403+
return counter
404+
405+
async def main():
406+
task_one = asyncio.create_task(cpu_bound_work())
407+
await task_one
408+
409+
asyncio.run(main(), debug=True)
410+
```
411+
412+
会看到有这样的输出信息
413+
414+
```shell
415+
Executing <Task finished name='Task-2' coro=<cpu_bound_work() done, defined at /Users/starslayerx/asyncio/util/async_timer.py:8> result=100000000 created at /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/tasks.py:420> took 6.085 seconds
416+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
+++
2+
date = '2025-11-25T8:00:00+08:00'
3+
draft = false
4+
title = 'Docker Context'
5+
tags = ['Docker']
6+
+++
7+
8+
Docker Context 是 Docker 2019 年引入的一个特性,用来管理多个 Docker 主机的上下文,通过切换 context 就能让本地的 docker 命令作用在不同的 Docker 主机上。
9+
10+
本地开发机:
11+
12+
```shell
13+
docker context use default
14+
```
15+
16+
远程服务器要配置好 ssh 免密登陆,然后使用下面命令添加 context:
17+
18+
```shell
19+
docker context create my-server --docker "host=ssh://[email protected]"
20+
docker context create my-server --docker "host=CompanyServer1"
21+
```
22+
23+
这里的 `CompanyServer1` 是 ssh 配置,例如这样
24+
25+
```ssh
26+
Host CompanyServer1
27+
Hostname 192.168.0.106
28+
User root
29+
Port 22
30+
IdentityFile ~/.ssh/id_rsa_company
31+
```
32+
33+
其中 `IdentityFile` 是存放无密码密钥的地方,如果你的密钥密钥密码就不需要这一行,否则需要设置一个没有密码的密钥,例如这样
34+
35+
```shell
36+
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_company -N ""
37+
```
38+
39+
配置好后就可以在本地连接服务器 docker 了
40+
41+
```shell
42+
docker context list
43+
```
44+
45+
输出类似这样
46+
47+
```
48+
NAME DESCRIPTION DOCKER ENDPOINT ERROR
49+
company-server ssh://CompanyServer1
50+
default Current DOCKER_HOST based configuration unix:///***/docker.sock
51+
desktop-linux * Docker Desktop unix:///***/docker.sock
52+
```
53+
54+
使用命令 use 切换 context
55+
56+
```shell
57+
docker context use company-server
58+
```
59+
60+
如果要删除 context,使用 remove 命令
61+
62+
```shell
63+
docker context remove company-server
64+
```
65+
66+
那么使用 docker context 和登陆服务器再用 docker 相比有什么好处呢?
67+
68+
一大优势就是,当前的 AI Agent 还不能登陆到服务器里面去,如果你的 docker 镜像出了问题,想要 ai 帮你排查,那么使用 context 就很方便,ai 运行 docker 命令直接作用于服务器。

public/archives/index.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@
5151
<span class="max-w-[4rem] md:max-w-none truncate">Home</span></a></li><li class="flex items-center gap-1 md:gap-2 min-w-0"><span class="text-muted-foreground/50 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
5252
</span><span class="text-foreground flex items-center gap-0.5 md:gap-1 font-medium min-w-0 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg>
5353
<span class="max-w-[3rem] md:max-w-none truncate">Archives</span></span></li></ol></nav><header class=mb-8><div class="mb-4 flex items-center gap-3"><svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg><h1 class="text-foreground text-3xl font-bold">Archives</h1></div><p class="text-muted-foreground mb-6">Browse all articles in chronological order and discover what interests you.</p><div class="text-muted-foreground flex items-center gap-4 text-sm"><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
54-
<span>80 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55-
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">78
56-
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">November 2025</h3><p class="text-muted-foreground text-xs">19
57-
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/python-asyncio-02-asyncio-basics-part-2/ class=block>Python Asyncio 02: Asyncio Basics Part 2</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
54+
<span>82 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55+
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">80
56+
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">November 2025</h3><p class="text-muted-foreground text-xs">21
57+
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/docker-context/ class=block>Docker Context</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
58+
<time datetime=2025-11-25>11-25</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
59+
<span>1
60+
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/fastapi-app-and-request/ class=block>FastAPI app and request</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
61+
<time datetime=2025-11-25>11-25</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
62+
<span>3
63+
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/python-asyncio-02-asyncio-basics-part-2/ class=block>Python Asyncio 02: Asyncio Basics Part 2</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
5864
<time datetime=2025-11-21>11-21</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
5965
<span>6
6066
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/python-asyncio-02-asyncio-basics-part-1/ class=block>Python Asyncio 02: Asyncio Basics Part 1</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>

0 commit comments

Comments
 (0)