Passing parameters to Docker container

Your problem might be coming from an underlaying incomprehension of string concatenation done by your Ubuntu shell

$ echo "Hello" "I" "am" "a" "developer"
Hello I am a developer

This works because the shell does not have a concatenation operator. So actually feeding echo with strings will make an echo of all those strings concatenated, there is no real black magic in that, and, as far as I know, the strings are concatenated in the order they are passed to echo.

Now what you are really trying to achieve here could easily be done with printf that can substitue a format from arguments:

printf 'Hello %s!' 'Bob'
> Hello Bob!

More info about printf substitution could be found there.

So if your Dockerfile would be:

FROM ubuntu:14.04
ENTRYPOINT ["printf", "Hello %s!"]
CMD ["World"]

You will get your expected result.

$ docker build -t demo .

$ docker run --rm demo 
Hello World!

$ docker run --rm demo Bob
Hello Bob!

CMD :

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

ENTRYPOINT :

Runs the container to not to override the executable which is specified in the image. The use of ENTRYPOINT sends a strong message that this container is only intended to run this one command.

Hence in your case when you run :

docker run --rm container1 Bob 

the CMD is replaced with the string Bob and hence the result.

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

Hope this helps.

More on ENTRYPOINT and CMD

EDIT : Adding a basic example to demonstrate how to get parameters from command line.

#cat Dockerfile
FROM ubuntu:14.04

ENTRYPOINT ["/bin/ping"]

In the above docker file the command(CMD) to execute has not mentioned.It indicates that the container expects some arguments when it is started.It can be provided through command line as below:

docker run --dns=172.24.100.50 -it stack:2.0 -c 1 google.com
PING google.com (216.58.197.78) 56(84) bytes of data.
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=1 ttl=54 time=68.2 ms

--- google.com ping statistics ---

1 packets transmitted, 1 received, 0% packet loss, time 0ms

The same can be achieved by adding the paramters into the dockerfile using CMD as below:

cat Dockerfile
FROM ubuntu:14.04

ENTRYPOINT ["/bin/ping"]

CMD ["-c", "1", "google.com"]

Now run the container without providig an paramaters while starting:

docker run --dns=172.24.100.50 stack:4.0
PING google.com (216.58.197.78) 56(84) bytes of data.
64 bytes from maa03s21-in-f78.1e100.net (216.58.197.78): icmp_seq=1 ttl=54 time=50.0 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms

EDIT 2 :

If you are looking specifically, then you can run the container as

docker run --rm container1 Bob!

As far as I know no option to insert in between as you need.

Tags:

Docker