Haipeng Su
Haipeng Su

Reputation: 2541

How to set up run bash file when starting run docker image

I would like to run a bash file every time when I start to run the docker image, but couldn't figure how to do this with a few hours already. my bash file looks like,

#!/bin/bash
while true;
do
   echo "hello world"
   sleep 10
done

So what I am thinking about is, when I start running the docker, the bash file will also be running inside the docker image continuously, in this case, the bash file will do its job as long as the docker is on.

How to set this up? should I build this inside the docker image? or I can put bash file in run.sh so it happens when docker runs?

Upvotes: 0

Views: 840

Answers (2)

ItayB
ItayB

Reputation: 11367

Just copy your script file with COPY or ADD in the docker file and then use the CMD command to run it..

For example, if u copy run.sh to / Then in you dockerfile last line just add:

CMD run.sh

for more info please refer to: https://docs.docker.com/engine/reference/builder/ and search for 'CMD'

make sure that the file has the right privileges for running (otherwise, after COPY/ADD the file make RUN chmod +x run.sh

Summary:

//Dockerfile

// source image, for example node.js 
FROM some_image

// copy run.sh script from your local file system into the image (to root dir)
ADD run.sh /

// add execute privillages to the script file (just in case it doesn't have)
RUN chmod +x run.sh

// default behaviour when containter will start
CMD run.sh

hope that's help.

Upvotes: 1

Bukharov Sergey
Bukharov Sergey

Reputation: 10215

Your Dockerfile needs look looke this

FROM <base_image>
ADD yourfile.sh /
CMD ['/yourfile.sh']

Upvotes: 1

Related Questions