Can we use RUN instruction with base image as scratch? - docker

FROM scratch
RUN mkdir hello
This is my Dockerfile, and i am unable to build image for this.
Build command is docker build -t sample .
Below is the output
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM scratch
--->
Step 2/2 : RUN mkdir hello
---> Running in faafa9f9aa98
OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown

A scratch image contains nothing, not even /bin/sh or /usr/bin/mkdir.
So you can't RUN anything unless you COPY in into the image first.

Related

How to extend a Docker image that does not have sh binaries

I am trying to extend this image https://hub.docker.com/r/hashicorp/waypoint-odr but I can't figure out how to apply any tasks.
Anything I try to run I get the same error ... starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
here is an example of minimal Dockerfile
FROM hashicorp/waypoint-odr:latest
RUN pwd
output
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM hashicorp/waypoint-odr:latest
---> 60e7c50c52f0
Step 2/2 : RUN pwd
---> Running in 075af9b246f9
failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown
I've tried this as well RUN /bin/ash -c "pwd" but the result is the same.
Do you have any ideas on how I can approach this issue?
It's a bit of an unusual image. The shell is installed in /kaniko/bin, but the Docker SHELL hasen't been set, so it looks for the shell in /bin and fails because it can't find it. You can set the shell and then you should be able to run commands like normal.
FROM hashicorp/waypoint-odr:latest
SHELL ["/kaniko/bin/sh", "-c"]
RUN pwd

tar -xvf failing in dockerfile

I want to create my own Docker image using the following Dockerfile
FROM scratch
COPY apache-cassandra-3.11.6-bin.tar.gz .
RUN tar -xzf apache-cassandra-3.11.6-bin.tar.gz
When I run the command, the tar instruction fails. Why? I am on Windows 10.
C:\Users\manuc\Documents\manu\cassandra_image_test>docker build -f CassandraImageDockerFile.txt -t manucassandra .
Sending build context to Docker daemon 184.8MB
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY apache-cassandra-3.11.6-bin.tar.gz .
---> Using cache
---> deda426d6948
Step 3/3 : RUN tar -xzf apache-cassandra-3.11.6-bin.tar.gz .
---> Running in 3edfb1031c06
OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown
Could it be that because I am not using an existing image, tar is not present in the image and thus tar is failing?
stat /bin/sh: no such file or directory
You started from a scratch image that has nothing. Then you told Docker to RUN a command, but you have no shell.
So you can't run any command, much less tar.
Why not start with alpine?
It looks like, whatever base image "scratch" is, it doesn't have /bin/sh, let alone tar or anything else. I would consider starting from a known OS image, or even the Cassandra official image on DockerHub. https://hub.docker.com/_/cassandra
A Dockerfile that starts FROM scratch starts from a base image that has absolutely nothing at all in it. It is totally empty. There is not a set of base tools or libraries or anything else.

ERRO[0001] error waiting for container: context canceled

Getting error while running docker image. It seems to look like the problem is on my pc.
I'm using MacOS 10.13.6.
I have followed steps to create a docker image.
Sanjeet:server-api sanjeet$ docker build -t apicontainer .
Sending build context to Docker daemon 24.01MB
Step 1/2 : FROM alpine:3.6
---> da579b235e92
Step 2/2 : CMD ["/bin/bash"]
---> Running in f43fa95302d4
Removing intermediate container f43fa95302d4
---> 64d0b47af4df
Successfully built 64d0b47af4df
Successfully tagged apicontainer:latest
Sanjeet:server-api sanjeet$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apicontainer latest 64d0b47af4df 3 minutes ago 4.03MB
alpine 3.6 da579b235e92 2 weeks ago 4.03MB
Sanjeet:server-api sanjeet$ docker run -it apicontainer
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown.
Sanjeet:server-api sanjeet$ ERRO[0001] error waiting for container: context canceled
Inside Dockerfile
FROM alpine:3.6
CMD ["/bin/bash"]
alpine does not include bash by default.
If you want to include bash, you should add RUN apk add --no-cache bash in your Dockerfile.
Issues while running/re-initialize Docker
ERRO[0000] error waiting for the container: context canceled
Steps to Stop the Running Docker Container Forcefully
docker ps
//copy the CONTAINER ID of the running process, ex: CONTAINER ID: 37146513b713
docker kill 37146513b713
docker rm 37146513b713
alpine does not provide glibc. alpine is that small because it uses a stripped down version of libstdc called musl.libc.org.
So we'll check statically linked dependencies using ldd command.
$ docker run -it <image name> /bin/sh
$ cd /go/bin
$ ldd scratch
Check the linked static files, do they exist on that version of alpine? If the do not from, the binaries perspective, it did not find the file-- and will report File not found.
The following step depends on what binaries were missing, you can look it up on the internet on how to install them.
Add RUN apk add --no-cache libc6-compat to your Dockerfile to add libstdc in some Golang alpine image based Dockerfiles.
In you case the solution is to either
disable CGO : use CGO_ENABLED=0 while building
or add
RUN apk add --no-cache libc6-compat
to your Dockerfile
or do not use golang:alpine
I get this error when the network doesn't exist.
docker: Error response from daemon: network xxx not found.
ERRO[0000] error waiting for container: context canceled
It's quite easy to miss the output line after a long docker command and before the red error message.

