Benjamin Schüller
Benjamin Schüller

Reputation: 2189

Replace first value of a cmd in a dockerfile

I'm testing around with docker and try to create own docker images.

My first Dockerfile just take an ubuntu as basic image and installs cowsay.

Dockerfile 1:

FROM ubuntu:latest
RUN apt-get update && apt-get -y install cowsay
ENTRYPOINT ["usr/games/cowsay"]

I build the image with

docker build -t test/cowsay .

and can run it with

docker run test/cowsay Hello World

In the next step I would like to create an image which puts default text in cowsay if there is no additional argument with the docker call. For the default text i use fortune.

Dockerfile 2

FROM test/cowsay:latest
RUN apt-get update && apt-get -y install fortune
ENTRYPOINT []
CMD /usr/games/fortune -s | /usr/games/cowsay -f tux

I build the image with

docker build -t test/fortune .

and run it with

docker run test/fortune


 ________________________________________
/ You possess a mind not merely twisted, \
\ but actually sprained.                 /
 ----------------------------------------
   \
    \
        .--.
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\
    \___)=(___/

But at least I want to use the default text from fortune only if there is no additional argument in the docker call.

If I type

docker run test/fortune Hello

the /usr/games/fortune -s has to be replaced with Hello.

 _______
< Hello >
 -------
   \
    \
        .--.
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\
    \___)=(___/

Has anyone an idea how to solve my problem?

Upvotes: 0

Views: 206

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146620

You will have to do this using a bash script and ENTRYPOINT

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT /entrypoint.sh

entrypoint.sh

if [ $# -eq 0 ]
  then
    /usr/games/fortune -s | /usr/games/cowsay -f tux
else
   echo $@ | /usr/games/cowsay -f tux
fi

Upvotes: 1

Related Questions