Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker argument to a RUN echo command

Tags:

docker

As part of a Dockerfile, I am attempting to modify a text file (ssh_config) to contain a variable passed through by the user (as an ARG) to the docker container at build time.

In my Dockerfile I have (this is not the entire file):

ARG key_name RUN echo 'Host geoserver\n\ User user\n\ HostName 38.191.191.111\n\ IdentityFile /root/$key_name' >> /etc/ssh/ssh_config 

This collects the argument key_name, and then appends some text to the ssh_config text file.

This is run as follows:

docker build --build-arg key_name=GC -t pyramid . 

When I check to see what has been written, the key_name variable hasn't been parsed, and instead has been written as text (so literally as $key_name). Obviously I want it to be replaced with the variable passed through ARG.

I have tried using ${key_file} instead of just $key_file, I just get the same text in the text file but with curly braces included.

So my question is, how can I use the ARG variable correctly within the RUN echo statement?

like image 236
Single Entity Avatar asked Nov 23 '17 16:11

Single Entity


People also ask

How do I pass args to ENTRYPOINT docker?

So if you want to pass the URL argument to ENTRYPOINT, you need to pass the URL alone. The reason is we have the ab command as part of the ENTRYPOINT definition. And the URL you pass in the run command will be appended to the ENTRYPOINT script. In this case, CMD instruction is not required in the Dockerfile.

How do I pass a variable in docker run?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

How do I run a command in 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.


2 Answers

First: Make sure, your ARG comes after your FROM. See: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

Second: As you can see here, variables won't be interpretad inside '', so use "" instead.

like image 69
Munchkin Avatar answered Sep 25 '22 18:09

Munchkin


When you surround the variable with single quotes it doesn't get replaced.

If you need the single qoutes in the file just surround everything with double quotes, otherwise just remove the single quotes all together.

ARG key_name RUN echo "'Host geoserver\n\ User user\n\ HostName 38.191.191.111\n\ IdentityFile /root/$key_name'" >> /etc/ssh/ssh_config 
like image 43
yamenk Avatar answered Sep 25 '22 18:09

yamenk