Problem of RUN shell script in Dockerfile in Mac OS X - macos

I'm trying to use Docker with my macOS Catalina v10.15.4
When I tried to RUN ./test.sh in Dockerfile and there is some error occured.
This is my Dockerfile:
FROM ubuntu:18.04
RUN mkdir -p /home/rootfs/src
COPY test.sh /home/rootfs
WORKDIR /home/rootfs
RUN chmod +x test.sh && ./test.sh
When I tried to build this, it did not find the script as below:
$ docker build -t test .
Sending build context to Docker daemon 264.6MB
Step 1/5 : FROM ubuntu:18.04
---> 4e5021d210f6
Step 2/5 : RUN mkdir -p /home/rootfs/src
---> Running in d0813632475d
Removing intermediate container d0813632475d
---> 87a6e284993d
Step 3/5 : COPY test.sh /home/rootfs
---> 26d281d0002c
Step 4/5 : WORKDIR /home/rootfs
---> Running in 4c7e81a514a7
Removing intermediate container 4c7e81a514a7
---> dab2872eb15a
Step 5/5 : RUN chmod +x test.sh && ./test.sh
---> Running in a03f3e5d29b4
/bin/sh: 1: ./test.sh: not found
The command '/bin/sh -c chmod +x test.sh && ./test.sh' returned a non-zero code: 127
I check the info of test.sh file test.sh in macOS.
Got this:
test.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators

Your test.sh script contains Windows line endings as specified in output of file test.sh. Convert them to Unix line endings:
$ dos2unix test.sh
dos2unix: converting file test.sh to Unix format...

Related

What is "delimited by end-of-file (wanted `EOF')"

I am trying to execute following lines-
sh """
#!/bin/bash
cd ${WORKSPACE}
cat << EOF > build.sh
#!/bin/bash
mkdir wsbuildlinux
cd wsbuildlinux
cmake3 ../../wiresharkbuild_linux
make
EOF
chmod 755 build.sh
# run container and build wireshark
./build-env/run-wireshark-env ./build.sh
"""
and here is the content of "build.sh"-
#!/bin/bash
mkdir wsbuildlinux
cd wsbuildlinux
cmake3 ../../wiresharkbuild_linux
make
EOF
chmod 755 build.sh
# run container and build wireshark
./build-env/run-wireshark-env ./build.sh
But I keep getting following error-
/opt/cvsdirs/crdbuilds/jenkins/slave/workspace/Wireshark_Wireshark_trunk#tmp/durable-f510f070/script.sh: line 17: warning: here-document at line 5 delimited by end-of-file (wanted `EOF')
I have no idea what this error means and how to resolve it. Could you please give some advice on this
The EOF token has to be at the beginning of the line.
sh """
#!/bin/bash
cd ${WORKSPACE}
cat << EOF > build.sh
#!/bin/bash
mkdir wsbuildlinux
cd wsbuildlinux
cmake3 ../../wiresharkbuild_linux
make
EOF
chmod 755 build.sh
# run container and build wireshark
./build-env/run-wireshark-env ./build.sh
"""

I can't run shell script

I made a dockerfile that uses the latest alpine and copies test.sh to /src.
I run the container and open up the CLI window in docker. Its path is /src and if I enter command ls I get the test.sh file.
But when I try to run it by either running test.sh or ./test.sh it states /bin/sh: .test.sh: not found.
I added RUN apk add --no-cache --upgrade bash before setting WORKDIR in my Dockerfile.
Dockerfile
FROM alpine:latest
RUN apk add --no-cache --upgrade bash
WORKDIR /src
COPY . .
test.sh
#!/bin/sh
echo "Test"
Content of host directory:
Dockerfile
test.sh
Opened up the docker cli for the container and run:
chmod +x test.sh
Then run ls -l and got -rwxr-xr-x on the file. then tried to run:
/bin/sh: ./test.sh: not found

executing shell command in docker composer gives me error

I'm trying to execute shell command in docker-compose.yml. Code is:
command: bash -c mkdir /opt/wa/usr/install_templates
When I do:
sudo docker-compose up serviceA
it gives me :
serviceA | mkdir: missing operand
When you use bash -c, it runs the first string after the -c flag. See this SO answer for more information. Docker is reading your command bash -c mkdir /path/ and your bash command is just running mkdir in a bash subshell, causing that error.
You don't need to put bash -c before your commands in a docker-compose file. The Docker engine will handle running it in a shell for you and you can simply write the command you want to be run. I'd suggest replacing your command with this:
command: mkdir /opt/wa/usr/install_templates
Alternatively, you could try putting the entire command into a string if you want to force the command to be run in bash:
command: bash -c "mkdir /opt/wa/usr/install_templates"

