<p>I'm basically trying to run <code>crond -f</code> as root, while having the default user be something different.</p>

<p>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.  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. </p>

<p>have tried a couple things like this:</p>

<pre><code>USER root

CMD ["./runCrons.sh"]

USER newuser
</code></pre>

<p>But this does not run the crond process as root, but as newuser.</p>

<p>If anyone has a solution it would save me some digging and experimentation.</p>

<p>While building the Docker image, create a user which belongs to sudo group and is allowed to run all sudo commands without a password.</p>

<p>Consider the below example which creates a docker image called test with user named myuser with sudo pass: </p>

<pre><code>$ 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" &gt; /etc/sudoers.d/${user_name}
WORKDIR /home/${user_name}
USER ${user_name}
CMD /bin/bash
</code></pre>

<p>Then build the image:</p>

<p><code>docker build -t test</code> .</p>

<p>Now to fix cron permissions issues for standard user, make sure all commands used on cron scripts start with sudo, like below.</p>

<p><code>CMD ["sudo ./runCrons.sh"]</code></p>

<p>Since no password is expected when using sudo, everything should execute fine and you should be good to go.</p>
