Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Dockerfile中启动进程_Docker_Dockerfile - Fatal编程技术网

在Dockerfile中启动进程

在Dockerfile中启动进程,docker,dockerfile,Docker,Dockerfile,我的问题如下:我需要启动一个运行dnsmasq服务的容器(但这可能是任何服务)。难点在于我在图像中创建了一个用户,所以当我用它创建一个容器时,它从我的自定义用户(没有根)开始 因此,如何使用非root用户启动需要root权限的服务(sudo service dnsmasq start) 可能的解决办法: 能够在服务已运行的情况下启动容器。据我所知,在Dockerfile中启动服务是不可能的,因为它不保留状态,只保留FS 以root用户身份启动容器,启动服务,然后切换回用户。这可能行得通,但可能

我的问题如下:我需要启动一个运行dnsmasq服务的容器(但这可能是任何服务)。难点在于我在图像中创建了一个用户,所以当我用它创建一个容器时,它从我的自定义用户(没有根)开始

因此,如何使用非root用户启动需要root权限的服务(sudo service dnsmasq start)

可能的解决办法:

  • 能够在服务已运行的情况下启动容器。据我所知,在Dockerfile中启动服务是不可能的,因为它不保留状态,只保留FS
  • 以root用户身份启动容器,启动服务,然后切换回用户。这可能行得通,但可能存在安全问题
  • 让我的自定义用户有权自己启动服务。怎么做
  • 不要使用自定义用户(可能是最简单的方法,但是嘿?这其中的乐趣何在?:)

还有其他解决方案吗?

使用entrypoint,运行shell脚本来做您想做的事情怎么样?

多亏了Rickkwa的评论,我能够解决这个问题:

在Dockerfile中(作为根目录):


还记得
/etc/sudoers
文件中的
NOPASSWD
吗?这可以防止系统在启动服务时询问用户密码

这将不起作用,因为我启动容器时使用的用户没有所需的权限。您可以使用类似于CMD sudo[“/usr/startup.sh”]的内容,也可以使用此用户[您的用户]在dockerfile内更改用户。此答案如何:。然后,您可以将其设置为用户无需使用
NOPASSWD
输入密码。
# Install and configure Dnsmasq
RUN apt-get update && apt-get install -y dnsmasq
# Need to add a new line
RUN echo '' >> /etc/dnsmasq.conf
# See https://github.com/nicolasff/docker-cassandra/issues/8
RUN echo 'user=root' >> /etc/dnsmasq.conf
# Add the needed route
RUN echo 'address=/my-domain.com/<my_ip>' >> /etc/dnsmasq.conf

# Allow my user's group to start the service
RUN echo ''%${group}' ALL=NOPASSWD:/usr/sbin/service dnsmasq *' >> /etc/sudoers

# Switch to the right user, that belongs to the group ${group}
USER ${user}
sudo service dnsmasq start