docker run fails to run shell script (file not found) although the shell script was added successfully with docker build

I am using a private docker hub repository https://hub.docker.com/u/privaterepoexample/, after which I have built my docker image using the commands below:
docker login
docker build -t privaterepoexample/sre:local .
docker tag 85cf9475bc1c privaterepoexample/sre
docker push privaterepoexample/sre
The output of docker build which shows login.sh added to container:
Executing busybox-1.29.3-r10.trigger
OK: 85 MiB in 57 packages
Removing intermediate container 12fd67450dfc
---> e9ca0b9e4ac4
Step 5/7 : WORKDIR /opt
---> Running in ce881ede94aa
Removing intermediate container ce881ede94aa
---> 2335b4f522ac
Step 6/7 : ADD login.sh /opt
---> 2aabf1712153
Step 7/7 : CMD ["chmod 755 login.sh && ./login.sh"]
---> Running in 8ec824d4e561
Removing intermediate container 8ec824d4e561
---> c97a4ad61578
Successfully built c97a4ad61578
Successfully tagged privaterepoexample/sre:local
The Dockerfile below is built successfully and login.sh is added successfully:
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y \
curl
FROM openjdk:8-jre-alpine
RUN apk --no-cache add curl
WORKDIR /opt
ADD login.sh /opt
CMD ["chmod 755 login.sh && ./login.sh"]
Now here comes with my problem, when I execute docker run like below, I get the error:
docker run -i privaterepoexample/sre
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"chmod 755 login.sh && ./login.sh\": stat chmod 755 login.sh && ./login.sh:
no such file or directory": unknown.
but why does it say no such file? given when I go inside the docker container, I can see the login.sh script with the command below:
$ docker run -it
privaterepoexample/sre /bin/sh
/opt # ls
login.sh
/opt # cat login.sh
#!/bin/sh
# Black Box Tester!
content=$(curl --location --request POST
"https://api.platform.abc.com/auth/oauth/token" --header
'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic ' --data-raw 'grant_type=password&
username=event#abc.com&password=fJff'| jq -r
'.domain_id' )
if [ $content = abc ]
then
echo “Valid Login Token”
else
echo “invalid url”
fi
/opt # exit
You get the error no such file or directory because you are using a so-called CMD in exec form in an unexpected way.
You can fix your Dockerfile in several ways, e.g.:
either use a CMD in shell form:
CMD chmod 755 login.sh && ./login.sh
or keep a CMD in exec form (which is often a good idea), but ensure the first argument of the JSON array is a program, not a composite command. You can do this e.g. by running chmod 755 … beforehand, at build time:
ADD login.sh /opt
RUN chmod 755 login.sh
CMD ["./login.sh"]
For more information on the CMD command and its brother command ENTRYPOINT, see also this other SO answer: CMD doesn't run after ENTRYPOINT in Dockerfile

How to set flags for executable in a Dockerfile RUN command?

I am trying to create a Docker image from a dockerfile, wherein I have an executable file. I want to set some flags for the executable, as example: An executable file with a silent flag and a flag for a ressource file. How can I do this in a RUN command in a Dockerfile?
I have so far tried to do:
RUN /bin/bash -c 'PATH/TO/EXECUTABLE -i silent -f ressourceFile.properties',
this just returns an exit code of non-zero 1.
If I try to start the intermediate container from the layer before, and run the command with a docker exec -it container_id /bin/bash -c 'PATH/TO/EXECUTABLE -i silent -f file.properties' .
It gives me
-i: -c: line 0: unexpected EOF while looking for matching `''
-i: -c: line 1: syntax error: unexpected end of file
I have tried to run it as
RUN 'PATH/TO/EXECUTABLE -i silent -f ressourceFile.properties'
with the same result
And I have also tried to convert the RUN command to exec form per Dockers documentation
RUN ["/bin/bash", "-c", "PATH/TO/EXECUTABLE", "-i", "silent", "-f", "file.properties"]
but then it is unable to catch the flags.
Currently my dockerfile looks like this, with redacted executable:
FROM perl
COPY EXECUTABLE.bin ressourceFile.properties /tmp/
RUN chmod +x /tmp/EXECUTABLE.bin
RUN /bin/bash -c '/tmp/EXECUTABLE.bin -i silent -f /tmp/ressourceFile.properties'
# For debug purposes
CMD ["tail", "-f", "/dev/null"]
Expected result should be that the executable runs with the correct flags set and the path to the file.properties file correctly set. Hope you guys can help.

Resources