Docker Community Forums

Share and learn in the Docker community.

Customizing hostname during docker build phase?

(Mnhan32) #1

Is there way to set hostname during docker build phase now?
Said I want to install a license server that is binded with hostname,
do I have to use those weired, auto generated, serial numbers and letters as my hostname? Or, I just need to give up insalling license server with Dockerfile.


(Nathan Le Claire) #2

I think the Docker daemon sets the hostname dynamically at runtime, I don’t think it can be set in the image (output of docker build). Think about it: Would it make sense to have only one static hostname that all running instances of the application would share? Then every running instance of your service would think its hostname was foobar instead of being unique.

You can specify --hostname flag to docker run to set the hostname for the container at runtime. That’s what I would do.

(Mnhan32) #3

Hey there, Thanks for reply.
Like you said, it doesn’t make sense to have a static hostname in build phase in most of case. But for a license server, I will probably not want to have more than one instance running. In this case, in order to make my license keys to match my hostname, I will need to have my hostname set in build phase. It’s true 99 out of 100 cases you would not need/want to assign a static hostname in build phase, but there are always services or situation that you will need it in build phase.
I ended up just installing everything in a running docker, with a base image using docker run -h flag, and then commit it. It would be must easier if I can set hostname in build phase just for this kind of scenario though. I just wondering if there is a workaround.


(Richardpayne) #4

Why is your license server bound to a specific hostname? That seems like an odd design choice. Is it your own app or a third party one?

(Mnhan32) #5

It’s not my app. I I was told that it’s a Flexlm license server, and from what I understand, it is bound to hostname/hostid for license generation.
I am not familiar with license server. nor do I know any IT stuffs well. I am just a hobbyist, and this is kind of experiment I did in my spare time.
It came to me while I was chatting with our IT the possibility to install license server on docker, since some software license server crash a lot, and it will be super fast to just restart docker. Also, it will be easier to manager/transfer them.

(Stevekm) #6

Is there a solution to this? I am trying to figure out how to set up my Dockerfile to change the hostname. The following did not work:

ENV HOSTNAME="foo" - sets the environment variable but does not actually change the active hostname

RUN echo "foo" > /etc/hostname - same as above

RUN sudo /etc/init.d/ start - returns the error /bin/sh: 1: sudo: not found, even after installing sudo or preceeding with USER root

You can specify --hostname flag to docker run to set the hostname for the container at runtime. That’s what I would do.

The software I am installing needs to be pre-configured inside the container upon build, and inherits the current hostname in its configuration and then tries to resolve it. So the desired hostname needs to be both specified and active upon build. Specifying the hostname at runtime creates a lot of complexity since the container needs to configure itself on the fly when started instead of being pre-configured, and will not work without the correct hostname being passed.

(Darthhexx) #7

I found a solution :slight_smile: You just need to set the hostname and run the command that is dependant on the hostname in the same RUN command, since the hostname is reset for each layer.