0

My task is to create Dockerfile such that it works the following way:

docker build -t test .
Returns Image named test successfully created

docker run --rm test
Returns Hello world!

docker run --rm test Universe
Returns Hello Universe!

What I have so far:
Dockerfile

FROM ubuntu:14.04 LABEL maintainer="trthhrtz" CMD if [ $# -eq 0 ]; then echo "Hello world!"; else echo "Hello " + $@ + "!"; fi 

It does not work in case of input arguments, the error is:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"Universe\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled

1
  • You had selected the right answer before. What has changed since then? Commented Nov 6, 2019 at 15:52

2 Answers 2

2

It would be easier to:

  • define an entrypoint script entrypoint.sh with your command logic scripted in it.
  • COPY that file in your Dockerfile
  • leave CMD undefined

That way, any additional parameter to your docker run -it --rm myImage arg1 arg2 ... command will be passed to the bash entrypoint.sh script, which will interpret $@ correctly, as illustrated in "What does set -e and exec "$@" do for docker entrypoint scripts?".
See "Passing arguments from CMD in docker" for more.

Sign up to request clarification or add additional context in comments.

Comments

1

Make sure you have a correct order of arguments when you do docker command.

For example:

docker run --name test-ubuntu -it d37f4165b5d2 bash
instead of
docker run --name test-ubuntu d37f4165b5d2 -it bash

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.