I create a container docker. Which should give the following:
docker run --rm container1
> Hello World!
docker run --rm container1 Bob
> Hello Bob!
My Dockerfile:
FROM ubuntu:14.04
ENTRYPOINT ["/bin/echo", "Hello"]
CMD ["World!"]
My output:
docker run --rm container1
> Hello World!
docker run --rm container1 Bob
> Hello Bob
I have a loss "!"
When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.
Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.
Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.
You can pass the values of environment variables from the host to your containers without much effort. Simply don't specify a value in the command line, and make sure that the environment variable is named the same as the variable the containerized app expects: $ docker run -e var_name (...)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With