如何以root用户身份运行ENTRYPOINT? [英] How can I run ENTRYPOINT as root user?
问题描述
这是我的dockerfile的一部分:
This is a part of my dockerfile:
COPY ./startup.sh /root/startup.sh
RUN chmod +x /root/startup.sh
ENTRYPOINT ["/root/startup.sh"]
EXPOSE 3306
CMD ["/usr/bin/mysqld_safe"]
USER jenkins
最后我必须切换到USER jenkins,并且必须以jenkins的身份运行容器.
I have to switch in the end to USER jenkins and i have to run the container as jenkins.
我的问题是,当容器启动时,如何以root用户身份运行startup.sh?
My Question is now how can I run the startup.sh as root user when the container starts?
推荐答案
删除Dockefile中的 USER jenkins
行.
Delete the USER jenkins
line in your Dockefile.
在入口点脚本(/root/startup.sh
)末尾更改用户.
Change the user at the end of your entrypoint script (/root/startup.sh
).
通过添加: su-jenkins
man su
示例:
Dockerfile
FROM debian:8
RUN useradd -ms /bin/bash exemple
COPY entrypoint.sh /root/entrypoint.sh
ENTRYPOINT "/root/entrypoint.sh"
entrypoint.sh
#!/bin/bash
echo "I am root" && id
su - exemple
# needed to run parameters CMD
$@
现在您可以运行
$ docker build -t so-test .
$ docker run --rm -it so-test bash
I am root
uid=0(root) gid=0(root) groups=0(root)
exemple@37b01e316a95:~$ id
uid=1000(exemple) gid=1000(exemple) groups=1000(exemple)
这只是一个简单的示例,您也可以使用 su -c
选项在更改用户的情况下运行命令.
It's just a simple example, you can also use the su -c
option to run command with changing user.
这篇关于如何以root用户身份运行ENTRYPOINT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!