我有一个Dockerfile,该文件最初是从ubuntu中提取的,最近我遇到了更轻巧的alpine,因此想从中提取它。我尝试构建的部分代码称为Healpix,它取决于cfitsio。当我最初构建ubuntu版本时,我发现了这个Dockerfile https://github.com/MilesCranmer/dockers/blob/master/dockerfiles/healpix

本质上,问题是在ubuntu中有以下作品,但在alpine中却没有:

RUN echo "3\ngfortran\n\nY\n\n\ngcc\n\n\n\n\nN\n1\nY\nN\nN\n0\n" |
    ./configure && make

我得到的错误是
Something went wrong ...
Quitting configuration script !

./configure: exit: line 162: Illegal number: -1
The command '/bin/sh -c echo     "3\ngfortran\n\nY\n\n\ngcc\n\n\n\n\nN\n1\nY\nN\nN\n0\n" | ./configure     && make' returned a non-zero code: 2

令人困惑的是,配置脚本的https://sourceforge.net/p/healpix/code/HEAD/tree/branches/branch_v350r1006/configure长度不是162行。我尝试安装bash并将脚本更改为该脚本,但这没有用。

ubuntu的Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y gcc g++ gfortran make wget
WORKDIR /home
RUN wget \
   http://heasarc.gsfc.nasa.gov/FTP/software/fitsio/c/cfitsio_latest.tar.gz \
   && tar xzf cfitsio_latest.tar.gz
WORKDIR cfitsio
RUN ./configure --prefix=/usr && make && make install
WORKDIR /home
RUN wget \
   https://sourceforge.net/projects/healpix/files/Healpix_3.50/Healpix_3.50_2018Dec10.tar.gz \
   && tar xzf Healpix*.tar.gz
WORKDIR Healpix_3.50
RUN echo \
    "3\ngfortran\n\nY\n\n\ngcc\n\n\n\n\nN\n1\nY\nN\nN\n0\n" | ./configure \
    && make

Alpine Dockerfile
FROM alpine
RUN apk --no-cache add gcc g++ gfortran make wget
WORKDIR /home
RUN wget \
   http://heasarc.gsfc.nasa.gov/FTP/software/fitsio/c/cfitsio_latest.tar.gz \
   && tar xzf cfitsio_latest.tar.gz
WORKDIR cfitsio
RUN ./configure --prefix=/usr && make && make install
WORKDIR /home
RUN wget \
   https://sourceforge.net/projects/healpix/files/Healpix_3.50/Healpix_3.50_2018Dec10.tar.gz \
   && tar xzf Healpix*.tar.gz
WORKDIR Healpix_3.50
RUN echo \
    "3\ngfortran\n\nY\n\n\ngcc\n\n\n\n\nN\n1\nY\nN\nN\n0\n" | ./configure \
    && make

最佳答案

TL; DR

在您的Dockerfile中,使用:

RUN /bin/echo -e "3\ngfortran\n[...]" | ./configure && make

在Ubuntu和Alpine上具有相同的行为。

说明
./configure脚本使用/bin/sh执行(请参阅shebang)。在Ubuntu上,/bin/sh/bin/dash的链接,而在Alpine上,/bin/sh/bin/busybox的链接。

下面的小示例重现您的问题。

考虑以下./configure脚本:
#!/bin/sh

read -p "1st prompt : " first
read -p "2nd prompt : " second

echo "$first-$second"

在Ubuntu上:
docker run --rm -v $PWD/configure:/configure ubuntu:18.04 \
    /bin/sh -c 'echo "a\nb" | ./configure'

版画:



而在 Alpine 上:
docker run --rm -v $PWD/configure:/configure alpine:3.8 \
    /bin/sh -c 'echo "a\nb" | ./configure'

版画:



在Alpine(busybox)上,echo ed字符串(a\nb)被解释为单个参数,而在Ubuntu(dash)上,\n用于分隔两个参数。

要与Alpine上的Ubuntu具有相同的行为,可以运行:
docker run --rm -v $PWD/configure:/configure alpine:3.8 /bin/sh -c 'echo "a
b
" | ./configure'

要么 :
docker run --rm -v $PWD/configure:/configure alpine:3.8 /bin/sh -c \
    'echo -e "a\nb" | ./configure'

(请参阅-eecho参数)

这两个命令打印:



至于Dockerfile,您应该编写如下内容:
RUN /bin/echo -e "3\ngfortran\n[...]" | ./configure && make

使用/bin/echo代替echo,因为在Ubuntu上,echo -e "3\ngfortran\n[...]"将打印-e 3\nngfortran\n[...]

关于docker - 无法使用Alpine OS在Docker中使用echo配置代码,但可以在ubuntu中进行配置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55431538/

10-16 06:02