Search code examples
bashdockerdocker-build

Using Docker, how to change bash version?


Does anyone know of a reliable way to switch between Bash version 3 and Bash version 4 in a Docker container?

For testing purposes, I want to run one container with version 3 and one with version 4, however technically I don't need to switch versions, just need to build an image with the two different versions.

ARG base_image
FROM $base_image

RUN # install bash here

ENTRYPOINT bash -c "whatevs"

in my case, I care most about Debian/Ubuntu base images. But I suppose it would be interesting to find a generic bash install command that could work for most if not all *nix machines.

Btw, the above syntax is the way to create a generic base image, and you would build it like so:

docker build -t foo --build-arg base_image="$base_image"  .

Solution

  • There are already official bash images for all the versions, so you can just:

    docker run -it bash:4.4 -c 'whatevs'
    

    Or:

    docker run -it bash:3.2 -c 'whatevs'
    

    If for some reason you can't use the official images, building a single image with two versions of bash will probably require that you install at least one from source. For example, you could start with ubuntu:18.04, which has bash 4.4.19, and then build and install another version into /usr/local.


    If you want to build and install Bash yourself, you're going to need:

    • A functioning development environment (C compiler, make, autoconf, etc)
    • The bash sources

    This is actually a great situation for making use of a multi-stage build, since you don't necessarily want that build environment cluttering up the final image. Here's one way of tackling it:

    ##
    ## 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
    

    If you use this to build an image called basher, you can then...

    $ 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.
    

    And:

    $ 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.