Home > Net >  How to run a bash script that takes multiple user intactive inputs , as part of dockerfile
How to run a bash script that takes multiple user intactive inputs , as part of dockerfile

Time:11-17

I have the below dockerfile that needs to run a owasp bash file for its intallation. This .sh file needs multiple inputs(like 1, Y, enter) from the user for the completion of installation.

How do I provide these inputs from dockerfile or is there a way to skip these inputs and continue the installation.

This dockerfile is a part of the docker-compose.

Below is thew dockerfile

FROM ubuntu:20.04

RUN apt-get update && apt-get upgrade -y && apt-get clean
RUN apt-get install python3-pip -y
RUN apt-get install vim -y

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Indian

# Install OpenJDK-8
RUN apt-get install -y openjdk-8-jdk && \
    apt-get install -y ant && \
    apt-get clean;

# Fix certificate issues
RUN apt-get update && \
    apt-get install ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

RUN apt-get install wget -y && \
    apt-get install unzip -y && \
    apt-get install zip -y

RUN mkdir /home/owasp

RUN wget -c https://github.com/zaproxy/zaproxy/releases/download/v2.11.0/ZAP_2_11_0_unix.sh -P /home/owasp

RUN chmod u x /home/owasp/ZAP_2_11_0_unix.sh
RUN ./home/owasp/ZAP_2_11_0_unix.sh

CodePudding user response:

I would suggest adding a ENTRYPOINT so it by default will invoke your bash script, but it gives the flexibily to the end user to pass different arguments. See the official docs. Keep in mind the CMD provided in a Dockerfile is a default command. You override it by passing any other value.

FROM ubuntu:20.04

RUN apt-get update && apt-get upgrade -y && apt-get clean
RUN apt-get install python3-pip -y
RUN apt-get install vim -y

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Indian

# Install OpenJDK-8
RUN apt-get install -y openjdk-8-jdk && \
    apt-get install -y ant && \
    apt-get clean;

# Fix certificate issues
RUN apt-get update && \
    apt-get install ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

RUN apt-get install wget -y && \
    apt-get install unzip -y && \
    apt-get install zip -y

RUN mkdir /home/owasp

RUN wget -c https://github.com/zaproxy/zaproxy/releases/download/v2.11.0/ZAP_2_11_0_unix.sh -P /home/owasp

RUN chmod u x /home/owasp/ZAP_2_11_0_unix.sh

ENTRYPOINT ./home/owasp/ZAP_2_11_0_unix.sh

CMD ['--some', '--default', '--args']

You can even choose to pass default flags on build. So your script will then always run with default flags you provided on docker build --build-args DEFAULT_PARAMS=--foo, unless you override it:

ARGS DEFAULT_PARAMS

FROM ubuntu:20.04

ENV DEFAULT_PARAMS=${DEFAULT_PARAMS}

RUN apt-get update && apt-get upgrade -y && apt-get clean
RUN apt-get install python3-pip -y
RUN apt-get install vim -y

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Indian

# Install OpenJDK-8
RUN apt-get install -y openjdk-8-jdk && \
    apt-get install -y ant && \
    apt-get clean;

# Fix certificate issues
RUN apt-get update && \
    apt-get install ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

RUN apt-get install wget -y && \
    apt-get install unzip -y && \
    apt-get install zip -y

RUN mkdir /home/owasp

RUN wget -c https://github.com/zaproxy/zaproxy/releases/download/v2.11.0/ZAP_2_11_0_unix.sh -P /home/owasp

RUN chmod u x /home/owasp/ZAP_2_11_0_unix.sh

ENTRYPOINT ./home/owasp/ZAP_2_11_0_unix.sh

CMD ${DEFAULT_PARAMS}

CodePudding user response:

To provide input for command use some input generator and pipe it with your command.

Typical example is using command yes which provides endless stream of "y" on output:

RUN yes|./own-shell-scrpit.sh

You can run printf 'y\n1abc\nxxx' and pipe it. "\n" in printf states for newline (or enter).

  • Related