简体   繁体   English

你能否以root身份在Docker容器内启动进程,而exec调用的默认用户是非root用户?

[英]Can you start a process inside a Docker container as root, while having the default user of an exec call be non-root?

I'm basically trying to run crond -f as root, while having the default user be something different. 我基本上试图以root身份运行crond -f ,而默认用户则不同。

Since the crontabs it runs use sensitive information from other files on the image, I want to give root access to these files, start the crond process, then switch the user to a newly created one. 由于它运行的crontabs使用来自映像上其他文件的敏感信息,我想对这些文件进行root访问,启动crond进程,然后将用户切换到新创建的文件。 This way the cronjobs will be able to get the information they need, while securing the sensitive files in the container from anyone who may get exec access. 通过这种方式,cronjobs将能够获得所需的信息,同时保护容器中的敏感文件免受任何可能获得exec访问权限的人的攻击。

have tried a couple things like this: 尝试过这样的事情:

USER root

CMD ["./runCrons.sh"]

USER newuser

But this does not run the crond process as root, but as newuser. 但这并不是以root身份运行crond进程,而是以newuser身份运行。

If anyone has a solution it would save me some digging and experimentation. 如果有人有解决方案,它将节省我一些挖掘和实验。

While building the Docker image, create a user which belongs to sudo group and is allowed to run all sudo commands without a password. 在构建Docker镜像时,创建属于sudo组的用户,并允许在没有密码的情况下运行所有​​sudo命令。

Consider the below example which creates a docker image called test with user named myuser with sudo pass: 考虑下面的示例,它创建一个名为test的docker镜像,用户名为myuser with sudo pass:

$ cat Dockerfile

FROM debian:latest
ENV user_name myuser
RUN apt-get update
RUN apt-get install -y sudo
RUN useradd --create-home -s /bin/bash ${user_name}
RUN echo "${user_name} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${user_name}
WORKDIR /home/${user_name}
USER ${user_name}
CMD /bin/bash

Then build the image: 然后构建图像:

docker build -t test . docker build -t test

Now to fix cron permissions issues for standard user, make sure all commands used on cron scripts start with sudo, like below. 现在要修复标准用户的cron权限问题,确保cron脚本上使用的所有命令都以sudo开头,如下所示。

CMD ["sudo ./runCrons.sh"]

Since no password is expected when using sudo, everything should execute fine and you should be good to go. 由于使用sudo时不需要密码,所以一切都应该执行得很好,你应该好好去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以非 root 用户身份在 Docker 容器内运行 tcpdump - Running tcpdump inside a Docker container as non-root user Docker Nginx工作进程以非root用户身份运行,但仍可以访问root拥有的文件 - Docker Nginx worker process runs as non-root user but still can access a file owned by root 将 Docker 镜像中的用户切换为非 root 用户 - Switching users inside Docker image to a non-root user 以非root用户身份启动容器与以root用户身份启动然后降级为非root用户身份 - Starting container as a non-root user vs starting as root and then downgrade to non-root 如何以root身份启动Java程序,但降级为非root用户 - How to start Java program as root but downgrade to non-root user HDFS start-all.sh(由root用户或非root用户使用) - HDFS start-all.sh by root or non-root user 如何以非root用户身份运行Docker容器以及如何与他人共享Docker映像? - How to run docker container as non-root user and how to share the docker image to others? 在 Ubuntu Docker 容器内运行非 root Docker - Running non-root Docker within Ubuntu Docker container 在 Cirrus/Kubernetes 环境中以特定的非 root 用户身份运行 Docker 容器 - Run Docker container in Cirrus/Kubernetes environment as specific non-root user 允许容器的非根用户执行需要功能的二进制文件 - Allow non-root user of container to execute binaries that need capabilities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM