Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to Docker container

Tags:

docker

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 "!"

like image 421
Ulybin Vitaliy Avatar asked May 26 '17 09:05

Ulybin Vitaliy


People also ask

Can we pass arguments to docker container?

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.

How do you pass a command into a docker container?

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.

How do I pass an environment variable in Dockerfile?

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.

Does docker inherit environment variables?

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 (...)


2 Answers

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!
like image 126
β.εηοιτ.βε Avatar answered Sep 19 '22 19:09

β.εηοιτ.βε


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.

like image 43
Here_2_learn Avatar answered Sep 19 '22 19:09

Here_2_learn