所有版本都已经有official bash images,所以你可以:
docker run -it bash:4.4 -c 'whatevs'
或者:
docker run -it bash:3.2 -c 'whatevs'
如果由于某种原因您不能使用官方镜像,那么使用两个版本的 bash 构建一个镜像可能需要您从源代码中至少安装一个。例如,您可以从具有 bash 4.4.19 的 ubuntu:18.04 开始,然后在 /usr/local 中构建并安装另一个版本。
如果您想自己构建和安装 Bash,您将需要:
- 功能正常的开发环境(C 编译器、
make、autoconf 等)
- bash 源代码
这实际上是使用multi-stage build 的好地方,因为您不一定希望构建环境弄乱最终图像。这是解决它的一种方法:
##
## Build bash 3
##
FROM ubuntu:18.04 as bash_3
ARG bash_3_version=3.2.57
RUN apt-get update
RUN apt-get -y install build-essential curl bison
WORKDIR /tmp
RUN curl -o bash-${bash_3_version}.tar.gz \
http://ftp.gnu.org/gnu/bash/bash-${bash_3_version}.tar.gz
RUN tar xf bash-${bash_3_version}.tar.gz
WORKDIR /tmp/bash-${bash_3_version}
RUN ./configure --prefix=/opt/bash3
RUN make EXEEXT=3
RUN make install EXEEXT=3
##
## Build bash 4
##
FROM ubuntu:18.04 as bash_4
ARG bash_4_version=4.4.18
RUN apt-get update
RUN apt-get -y install build-essential curl bison
WORKDIR /tmp
RUN curl -o bash-${bash_4_version}.tar.gz \
http://ftp.gnu.org/gnu/bash/bash-${bash_4_version}.tar.gz
RUN tar xf bash-${bash_4_version}.tar.gz
WORKDIR /tmp/bash-${bash_4_version}
RUN ./configure --prefix=/opt/bash4
RUN make EXEEXT=4
RUN make install EXEEXT=4
##
## Build the final image
##
FROM ubuntu:18.04
ENV PATH=/opt/bash4/bin:/opt/bash3/bin:/bin:/usr/bin:/usr/local/bin
COPY --from=bash_3 /opt/bash3 /opt/bash3
COPY --from=bash_4 /opt/bash4 /opt/bash4
如果您使用它来构建一个名为basher 的映像,那么您可以...
$ docker run -it --rm basher bash3 --version
GNU bash, version 3.2.57(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
还有:
$ docker run -it --rm basher bash4 --version
GNU bash, version 4.4.18(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.