繁体   English   中英

使用 bash 脚本运行 Dockerfile

[英]Running Dockerfile with bash script

嗨,我正在尝试按照 Dockerfile 运行这个

FROM ubuntu:20.04
ADD install /
RUN chmod u+x /install
RUN /install
ENV PATH /root/miniconda3/bin:$PATH
CMD ["ipython"] 

结合这个 bash 脚本

#!/bin/bash
apt-get update
apt-get upgrade -y
apt-get install -y bzip2 gcc git htop screen vim wget
apt-get upgrade -y bashapt-get clean
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O Miniconda.sh
bash Miniconda.sh -b
rm -rf Miniconda.sh
export PATH="/root/miniconda3/bin:$PATH"
conda update -y conda python
conda install -y pandas
conda install -y ipython

Dockerfile 和 bash 脚本在同一个文件夹中,真的不知道我在这里做错了什么。 这是我得到的错误:

 $ docker build -t py4fi:basic .
[+] Building 0.5s (8/8) FINISHED                             
 => [internal] load build definition from Dockerfile    0.0s
 => => transferring dockerfile: 31B                     0.0s 
 => [internal] load .dockerignore                       0.0s 
 => => transferring context: 2B                         0.0s 
 => [internal] load metadata for docker.io/library/ubu  0.0s 
 => [internal] load build context                       0.0s 
 => => transferring context: 434B                       0.0s 
 => [1/4] FROM docker.io/library/ubuntu:20.04           0.0s 
 => CACHED [2/4] ADD install /                          0.0s
 => CACHED [3/4] RUN chmod u+x /install                 0.0s 
 => ERROR [4/4] RUN /install                            0.4s 
------                                                       
 > [4/4] RUN /install:
#8 0.416 /bin/sh: 1: /install: not found
------
executor failed running [/bin/sh -c /install]: exit code: 127

有什么想法我在这里做错了吗?

TL;博士

On all platforms, even containerized, Bash scripts must have Unix-style line endings (LF), except in some cases on Windows, like with sufficiently recent versions Git Bash.

细节

当我在计算机上保存带有 CRLF 行结尾的install文件时,我刚刚重现了您的确切错误消息。

在 Windows 上,Git-Bash 被修补以容忍 Windows 风格的 CRLF 行尾,但在所有其他平台上 Bash 只接受 Unix 风格的 LF 行尾。 当您在 docker 构建中运行install时,它使用与基本映像一起分发的 Bash ,它最终会寻找(而不是找到) /bin/bash\0x0D而不是/bin/bash

解决方案很简单,您必须将install文件的换行符转换为 LF。 (Dockerfile 可以有任一行结尾。)在我的计算机上,VSCode 在状态栏的右侧显示了一个CRLF (请参阅下面的屏幕截图),我可以单击它以更改为LF并使用 Unix 行再次保存文件结局。

改变: 在此处输入图像描述

至: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM