Docker run does not find ENTRYPOINT - bash

I have the following Dockerfile:
FROM ubuntu:19.10
WORKDIR /gen
COPY script.sh ./
RUN chmod +x script.sh && export PATH="/gen/:$PATH"
ENTRYPOINT [ "script.sh" ]
It builds fine, but I cannot execute it. The solution from post is to add the full path to the script (ENTRYPOINT [ "/gen/script.sh" ]), which does not work.
Adding the folder to PATH also does not work. Why is that?
Error: docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"script.sh\": executable file not found in $PATH": unknown.
EDIT: The problem comes from me calling the container with docker run --rm -v some/dir:/gen containerName.

The problem is that you did not modify your PATH variable. Using export in a RUN statement will not be persistent. You need to set environment variables with ENV.
FROM ubuntu:19.10
WORKDIR /gen
COPY script.sh ./
RUN chmod +x script.sh
ENV PATH="/gen:$PATH"
ENTRYPOINT ["script.sh"]
If you run the Docker image while mounting a volume to /gen, you will overwrite the /gen directory and your script will not be found.

Related

Docker Build Failed "chmod: cannot access '/main.sh': No such file or directory"

[this is the error I'm getting after build command ]
Step 7/9 : RUN chmod +x /main.sh
---> Running in 6e880a009c7d
chmod: cannot access '/main.sh': No such file or directory
The command '/bin/sh -c chmod +x /main.sh' returned a non-zero code: 1
and here is my docker file
FROM centos:latest
MAINTAINER Aditya Gupta
#install git
RUN yum -y update
RUN yum -y install git
#make git repo folder, change GIT_LOCATION
RUN mkdir -p /home/centos/doimages/dockimg;cd /home/centos/doimages/dockimg;
RUN git clone https://(username):(password)#gitlab.com/abc/xyz.git (foldername);cd (foldername)/
Run chmod +x ./main.sh
RUN echo " ./main.sh\n "
EXPOSE Portnumber
When you perform a RUN step in a Dockerfile, a temporary container is launched, often with a shell parsing your command. When that command finishes, the container exits, and docker packages the filesystem changes as an image layer. That process is repeated from the beginning for each RUN line.
The key piece there is the shell exits, losing environment variables you've set, background processes you've run, and in this case, the current working directory you tried to set here:
RUN git clone https://(username):(password)#gitlab.com/abc/xyz.git (foldername);cd (foldername)/
Instead of a cd in a RUN command, you can update the value of WORKDIR:
RUN git clone https://(username):(password)#gitlab.com/abc/xyz.git (foldername)
WORKDIR foldername
You want to execute a shell file which does not exist on your docker machine. use ADD command to add your script to your docker image!
-- somewehe inside your dockerfile befor the execution ---
ADD ./PATH/ON/HOST/main.sh /PATH/YOU/LIKE/ON/DOCKER/MACHINE
Then try to build your docker machine
issue is resolved with workdir and cloning manually without docker file and then give the path to mainsh in dockerfile.

Execute script when docker container start

I want container with "centos:latest" image to be started and should execute my script. The scripts are copied with docker cp commands.
docker create --name centos1 centos:latest
docker cp . 5db38b908880:/opt ---> scripts are in current directory, hence .
docker commit centos1 new_centos1 --> now new_centos1 image has scripts
Now I want to start new container with the scripts to be executed: I tried below commands:
docker run -ti --rm --entrypoint "cd /opt && deploy_mediainfo_lambda.sh" new_centos1:latest
docker run -ti --rm new_centos1:latest "cd /opt && deploy_mediainfo_lambda.sh"
Both of above commands failed with:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"cd /opt && deploy_mediainfo_lambda.sh\": stat cd /opt && deploy_mediainfo_lambda.sh: no such file or directory": unknown.
ERRO[0000] error waiting for container: context canceled
if used bash command while starting container, I can run my script using 'execuateble path'/'execuatble name' inside container, but I can not do this while starting container on commandline
docker run -ti --rm new_centos1:latest bash
[root#c34207f3f1c4 /]# ./opt/deploy_mediainfo_lambda.sh
If used below command, which calls executable directly, it gives path error.
docker run -ti --rm new_centos1:latest "deploy_mediainfo_lambda.sh"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"deploy_mediainfo_lambda.sh\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled
Also not sure about setting $PATH from commandline while starting the container.
I know, using Dockerfile this is achievable, like:
can set path using ENV,
can copy executables with ADD or COPY
run executables using CMD or ENTRYPOINT
How to achieves it using docker commandline?
Thanks melpomene.
Here is my bash script to automate script execution inside container, after copying them, all using docker commands.
# Start docker container
docker create --name mediainfo_docker centos:latest
# copy script files
docker cp . mediainfo_docker:/opt
# save container with the new image, which contains all scripts.
docker commit mediainfo_docker mediainfo_docker_with_scripts
# Now run scripts inside docker container
docker run -ti --rm mediainfo_docker_with_scripts:latest /opt/deploy_mediainfo_lambda.sh
Since deploy_mediainfo_lambda.sh is a script, first line of it is:
#!/bin/bash

Can't start bash script on ubuntu docker container

it's been a few days now but I really can't understand how to run a bash script correctly in ubuntu/xenial64 using docker. Any clarifications will be very appreciated.
I created a Dockerfile like this
FROM ubuntu:16.04
COPY setup.sh /setup.sh
RUN chmod +x /setup.sh
ENTRYPOINT [ "/setup.sh" ]
The error returned is: standard_init_linux.go:195: exec user process caused "no such file or directory"
But why?? If I run ls the file is correctly placed on the root. I also tried using CMD ["/setup.sh"]. My script file has a shebang like this #!/bin/bash.

How to fix "sh: 0: Can't open start.sh" in docker file?

I have created a docker image which contains the following CMD:
CMD ["sh", "start.sh"]
When I run the docker image I use the following command inside a Makefile
docker run --rm -v ${PWD}:/selenium $(DOCKER_IMAGE)
which copies the files from the current (host-)directory to the docker's /selenium folder. The files include files for selenium tests, as well as the file start.sh. But after the container has started, I get immediately the error
"sh: 0: Can't open start.sh"
Maybe the host volume is mounted inside docker after the command has been run? Anything else that can explain this error, and how to fix it?
Maybe there is a way to run more than one command inside docker to see whats going on? Like
CMD ["ls", ";", "pwd", ";", "sh", "start.sh"]
Update
when I use the following command i the Dockerfile
CMD ["ls"]
I get the error
ls: cannot open directory '.': Permission denied
Extra information
Docker version 1.12.6
Entrypoint: WORKDIR /work
Your mounting your volume to the /selenium folder in your container. Therefor the start.sh file isn't going to be in your working directory its going to be in /selenium. You want to mount your volume to a selenium folder inside your working directory then make sure the command references this new path.
If you use docker-compose the YAML-file to run the container would look something like this:
version: '3'
services:
start:
image: ${DOCKER_IMAGE}
command: sh selenium/start.sh
volumes:
- .:/work/selenium
If you try and perform each step manually, using docker run with bash,
docker exec -it (container name) /bin/bash
It will be more easier and quicker to look at the errors, and you can change the permissions, view where the file is located, before running the .sh file and try again.
Check the permission using ls -l.
Give the permission 777 using sudo chmod 777 file_name.
Repeat for other files you might find.

Dockerfile: code 127 when trying to RUN shell-script

Running Docker Toolbox on Windows 10 host.
There is a Dockerfile:
FROM 16.04
...
RUN if [ some_condition ]; then ./foo.sh; fi
...
There is a foo.sh:
#!/bin/bash
...
echo 'Me working'
Now when trying to build the Docker image:
docker build -t name_of_the_image .
Getting error:
Step 7/12 : RUN ./foo.sh
---> Running in e7e0703d3f8f
/bin/sh: 1: ./foo.sh: not found
The command '/bin/sh -c ./foo.sh' returned a non-zero code: 127
I would assume error 127 would be the Docker doesn't see the bash. Any suggestion how to fix this?
Edit: already copying all files into the Docker, Dockerfile:
FROM ubuntu:16.04
MAINTAINER Mr Anderson "mr#anderson.com"
# set workdir
COPY . /app
WORKDIR /app
# Run scripts
RUN ./foo.sh
You'll need to copy/COPY the file into the container before you can execute/RUN the script.
Also since you're using a relative path when you call the script be sure to set a WORKDIR.
COPY ./foo.sh /app/foo.sh
WORKDIR /dir
RUN chmod +x /app/foo.sh
RUN if [ some_condition ]; then ./foo.sh; fi
Also make sure the script is executable.
After some further investigation:
Using CMD over RUN is not a perfect solution because of the way those commands work. RUN can be used any amount of times, to build Docker image layer by layer, while CMD can be executed only once when the image has been build.
In my case the solution was to:
Open ./foo.sh file with VIM and run: :set fileformat=unix and save the file.
Long story short: the line ending in the shell-script were incorrect and had to be converted to the Unix ones.

Resources