How do I run a Bash script in an Alpine Docker container?

I have a directory containing only two files, Dockerfile and sayhello.sh:
.
├── Dockerfile
└── sayhello.sh
The Dockerfile reads
FROM alpine
COPY sayhello.sh sayhello.sh
CMD ["sayhello.sh"]
and sayhello.sh contains simply
echo hello
The Dockerfile builds successfully:
kurtpeek#Sophiemaries-MacBook-Pro ~/d/s/trybash> docker build --tag trybash .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM alpine
---> 665ffb03bfae
Step 2/3 : COPY sayhello.sh sayhello.sh
---> Using cache
---> fe41f2497715
Step 3/3 : CMD sayhello.sh
---> Using cache
---> dfcc26c78541
Successfully built dfcc26c78541
However, if I try to run it I get an executable file not found in $PATH error:
kurtpeek#Sophiemaries-MacBook-Pro ~/d/s/trybash> docker run trybash
container_linux.go:247: starting container process caused "exec: \"sayhello.sh\": executable file not found in $PATH"
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"sayhello.sh\": executable file not found in $PATH".
ERRO[0001] error getting events from daemon: net/http: request canceled
What is causing this? I recall running scripts in debian:jessie-based images in a similar manner. So perhaps it is Alpine-specific?
Alpine comes with ash as the default shell instead of bash.
So you can
Have a shebang defining /bin/bash as the first line of your sayhello.sh, so your file sayhello.sh will begin with bin/sh
#!/bin/sh
Install Bash in your Alpine image, as you seem to expect Bash is present, with such a line in your Dockerfile:
RUN apk add --no-cache --upgrade bash
This answer is completely right and works fine.
There is another way. You can run a Bash script in an Alpine-based Docker container.
You need to change CMD like below:
CMD ["sh", "sayhello.sh"]
And this works too.
Remember to grant execution permission for all scripts.
FROM alpine
COPY sayhello.sh /sayhello.sh
RUN chmod +x /sayhello.sh
CMD ["/sayhello.sh"]
By using the CMD, Docker is searching the sayhello.sh file in the PATH, BUT you copied it in / which is not in the PATH.
So use an absolute path to the script you want to execute:
CMD ["/sayhello.sh"]
BTW, as #user2915097 said, be careful that Alpine doesn't have Bash by default in case of your script using it in the shebang.

Docker Image can't execute command

My Dockerfile looks like below:
FROM scratch
RUN skype
I have skype installed in my OS and when I try to build the docker with below command:
sudo docker build -t tryskyped .
It says
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM scratch
--->
Step 2/2 : RUN skype
---> Running in 0ecf7c719567
container_linux.go:247: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory"
oci runtime error: container_linux.go:247: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory"
If I am not wrong it should have executed like /bin/sh -c skype. When I manually try this , I can see the skype opens
I am very new to this and I am just trying out docker.Please help
Your Dockerfile does nothing, as it does not have a CMD or ENTRYPOINT to execute.
A Dockerfile has a configuration phase (the RUN, ADD, COPY and so) and a start phase, with ENTRYPOINT and CMD
As an example, the last line of the Dockerfile for the Nginx image reference contains
extract from
https://github.com/nginxinc/docker-nginx/blob/0c7611139f2ce7c5a6b1febbfd5b436c8c7d2d53/mainline/jessie/Dockerfile
CMD ["nginx", "-g", "daemon off;"]
Check What is the difference between CMD and ENTRYPOINT in a Dockerfile?
The doc for CMD
https://docs.docker.com/engine/reference/builder/#cmd
for ENTRYPOINT
https://docs.docker.com/engine/reference/builder/#entrypoint

Resources