Cannot start docker container from scratch - docker

Nowadays I am trying to learn about the Docker but still have some ideas not clear;
I have tried to run an image that I just created, here is the directory;
hello (directory) :
Dockerfile
start.sh
And the Dockerfile :
FROM scratch
ADD start.sh /var/
CMD ["/var/start.sh"]
start.sh :
#!/bin/bash
echo "hello world"
I have tagged as using :
docker build -t mozer/hello .
Once I run the command;
docker run mozer/hello
no such file or directory
Error response from daemon: Cannot start container f22019aabc81f29fe17e849a2c040902ccadefe6cb8a8fe2612c83fe8eda40ea: [8] System error: no such file or directory
and once I run the command:
docker run mozer/hello /bin/sh -c
exec: "/bin/sh": stat /bin/sh: no such file or directory
Error response from daemon: Cannot start container 3b54584092e70b639671aca66122a0b1f6b1e4327cb2471a8792c3b2337b0bcc: [8] System error: exec: "/bin/sh": stat /bin/sh: no such file or directory
Can you please give me some ideas to find the solution ?
P.S. : I am working on a machine which is not connected to internet !

FROM scratch is a completely empty filesystem. You have no installed libraries, and no shell (like /bin/sh) included in there. To use this as your base, you'd need a statically linked binary, or you'll need to install all of the normal tools that are included with a linux distribution.
The latter is what is prepackaged in the various busybox, debian, ubuntu, centos, etc images on the docker hub. The fast way to make your image work with a minimal base image is to change the from to FROM busybox and change your /bin/bash to /bin/sh.

Scratch image is an empty file system, there is not /bin/sh inside it.
Look at Creating a simple base image using scratch
You can use Docker’s reserved, minimal image, scratch, as a starting
point for building containers. Using the scratch “image” signals to
the build process that you want the next command in the Dockerfile to
be the first filesystem layer in your image.
You can add an executable compiled program and run it.

Related

standard_init_linux.go:219: exec user process caused: no such file or directory on Raspbian

Problem: I am attempting to create and run a Docker container of an Edge Service on a Raspberry Pi using 64 bit Raspbian. Due to other limitations, I am attempting to containerize the program (in Golang) by containerizing the build executable file, rather than building the .go in the Dockerfile.
My Dockerfile looks like this
FROM golang:alpine
WORKDIR /build
ADD server .
EXPOSE 50051
CMD ["./server"]
Running the built executable on it's own works correctly, and when creating the Docker Image using the command "sudo docker build -t server:v7 .", the Docker Daemon gives no errors, with the finished image being displayed on the list of Docker Images as if it were correctly working. However, when I try to run the Image, I receive the error "standard_init_linux.go:219: exec user process caused: no such file or directory", rather than the file running.
Some additional notes as per the comments: All of the programs were written in Ubuntu, they were built to an executable on Raspberry Pi, and the Dockerfile was also written on the the Raspberry Pi. When using the command "sudo docker run -it -d : sh", the image will run, and when using the command "sudo docker exec -it sh", terminal will enter the shell. Attempts to run the program from within the shell similarly fail, yielding the error "sh: error not found"
ldd yields the following results:
/lib/ld-linux-aarch64.so.1 (0x7f8731f000)
libpthread.so.0 => /lib/ld-linux-aarch64.so.1 (0x7f8731f000)
libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7f8731f000)
And there seems to be no problem with Alpine or with $PATH
Do you build on Raspberry or a different machine? If you build on PC, you need to set the build target architecture to match the Raspberry.
The image "golang:alpine" is available for Raspberry, so this should not be the problem.
The error "user process caused: no such file or directory" is then probably related to the file you are trying to execute.
Make sure the file exists in the directory
Make sure the file is executable
Maybe change the CMD to "ls -la" to see in the docker logs if the file is there and which attributes it has.

Docker unable to find file in Alpine Linux container

