我理解得对吗?`launcher run` 不使用当前运行的容器?

在尝试理解 launcher run 时,例如:

groot@galaxy:/var/discourse$ sudo ./launcher run app whoami

如果站点正在运行,上述命令不会进入该站点并启动 bash 来执行 whoami 命令,而是会基于上次引导的镜像启动一个新容器,然后在该容器中运行该命令。

换句话说,如果我想使用 launcher 来了解正在容器中运行的实时站点,我应该使用 launcher enter 而不是 launcher run

1 个赞

你好 @EricGT

是的,我认为许多 Discourse 系统管理员会使用:

./launcher enter  <container_name>

……来进入正在运行的容器,并在其中检查状态(或执行任务)。

不过请注意,你可以直接使用 Docker(而且很简单)来完成“你似乎想做的”事情。没有必要使用中间 shell 脚本来获取运行中容器的这些信息:

# docker exec -it  app whoami
root

你可能会发现这个关于 docker exec 命令语法的链接很有用。

让我们再试几个例子玩玩:

# docker exec -it  app ps aux | grep nginx | wc -l
4
# docker exec -it  app ps aux | grep redis | wc -l
2
# docker exec -it app df 
Filesystem     1K-blocks     Used Available Use% Mounted on
overlay         51043548 26426300  22005700  55% /
tmpfs              65536        0     65536   0% /dev
tmpfs            1017712        0   1017712   0% /sys/fs/cgroup
shm               524288        8    524280   1% /dev/shm
/dev/sda        51043548 26426300  22005700  55% /shared
tmpfs            1017712        0   1017712   0% /proc/acpi
tmpfs            1017712        0   1017712   0% /proc/scsi
tmpfs            1017712        0   1017712   0% /sys/firmware
failed to resize tty, using default size
# docker exec -it app du -sh /shared

403M /shared
# docker exec -it app du -sh /shared/uploads
2.0M	/shared/uploads
# docker exec -it app ls -l /var/www/discourse/plugins/
total 36
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 discourse-details
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 discourse-local-dates
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 discourse-narrative-bot
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 discourse-presence
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 discourse-unsupported-browser
drwxr-xr-x 12 discourse root      4096 Jun  7 04:49 docker_manager
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 lazy-yt
drwxr-xr-x 11 discourse root      4096 Jun  7 04:49 neo-revive-discourse
drwxr-xr-x  1 discourse discourse 4096 Jun  7 04:49 poll
root@localhost:/var/discourse# docker exec -it app ls -l /shared
total 112
drwxr-xr-x  3 discourse www-data  4096 May 23 09:57 backups
drwxr-xr-x 10 root      root      4096 Jun  7 04:55 letsencrypt
drwxr-xr-x  4 root      root      4096 May 23 09:43 log
drwxr-xr-x  2 postgres  postgres  4096 May 23 09:43 postgres_backup
drwx------ 19 postgres  postgres  4096 Jun  7 04:57 postgres_data
drwxrwxr-x  3 postgres  postgres  4096 Jun  7 04:57 postgres_run
drwxr-xr-x  2 redis     redis     4096 Jun  8 02:56 redis_data
drwxr-xr-x  2 root      root      4096 May 28 04:19 ssl
drwxr-xr-x  4 root      root      4096 May 23 09:52 state
drwxrwxrwx  4 discourse www-data  4096 Jun  7 04:57 tmp
drwxr-xr-x  3 discourse www-data  4096 May 23 09:46 uploads

希望这些有趣的例子能给你一些灵感,了解如何使用 docker exec 来“了解运行在容器中的实时站点”,正如你向 @EricGT 询问的那样。

这里还有几个有趣的例子:

# docker exec -it app ls -l /shared/tmp/redis.sock
srwxrwxrwx 1 redis redis 0 Jun  7 04:57 /shared/tmp/redis.sock

这个例子其实不是必需的,因为 Unix socket 就在共享卷中,但你可以理解其中的意思:

#docker exec -it app redis-cli -s /shared/tmp/redis.sock monitor
OK

(已截断,包含大量实时流数据)

最后,当然还有我们系统管理员最喜欢的“老把戏”命令:

root@localhost:~# docker exec -it app ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0   6660   292 pts/0    Ss+  Jun07   0:00 /bin/bash /sb
root         627  0.0  0.0   2308    60 pts/0    S+   Jun07   0:01 /usr/bin/runs
root         628  0.0  0.0   2156    68 ?        Ss   Jun07   0:00 runsv cron
root         629  0.0  0.0   2156    64 ?        Ss   Jun07   0:00 runsv rsyslog
root         630  0.0  0.0   2156    68 ?        Ss   Jun07   0:00 runsv redis
root         631  0.0  0.0   2156    64 ?        Ss   Jun07   0:00 runsv postgre
root         632  0.0  0.0   2156    64 ?        Ss   Jun07   0:00 runsv nginx
root         633  0.0  0.0   2156    64 ?        Ss   Jun07   0:00 runsv unicorn
discour+     634  0.0  0.1  15128  2484 ?        S    Jun07   0:35 /bin/bash con
root         635  0.0  0.1  55176  3956 ?        S    Jun07   0:00 nginx: master
postgres     636  0.0  1.2 351840 26132 ?        S    Jun07   0:04 /usr/lib/post
root         637  0.0  0.0   2300    60 ?        S    Jun07   0:00 svlogd /var/l
redis        638  0.3  0.3  56700  7880 ?        Sl   Jun07   4:01 /usr/bin/redi
root         639  0.0  0.0 156184   588 ?        Sl   Jun07   0:00 rsyslogd -n
root         640  0.0  0.0   8436  1216 ?        S    Jun07   0:00 cron -f
www-data     651  0.0  0.3  56628  6852 ?        S    Jun07   0:00 nginx: worker
www-data     652  0.0  0.0  55668  1676 ?        S    Jun07   0:00 nginx: cache 
postgres     657  0.0  1.8 352116 36776 ?        Ss   Jun07   0:01 postgres: 10/

(已截断)

正如你所见或可以想象,docker exec 非常有用,希望这些例子能稍微激发你的灵感,@EricGT

这里还有几个对 Discourse 有用的 docker exec 命令:

6 个赞

那是一个超棒的回复,我非常感谢你的回答,它让我对其他 Docker 系统管理员如何看待重要事项以及如何快速获取相关信息有了更深入的了解。不过,Docker 命令对我来说已是老相识,我真正想要的只是一个简单的“是”或“否”的回答:

我理解得对吗?launcher run 并不会使用当前正在运行的容器?

前几天我花了好几个小时分析 launcher run 命令的 bash 跟踪日志,只是想确认我的分析是否正确。我原本并不认为 launcher run 会为了运行像 whoami 这样的单条命令而启动一个全新的容器。更令人担忧的是,如果人们以为该命令会在当前运行的容器上执行并返回关于正在运行生产站点的容器的反馈,实际上它返回的却是另一个容器的信息。

我完全同意你使用 Docker 命令的方式,这也是我会采用的方法。但同时我也认为,除非你清楚 $# 之间的区别,否则最好还是远离 Docker 命令,转而信任 launcher。

既然我知道你喜欢用带有辅助问题的问句作为引子来分享你觉得很棒的事情,那我也将尝试时不时地偷偷加入这样的问句。:grinning:

亲爱的 @EricGT

是的,我也使用 docker run(不过不是 launcher run);但我并没有找到在 run 命令后追加 shell 命令的理由,因为正如我在回复中提到的,我总是使用 docker exec

很抱歉你觉得我关于为何使用 docker exec 的回复是“借机炫耀”。我向你保证,我确实忙于许多项目,并不需要借机炫耀,我只是想帮助你完成你的任务,因为我在 Discourse 容器中运行 shell 命令时不使用 launcher run,而是如我回复中所述,仅使用 docker exec;目的是帮助你。

祝你在未来的 Discourse 系统管理任务中一切顺利!

享受并保持好奇心!

2 个赞

我之所以专门询问 launch run,是因为我们正在探索自托管的可能性,并正在为其他 Discourse 管理员编写标准操作程序(SOP)手册。为了详细记录每个启动器命令,我需要深入了解它们。目前的 SOP 指出,使用 launcher run 会创建新容器,因此警告不要使用 launcher run

我认为你说得对,./launcher run 会启动一个新容器并在其中运行命令。如果你想在现有容器中运行命令,需要按讨论的那样使用 docker

如果 ./launcher 不是这样工作的,那么在不存在现有容器的情况下,它将无法运行。

5 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.