Customizing hostname during docker build phase?

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/hostname.sh 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.