【解決方法】docker コンテナー内の Python スクリプトから clamav を起動する systemctl コマンドを実行するにはどうすればよいですか。

プログラミングQA


Description: I have a simple Python script that tries to check whether ClamAV is "inactive" in the container and if it is then it attempts to start it using 
    <pre lang="Python">import os 
        os.popen("systemctl start clamav-freshclam.service")
        os.popen("systemctl start clamav-daemon")

ただし、これは常に

Python
FileNotFoundError: systemctl clamav-daemon file not found.

ここがトリッキーな部分です – コンテナ内で Python コンソールを起動し、これと同じコードを書くと、サービス (freshclam とデーモンの両方) が開始されます。 ここで何が欠けていますか?

私が試したこと:

私はスクリプトを作成し、コンテナーの python シェルから機能するコマンドを実行しようとしましたが、うまくいきませんでした。

解決策 1

It looks like the systemctl command is not available inside your Docker container.

One way to solve this problem is to install the systemd package inside your container. However, installing systemd inside a container is not recommended since it can cause issues with process management and resource usage.

An alternative solution is to use the supervisor package to manage the ClamAV service inside the container. supervisor is a process control system that provides a convenient way to manage multiple processes within a single container.

Here's an example Dockerfile that shows how to install ClamAV and supervisor, and configure supervisor to manage the ClamAV service:

dockerfile

FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y clamav supervisor && \
    rm -rf /var/lib/apt/lists/*
COPY clamd.conf /etc/clamav/
COPY freshclam.conf /etc/clamav/
COPY supervisord.conf /etc/supervisor/conf.d/
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]<pre>

この Dockerfile では、clamav とスーパーバイザー パッケージをインストールし、ClamAV (clamd.conf と freshclam.conf) とスーパーバイザー (supervisord.conf) の構成ファイルをコンテナーにコピーします。
ClamAV サービスを管理する Supervisord.conf ファイルの例を次に示します。

これ
[supervisord]

のだえもん=真

[program:clamav-freshclam]

command=/usr/bin/freshclam -d -c 4
開始秒 = 0
自動開始=真
自動再起動=真

[program:clamav-daemon]

command=/usr/sbin/clamd –foreground=true –config-file=/etc/clamav/clamd.conf
開始秒 = 0
自動開始=真
自動再起動=真

この Supervisord.conf ファイルでは、clamav-freshclam と clamav-daemon の 2 つのプログラムが定義されています。 command オプションは、プログラムを開始するコマンドを指定します。 startsecs オプションは、スーパーバイザーに、プログラムが起動されるとすぐにプログラムが開始されたと見なすように指示します。 autostart および autorestart オプションは、スーパーバイザーの起動時にプログラムを自動的に開始するかどうか、およびプログラムがクラッシュした場合に自動的に再起動するかどうかを指定します。

このセットアップでは、次のように、supervisorctl コマンドを使用して ClamAV サービスを開始できます。

バッシュ
$ Supervisorctl start clamav-daemon
$ Supervisorctl start clamav-freshclam

また、Python スクリプトでは、次を使用してサービスを開始できます。

Python
私たちをインポート
os.system(“supervisorctl start clamav-freshclam”)
os.system(“supervisorctl start clamav-daemon”)

このアプローチは、コンテナ内に systemd をインストールしなくても機能するはずです。

コメント

タイトルとURLをコピーしました