I have an application I'm creating a docker image for, the image is created but when I run it the container errors saying it cannot find the executable file.
Here is my docker file:
FROM alpine:3.14
WORKDIR /
EXPOSE 3000
COPY ./weather-api /weather-api
CMD ["/weather-api"]
I've included a RUN ls -la immediately above the CMD which shows the file is there whilst building the image is being built, and I've included a shell script with la -la as the CMD which has shows that the files is there when the image is being ran.
This is the error I'm getting:
standard_init_linux.go:228: exec user process caused: no such file or directory
When I try to call the weather-api from a shell script I get the standard file not found error.
I've tried using an absolute and relative path for the command, copying the file to /usr/local/bin which I think should put it on the PATH so it would be callable from anywhere, changing COPY to ADD.
I'm still new to docker so any help would be greatly appreciated.

Docker build error :: exec: \"/bin/sh\": stat /bin/sh: no such file or directory [duplicate]

This question already has an answer here:
Starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown
(1 answer)
Closed 1 year ago.
I am very new to Docker. I figured out on internet on how to build a dockerfile and then create an image.
This is how my docker file looks like:
FROM scratch
ADD abc.py .
RUN pip3 install requests json HTTPBasicAuth
CMD [ "python3", "./abc.py"]
I am using scratch (base image) as I am on a Linux server where we are not allowed to connect to the outside world.
The thing is I am getting the below error when i try to run Docker build command i.e.
docker build -t testimage .
OCI runtime create failed: container_linux.go:345: starting container process caused "exec: "/bin/sh": stat /bin/sh: no such file or directory": unknown
Can anyone explain why is this and what would be the solution. I got some idea about the problem but couldn't identify the solution.
The scratch base is only meant to be used when creating other base images. It contains nothing, not even a shell like bash. If you want to run a python script in a Docker image, I would suggest using a Python base image. For example, you could use python:3.8-slim, which is based on Debian Linux.
FROM python:3.8-slim
COPY abc.py .
RUN pip3 install --no-cache-dir requests
CMD ["python3", "./abc.py"]
I made some changes to your Dockerfile.
Changed ADD to COPY, because the docker documentation says to prefer COPY.
Added --no-cache-dir to pip install to prevent downloads from being cached. This reduces the size of the Docker image.
Removed some packages from pip install because I don't think they exist...
If you cannot pull this base image on your server, then you can try building the image elsewhere and pushing it to your server. If you can build the image somewhere else, then you can use docker export to export it to a tar file. Then you can upload it to your server, and use docker import to convert it to a Docker image.

starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory":

I am trying to build an docker image on centos 7 from SCRATCH. I have performed following steps :
FROM scratch
RUN rpm -ivh https://address/app.rpm
RUN YUM install tools
...
...
CMD ["rpm","start"]
After executing this , I tried to build this dockerfile with command
"docker build -t testsid -f ./dockerfile ."
Now I see following error :
Sending build context to Docker daemon 3.072kB
Step 1/16 : FROM scratch
--->
Step 2/16 : RUN rpm -ivh https://address/app.rpm
---> Running in d25a0a879d9e
OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown
Please let me know whether anyone has suggestion regarding this. ?
Any input will be really helpful.
Thank you.
FROM scratch starts from a totally empty image. The only things that will be in the container filesystem at all are files in /dev, /proc, and /etc that Docker automatically provides. When you say rpm, that command doesn't exist. A string-form RUN command gets wrapped in /bin/sh -c ..., but there is no /bin directory.
FROM scratch is a somewhat advanced use of Docker. If you're very comfortable with concepts like statically-linked binaries and the differences between bare-string and JSON-array CMD, you can use it to make an extremely small image. For most typical uses you'll want to at least start from something with some sort of distribution and package manager.
If you need Red Hat's package management tools, the easiest Docker Hub image to start from will be centos:
FROM centos:8 # includes rpm, yum, /bin directory
RUN rpm -ivh https://address/app.rpm
RUN yum install tools
...

Docker create base image

I am trying to create a docker base image from scratch. The Dockerfile is simple:
FROM scratch
COPY data_folder /opt/data_folder/
CMD ["/opt/data_folder/"]
However, at the final Dockerfile instruction when the default command is run the process exits and says that "oci runtime error: exec: "/opt/data_folder/": permission denied".
Why is this happening since the docker docs specify that this is how you create a base image.
Note: I am using docker on windows native.
Note: The Dockerfile is run from docker-compose up.
You are trying to "execute" a folder /opt/data_folder/
The docker doc suggest executing a command (the executable hello)
FROM scratch
ADD hello /
CMD ["/hello"]

Resources