I am trying, as part of an exercise, to create an image and run a simple bash script. This is my Dockerfile:
FROM ubuntu
RUN chmod 700 .
#Create container to store file in
RUN mkdir doc-conatiner
# source then the destination of container in docker if I have one
COPY . /functionfibonnaci/doc-conatiner
#when conatiner starts what is the executable
CMD ["bash", "functionfibonnaci.sh"]
when I run docker run:
bash: functionfibonnaci.sh: No such file or directory```
No such file or direcotry
I have been at this for two days and just cant get this to work- so answers will be appreiacted.
CodePudding user response:
From the error message, it is clear that functionfibonnaci.sh
is not found.
Update the CMD command in the Dockerfile to this:
CMD ["bash", "/functionfibonnaci/doc-conatiner/functionfibonnaci.sh"]
Note: This will work if the functionfibonnaci.sh
file is in the same directory where the Dockerfile is present on the host machine. If it is present in a different directory, feel free to update the path of the file in the CMD accordingly.
TL;DR
Let's look closely what you are trying to do. The first two lines of the Dockerfile are self-explainatory. In the third command, you are creating a directory with the intention to copy your script files. Sounds good so far!!!
The fourth line of the Dockerfile is what created a mess IMO. You are actually copying all the files from host to the directory /functionfibonnaci/doc-conatiner
. But wait, you were supposed to copy those file inside the doc-conatiner
directory that you created earlier.. right?
Now in the last line of the Dockerfile, you are trying to run the bash script functionfibonnaci.sh
. But now, since the default WORKDIR is /
by default, it will search for the functionfibonnaci.sh
file inside the /
directory. This file is actually present inside the /functionfibonnaci/doc-conatiner
directory.
Hence, you are facing this issue.
CodePudding user response:
As @KapilKhandelwal indicates in their answer, you're having trouble because the bash functionfibonnaci.sh
command is looking for the script in the current directory, but you've never changed directories, so you're in the container filesystem's root directory.
I'd suggest updating this in a couple of ways:
On your host system, outside of Docker, make sure that the script starts with a "shebang" line; the very first line, starting at the very first character, should be
#!/bin/sh
(or if you have bash-specific extensions and can't remove them,#!/bin/bash
, but try to stick to POSIX shell syntax if you can).On your host system, outside of Docker, make sure the script is executable;
chmod x functionfibonnaci.sh
. With this and the previous step, you'll be able to just run./functionfibonnaci.sh
without explicitly mentioning the shell.In the Dockerfile, change
WORKDIR
to some directory early. Often a short directory name like/app
works well.You don't need to
RUN mkdir
theWORKDIR
directory or directories youCOPY
into; Docker creates them for you.When you
COPY
content into the Dockerfile, the right-hand side can be a relative path like.
, relative to the currentWORKDIR
, so you don't need to repeat the directory name.In your
CMD
you can also specify the script location relative to the current directory.
These updates will get you:
FROM ubuntu
# do not need to mkdir this directory first
WORKDIR /app # or /functionfibonnaci/doc-conatiner if you prefer
# copy the entire build-context directory into the current workdir
COPY . .
# the command does not need to explicitly name the interpreter
# (assuming the script has a "shebang" line and is executable)
CMD ["./functionfibonnaci.sh"]