Dockerfile HOSTNAME docker构建的指令,如docker run -h [英] Dockerfile HOSTNAME Instruction for docker build like docker run -h

查看:641
本文介绍了Dockerfile HOSTNAME docker构建的指令,如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可以使用build来实现相同的功能。

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黑客,以便我可以将主机名设置为localhost。 LD_PRELOAD黑客有不必要的副作用,我遇到麻烦。当使用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

更新1

这与问题如何在Dockerfile中处理特定的主机名,如-h选项,因为正在谈论该文件所产生的/ 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运行自定义主机名,则使用-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构建的指令,如docker run -h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