Search code examples
dockeralpine-linux

How to run a shell script on a Alpine Docker container on startup?


I want to execute a shell script when a Alpine Docker container starts up.


Solution

  • You should create a shell file to be copied inside a new container and then setup the container entrypoint as the shell script you created, synthax would be like

    FROM <docker/alpine-image>
    #.... somecode
    USER root
    COPY /localmachinelocation/entrypoint.sh /containerlocation/entrypoint.sh
    RUN chmod 544 /var/www/webapp/entrypoint.sh
    #.... somecode
    CMD /containerlocation/entrypoint.sh
    

    You could specify the user you need to run the script. Chmod ensure the script would be runnable by the owner of the script, you could change it for security matters.

    Let's say you have your script in /home/user/script.sh, you could do

    FROM <docker/alpine-image>
    USER root
    COPY /home/user/script.sh /root/entrypoint.sh
    RUN chmod 544 /var/www/webapp/entrypoint.sh
    CMD /root/entrypoint.sh