0

I want my bash file to run whenever I the run the docker image, so firstly I created the Dockerfile inside a new directory say demo and in that directory demo I created a new directory home and in that directory I’ve my bash file - testfile.sh. Here is my docker file -

FROM ubuntu
MAINTAINER Aman Kh
COPY . /home
CMD /home/testfile.sh

On building it with command - sudo docker build -t amankh99/hello . the following output was received -

Sending build context to Docker daemon 3.584kB
Step 1/4 : FROM ubuntu
—> 0458a4468cbc
Step 2/4 : MAINTAINER Aman Kh
—> Using cache
—> 98fbe31ed233
Step 3/4 : COPY . /home
—> Using cache
—> 7e52ff3439e2
Step 4/4 : CMD /home/testfile.sh
—> Using cache
—> 1d2660df6387
Successfully built 1d2660df6387
Successfully tagged amankh99/hello:latest

But when I run it with command

sudo docker run --name test -it amankh99/hello

it says

bin/sh: 1: /home/testfile.sh: not found

After having build successfully why it is unable to found the file. I want to convert this container in image and want to push on docker hub, so that when I can run this with simple run command as we run the hello-world(sudo docker run hello-world) , I get my bash file executed, so what changes in dockerfile can I do to acheive this.

7
  • RUN chmod +x /home/testfile.sh after copy Commented Feb 7, 2018 at 16:45
  • @aerokite: If the mode was not executable, it would give: /bin/sh: 1: /home/testfile.sh: Permission denied Commented Feb 7, 2018 at 16:47
  • Looks like the testfile.sh is not in the same directory as your Dockerfile. Try: sudo docker run -it amankh99/hello /bin/bash -c "ls -l /home". Commented Feb 7, 2018 at 16:48
  • yea.. you are right.. Commented Feb 7, 2018 at 16:50
  • Can you check RUN ls -la /home after copy ? Commented Feb 7, 2018 at 16:50

2 Answers 2

2

OP's Description

I created the Dockerfile inside a new directory say demo and in that directory demo I created a new directory home and in that directory I’ve my bash file - testfile.sh.

So according to your description

demo/
+--- Dockerfile
+--- home/
     +--- testfile.sh

You need to COPY home directory into /home

FROM ubuntu
COPY home /home
CMD /home/testfile.sh

If you do this COPY . /home, your home/testfile.sh will be copy into /home/home/testfile.sh

If you want copy only your testfile.sh, then do this

COPY home/testfile.sh /home/
Sign up to request clarification or add additional context in comments.

4 Comments

Read carefully, he has a home directory, then in that directory, he has the script. @marcolz
Right, this is the most likely problem. @aerokite
Thanks for the credit. @aerokite which i did not get for pointing out this.
@abeltre1, I didn't get you
1

It either can find the file and it just does not know what to do with it because the interpreter of your script ( #!.... on the first line of your script) is not present in your docker image, or it cannot find the file because it was not copied.

You can verify this by passing /bin/bash as final argument to your docker run command, and run ls -l /home/testfile.sh and/or /home/testfile.sh on the prompt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.