Docker Community Forums

Share and learn in the Docker community.

How to execute a script as root when startup a container?

docker

(Shrimpy) #1

i try to place my script in /etc/rc.local, but i don`t see script get run.

i am using “buildpack-deps:jessie-curl” as my base image.


(Nathan Le Claire) #2
FROM buildpack-deps:jessie-curl
COPY ./script.sh /etc/rc.local/script.sh
ENTRYPOINT ["/etc/rc.local/script.sh"]

(Shrimpy) #4

you are using ENTRYPOINT, so it doesn`t matter where we place the script. let me try to see if it work.

basicly i don`t want to script to run when building the image, i only want to to run when we start the container.

let me verify to see if it work.


(Shrimpy) #5

it doesn`t work if container is run under “-u” parameter


(Nathan Le Claire) #6

you are using ENTRYPOINT, so it doesn`t matter where we place the script. let me try to see if it work.

It does matter – ENTRYPOINT still follows the lookup rules of PATH etc.

basicly i don`t want to script to run when building the image, i only want to to run when we start the container.

Using COPY + ENTRYPOINT as outlined above should work then. COPY won’t run the script, just place it in the container image.

If you want to run the container as a particular user (-u) you should most likely create that user as part of the build using USER. Then, switch back to root and chmod whichever files you want to have the appropriate permissions for that user (COPY will not add files in as any user other than root; it is a known issue).