0

I am learning docker, and referred to online course. After some comfort level, I am now trying to make a dockerized java image, a simple Java app.

I am trying to make the dockerized Java app without having any minimal OS, and hence using FROM scratch in the Dockerfile. The below are the contents:

FROM scratch
ADD FirstJavaApp.class .
RUN yum -y install java
CMD java FirstJavaApp

As I understand dockerized image of any App should have all the dependencies met (and toward this aim, I have added the yum -y install java in the Dockerfile).

Now when I am building the image using this Dockerfile, it is giving me the following error:

sudo docker build -t javaappusingscratch .
Sending build context to Docker daemon  377.8MB
Step 1/4 : FROM scratch
 --->
Step 2/4 : ADD FirstJavaApp.class .
 ---> Using cache
 ---> c624d7dc7c21
Step 3/4 : RUN yum -y install java
 ---> Running in 702829f38ad8
container_linux.go:265: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory"
oci runtime error: container_linux.go:265: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory"

However, if I replace FROM scratch with FROM centos it is working fine. As I understand that scratch don't have any minimal OS and my aim is to build the image which have just the application and its dependencies, and this is what the aim of docker images is.

So is there something which I am missing or not understood well? Can anyone help me understand this?

1 Answer 1

1

You can't use CMD unless you have a shell, and you don't have a shell from scratch. In general, you will find that Java depends on a heap of things that are not scratch. You can find slimmer alternatives than centos (e.g. Alpine) but it won't work from scratch.

Sign up to request clarification or add additional context in comments.

3 Comments

But the error is coming while executing the yum -y install java from logs, hence not able to understand what exactly scratch provide? Can you please expand your reply a bit more.
Cmd launches a shell. You have no shell. You have no yum.
Thanks for your response, I am getting it. However one doubt which I have, does't the host OS provides these missing stuff? Isn't this concept of docker to provide the services to the container, providing the services from host kernel to the container? I may be totally wrong, however felt to share this thought here. If your time permits, please do respond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.