Dockerイメージを実行するときにシェルパラメータを渡す方法は?

Dockerイメージを実行するときにシェルパラメータを渡す方法は?

次のパラメータを受け入れるbashスクリプトを実装しました。

SYNOPSIS
run.sh: run.sh [-H|-L|-P profile -D device [-R runtime]]
        list the fio profiles natively supported by the docker images or
        execute the selected profile on the targe devices within given
        duration, notice that the option L and P, D, R are mutually exclusive
                   
        Options:
          -H display this message
          -L list the profiles supported by the docker
          -P the profile name need to execute
          -D the device name(s) need to start FIO jobs, support format as /dev/vd[a-b]
          -R the exeuction time, optional, will run forever if not specified

# use case1:list all the profiles
# ./run.sh -L
###################################
#         Profiles supported      #
###################################
fio_profile_1.py
fio_profile_2.py
fio_profile_3.py
fio_profile_4.py

# use case2, start FIO job with profile_1 on vdb2 for 30 seconds
./run.sh -P fio_profile_1.py -D '/dev/vdb2' -R 30

このスクリプトをdockerにパッケージ化し、dockerファイルに次のセクションを含めました。

EXPOSE 8000
ENTRYPOINT ["/bin/sh", "/opt/runall.sh"]

次のコマンドを使用してdockerを実行してパラメータを渡すことはできますか?

docker run --privileged -v /dev:/dev -v workspace:/root  --rm fiotools/tool-aio:latest -P 'fio_profile_1.py' -D '/dev/vdb2' -R 39

しかし今、もう一つの問題に直面しました。スクリプトは実行中に期待どおりに FIO の出力を表示しますが、docker で実行すると失敗します。

# running with docker
# docker run --privileged -v /dev:/dev -v workspace:/root --attach stdout --rm fiotools/tool-aio:latest -P 'fio_profile_1.py' -D '/dev/vdb2' -R 39
working on the workspace /root/job.2023_02_06_16_21_32
# 

# running with shell inside the docker
# ./run.sh -P 'fio_profile_1.py' -D '/dev/vdb2' -R 39
working on the workspace /root/job.2023_02_06_16_25_13
Jobs: 1 (f=1): [m(1)][15.4%][r=46.9MiB/s,w=20.5MiB/s][r=5999,w=2622 IOPS][eta 00m:33s]

bashスクリプト(run.sh)は、以下のようにfioコマンドを実行する以外は何もしません。 FIO出力を取得する方法がわかりますか?

    echo "working on the workspace ${target}"
    ... ...
    cd ${target} && fio --write_bw_log=rw --write_iops_log=rw --write_lat_log=rw --output=fio.output --output-format=json $jobfiles

    fio2gnuplot -t ${job}-bw -b -g -p '*_bw*'
    fio2gnuplot -t ${job}-iops -i -g -p '*_iops*'

ベストアンサー1

CMD ["/opt/run.sh"]に変更する必要がありますENTRYPOINT [] ...

以下を使用できます。

ENTRYPOINT /opt/run.sh $@

または(上記のオプションに問題がある可能性があるため、このオプションを使用することをお勧めします)

ENTRYPOINT ["/bin/sh", "/opt/run.sh"]

/opt/run.sh実行権限があることを確認してください。

Dockerイメージを実行するには(ビルドが完了した後)、次のコマンドを実行します。

docker run --rm image:latest -P fio_profile_1.py -D '/dev/vdb2' -R 30

また、スクリプトには以下が含まれています。

-P the profile name need to execute

そのため、Docker イメージを実行し、そのパラメーターをファイルと共に指定すると、そのファイルが Docker コンテナーに存在しないため、エラーが発生します。

考えられる解決策の1つは、次のようにファイルをボリュームに渡すことです-v

docker run --rm -v /path/to/fio_profile_1.py:/fio_profile_1.py image:latest -P fio_profile_1.py -D '/dev/vdb2' -R 30

-P fio_profile_1.py指定されたボリュームとファイル名は:/fio_profile_1.py同じ名前でなければなりません。


Pythonファイルを実行する別の回避策は、次のように内容全体をパラメータとして渡すことです。

docker run --rm image:latest -P "$(cat fio_profile_1.py)" -D '/dev/vdb2' -R 30

-Pコンテンツの値を取得したら、次のようにfio_profile_1.pyファイルを実行できます。

# Assuming the content of python is script is in `$2`:
printf "%s\n" "$2" | python

おすすめ記事