【问题标题】:How to run a bash script from dockerfile如何从 dockerfile 运行 bash 脚本
【发布时间】:2021-08-26 17:25:37
【问题描述】:

我正在使用 bash 脚本 (run.sh) 来运行 python 文件 (calculate_ssp.py)。代码工作正常。文件夹结构如下所示

├── dataset
    └── dataset_1.csv
├── Dockerfile
├── __init__.py
├── output
├── run.sh
├── scripts
│   ├── calculate_ssp.py
│   ├── __init__.py

bash 脚本是

#!/bin/bash

python3 -m scripts.calculate_ssp 0.5

现在,我正在尝试从 Dockerfile 运行 bash 脚本 (run.sh)。 Dockerfile的内容是

#Download base image ubuntu 20.04
FROM ubuntu:20.04

#Download python
FROM python:3.8.5

ADD run.sh .

RUN pip install pandas
RUN pip install rdkit-pypi

CMD ["bash", "run.sh"]

但是,我收到一个错误/usr/local/bin/python3: Error while finding module specification for 'scripts.calculate_ssp' (ModuleNotFoundError: No module named 'scripts')

您能告诉我为什么会收到错误消息(因为代码在 bash 脚本中运行良好)吗?

【问题讨论】:

  • 您的 Dockerfile 永远不会将 Python 脚本添加到映像中。
  • 另外,FROM ubuntu:20.04 并没有(我相信)做任何有效的事情,因为您会立即切换到 Python 基础映像。
  • @感谢您的评论。那么我应该删除FROM ubuntu:20.04 吗?而且,我必须做些什么来解决这个问题?
  • 您必须将scripts 包添加到您的图像中,就像您必须添加run.sh 一样。
  • 喜欢这个ADD scripts.calculate_ssp.py?

标签: python python-3.x bash docker dockerfile


【解决方案1】:

您需要将您的脚本所需的文件全部打包到镜像中。您可以删除 Ubuntu 基础映像,因为它没有被使用。 (python 基础镜像基于已经提供 bash 的其他镜像。)

#Download python
FROM python:3.8.5

RUN pip install pandas
RUN pip install rdkit-pypi

ADD run.sh .
ADD scripts/ ./scripts

CMD ["bash", "run.sh"]

【讨论】:

  • 谢谢。还有一件事,因为我将输出(来自calculate_ssp.py)文件保存在output 文件夹中。所以,我还要加ADD output/ ./output?
  • 不,但这是另一个问题的主题。您需要在映像中创建目录,然后在创建容器时在该路径上挂载一个外部目录。
  • Opps 我收到一个新错误。未找到数据集文件!我用来在calculate_ssp.py中获取数据集文件的代码是dataset = pd.read_csv(os.path.join(os.path.join(os.getcwd(), "dataset"), "dataset_1.csv"), index_col=None)
  • 再次,另一个问题的主题。上面提出的问题已经得到解答。
猜你喜欢
  • 2022-01-15
  • 2020-08-20
  • 2011-01-30
  • 1970-01-01
  • 2023-03-07
  • 2017-11-04
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多