如何以 root 用户身份运行 ENTRYPOINT? [英] How can I run ENTRYPOINT as root user?

查看:49
本文介绍了如何以 root 用户身份运行 ENTRYPOINT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 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屋!

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