【问题标题】:Can't open file when i try to run a shell script inside Docker container当我尝试在 Docker 容器中运行 shell 脚本时无法打开文件
【发布时间】:2016-01-11 22:38:05
【问题描述】:

我已经为此苦苦挣扎了一段时间,但我不知道自己做错了什么。我正在尝试在容器内运行 shell 脚本,并且 shell 脚本从 shell 脚本所在的目录中读取 python 脚本。但我收到此错误消息“python:无法打开文件'get_gene_length_filter.py':[Errno 2] 没有这样的文件或目录”。

这是我的 Dockerfile:

FROM ubuntu:14.04.3
RUN apt-get update && apt-get install -y g++ \
                make \
                git \
                zlib1g-dev \
                python \
                wget \
                curl \
                python-matplotlib \
                python-numpy \
                python-pandas

ENV BINPATH /usr/bin
ENV EVO2GIT https://upendra_35@bitbucket.org/upendra_35/evolinc_docker.git
RUN git clone $EVO2GIT
WORKDIR /evolinc_docker
RUN chmod +x evolinc-part-I.sh && cp evolinc-part-I.sh $BINPATH

RUN wget -O- http://cole-trapnell-lab.github.io/cufflinks/assets/downloads/cufflinks-2.2.1.Linux_x86_64.tar.gz | tar xzvf -
RUN wget -O- https://github.com/TransDecoder/TransDecoder/archive/2.0.1.tar.gz | tar xzvf -

ENV PATH /evolinc_docker/cufflinks-2.2.1.Linux_x86_64/:$PATH
ENV PATH /evolinc_docker/TransDecoder-2.0.1/:$PATH
ENTRYPOINT ["/usr/bin/evolinc-part-I.sh"]
CMD ["-h"]

这是我的 git repo 代码:

#!/bin/bash
# Create a directory to move all the output files
mkdir output
# Extracting classcode u transcripts, making fasta file, removing transcripts > 200 and selecting protein coding transcripts
grep '"u"' $comparefile | gffread -w transcripts_u.fa -g $referencegenome - && python get_gene_length_filter.py transcripts_u.fa \
    transcripts_u_filter.fa && TransDecoder.LongOrfs -t transcripts_u_filter.fa

这就是我运行它的方式:

docker run --rm -v $(pwd):/working-dir -w /working-dir ubuntu/evolinc -c AthalianaslutteandluiN30merged.gtf -g TAIR10_chr.fasta  

【问题讨论】:

    标签: python shell docker


    【解决方案1】:

    我将猜测并假设get_gene_length_filter.py 位于/evolinc_docker 中,即Dockerfile 中声明的工作目录。不幸的是,当您运行docker run ... -w /working-dir ... 时,工作目录将为/working-dir,因此Python 将在/working-dir 中寻找get_gene_length_filter.py,但显然找不到。编辑您的 shell 脚本以通过其完整绝对路径引用get_gene_length_filter.pypython /evolinc_docker/get_gene_length_filter.py

    【讨论】:

    • 我想过,但由于某种原因从未尝试过。是的,它奏效了,结束了我的痛苦。非常感谢您的提示..
    【解决方案2】:

    你的陈述

    shell脚本从shell脚本所在目录读取python脚本。

    错了。

    在您的shell script 中,当您调用python get_gene_length_filter.py 时,不会假定get_gene_length_filter.py 文件与shell 脚本位于同一目录中,而是假定位于当前工作目录中。

    描述相对于当前shell脚本目录的路径,use a variable set as follow

    SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    

    然后你会调用你的python脚本:

    python $SCRIPT_DIR/get_gene_length_filter.py
    

    这样,无论工作目录是什么,您的 shell 脚本都可以工作。

    【讨论】:

    • 是的,现在我意识到 python 脚本实际上与 shell 脚本不在同一路径中。当然我用那个
    • 我将文件复制到我的 /src 的 script_dir 中,并且它有效。看起来很明显,但没有击中我。
    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 2015-10-13
    • 2021-10-22
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多