user1536782
user1536782

Reputation: 97

How to pass parameter into docker container and update file in container with passed parameter

I'm new to docker, am trying to pass the local hostname ($Hostname) into docker container and change an entry of configuration file in a container with the new $Hostname.

I added a command in dockerfile as below:

RUN echo $Hostname >> /etc/***.config

and running docker from image

run -e $Hosname='cat /etc/hostname' ...

However, the $Hostname in the container is container's hostname instead of local host's. Can anyone help me with this? Thanks a lot!

Upvotes: 0

Views: 1251

Answers (2)

John Weldon
John Weldon

Reputation: 40739

Generally speaking; the -e flag sets the specified environment variable at invocation time.

Try this instead:

docker run -e PARENT_HOSTNAME=${HOSTNAME} ...

(see @code_monk's answer for the specifics about setting the containers hostname to the same as the docker host)

I don't think you usually need or want the docker host hostname in the container, but that will set the environment variable in the container.

Upvotes: 0

code_monk
code_monk

Reputation: 10120

The hostname of the container is actually one of the few things you can't change using normal methods. This is because the docker engine needs to know it's controlling this in order to handle linking and networks.

To set the hostname in a way that docker engine respects, do this:

docker run --hostname myhostname imagename

The Network Settings section in the docs explain how this works

Upvotes: 1

Related Questions