本文介绍了Dockerfile HOSTNAME 用于 docker build 的指令,如 docker run -h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在构建过程中在 docker 容器中设置主机名,因为某些软件安装使用发现的随机生成的主机名并将该主机名永久烘焙到配置中.

I am attempting to set the hostname inside a docker container during the build since certain software installs use the discovered randomly generated hostname and permanently bake that hostname into the configuration.

虽然可以通过 run -h 交互式运行时设置主机名,但通过 Dockerfile 构建时无法使用相同的功能.

While it is possible to set the hostname when you run interactively via run -h, the same functionality is not available using build via the Dockerfile.

解决此问题的唯一方法是使用 LD_PRELOAD hack,以便我可以将主机名设置为 localhost.LD_PRELOAD hack 有一些不想要的副作用,我无法解决这些副作用.使用docker run -it -h localhost"时,软件安装没有问题.

The only way to work around this is to use LD_PRELOAD hacks so that I can set the hostname to localhost. The LD_PRELOAD hacks have unwanted sideeffects that I am having trouble working around. The software install works without issue when using "docker run -it -h localhost ".

strace 报告安装程序调用 uname 确定主机名.

strace reports that the installer make a call to uname determine the hostname.

uname({sys="Linux", node="12b0c7c7eacb", ...}) = 0

有谁知道如何解决这个限制?

Does anyone know how it might be possible to work around this limitation?

更新 1

这不是问题 How to handle specific hostname like -h option in Dockerfile 的重复问题,因为这是专门讨论的动态生成该文件引起的/etc/hosts"问题.这很容易解决,因为它是一个可写文件.

This is not a duplicate of the question How to handle specific hostname like -h option in Dockerfile as that is talking specifically about "/etc/hosts" problems arising from that file being dynamically generated. This is easily worked around since it is a writable file.

这是关于尝试从系统调用(例如 uname 和 gethostname)解析主机名的软件安装.据我所知,这是无法解决的,因为无法在正在运行的 docker 容器中更改主机名.uname 系统调用可能引用/proc/sys/kernel/hostname,这是只读的,不能更改.通常可以运行 hostname 命令,但是该命令会生成一个错误,即即使您是 root,您也必须是 root.解决方法是使用 -h 标志,这在构建中不可用.

This is about software installs that attempt to resolve the hostname from system calls such as uname and gethostname. From what I can tell this cannot be worked around since the hostname cannot be changed within a running docker container. The uname system call likely references /proc/sys/kernel/hostname, this is read only and cannot be changed. Normally the hostname command could be run, but this command generates an error that you must be root even if you are root. The workaround for this is to use the -h flag, this is not available in builds.

更新 2

对于任何在这里寻找解决方法的人来说,这只需要在 docker 构建期间使用,如果您需要使用 docker run 自定义主机名,请使用 -h 标志.这是基于其他人的工作.

For anyone looking for a workaround here it is, this only needs to be used during the docker build, use the -h flag if you need to customize the hostname with docker run. This is based on someone else's work.

Dockerfile:

Dockerfile:

RUN gcc -o fakehostname.o -c -fPIC -Wall fakehostname.c
RUN gcc -o libfakehostname.so -shared -W1,export-dynamic fakehostname.o -ldl

RUN ..
     export LD_PRELOAD=/u01/app/oracle/libfakehostname.so;
     installer section
    ..

C 来源:

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/utsname.h>

#include <stdio.h>
#include <string.h>

static int (*real_gethostname)(char *name, size_t len);

int uname(struct utsname *buf)
{
 int ret;

 ret = syscall(SYS_uname, buf);

 strcpy(buf->nodename, "localhost");

 return ret;
}

int gethostname(char *name, size_t len)
{
  const char *val;

  /* Override hostname */
  val = "localhost";
  if (val != NULL)
  {
    strncpy(name, val, len);
    return 0;
  }

  /* Call real gethostname() */
  return real_gethostname(name, len);
}

http://github.com/docker/docker/issues 根据 into_the_void,因为这个问题没有解决办法.

http://github.com/docker/docker/issues as per into_the_void as there is no solution to this problem.

推荐答案

让我看看我是否理解您的问题,您想构建一个在作为容器运行时具有运行时主机名的映像,即使用于构建的主机名不一样.正确的?如果是这样,我向您提出的问题如下,您能否在安装软件后重新配置软件以使用新主机名?

Let me see if I understand your question, you would like to build an image that when run as a container has the runtime hostname even if the hostname used for building is not the same. Correct? If so, my question to you is the following, are you able to reconfigure the software to have a new hostname after it is installed?

如果可能的话,我建议编写一个能够修改主机名的脚本并将此脚本用作 ENTRYPOINT.这样,您可以保证在容器运行时(使用任何命令)已更正主机名,并且您不会花时间尝试在构建时强制支持特定的主机名,您自己承认,这很困难去做.

If this is possible, I would recommend writing a script that is able to modify the hostname and use this script as an ENTRYPOINT. This way you can guarantee that you have corrected the hostname anytime your container is run (with any command) and you don't spend time trying to force support for a particular hostname through at build time, which, by your own admission, is difficult to do.

这篇关于Dockerfile HOSTNAME 用于 docker build 的指令,如 docker run -h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:31