Where am I going wrong with my Dockerfile? - docker

I've created a Dockerfile at the root of a directory that contains a web application. My Dockerfile reads as follows:
FROM openjdk:8u282-jre
MAINTAINER me <me#email.com>
COPY target/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar /
EXPOSE 8080
ENTRYPOINT ["java -jar"]
I'm attempting to copy a jar file from my local machine using the path from the within the root of the project directory - copying this to the root of the container, expose port 8080, and use an entry point thinking that after building the image, the jar will be run as an executable using this entry point. I then built the image as follows:
docker build -t se441/spring-petclinic:standalone .
Giving the build the name se441/spring-petclinic:standalone, I then attempt to run the container:
docker run -i -t se441/spring-petclinic:standalone
And I am getting the following error:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:370:
starting container process caused: exec: "java -jar": executable file not found in $PATH:
unknown.
I edited the Dockerfile to have the entry point be a /bin/bash, did an 'ls' and the jar file is definitely there. While in the container, I can run the jar successfully. Any advice on why the jar file can't be found when building/running with the Dockerfile as I've listed above. would be greatly appreciated. Thank you.

Change:
ENTRYPOINT ["java -jar"]
to:
ENTRYPOINT ["java", "-jar"]
The exec syntax (with the json array) does no do any command line parsing that you get with a shell, so it's looking for the literal binary java -jar rather than java and passing the -jar argument. You need to separate each parameter as another array entry, just as you'd separate them with spaces on the command line.
At that point, you'll find that -jar expects the name of the jar file. You can pass that as an argument when you run the container, e.g.:
docker run -i -t se441/spring-petclinic:standalone spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
Or specify that as well in the entrypoint:
ENTRYPOINT ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]

Related

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 can't find script living in home dir

I have the following Dockerfile:
FROM ubuntu:21.04
COPY keep-alive.sh $HOME/keep-alive.sh
CMD ["$HOME/keep-alive.sh"]
Yes I know its really useless but I am learning.
When I run this:
$ docker run -d --name linux-worker myorg/linux-worker
71cfc9ff7072688d1758f2ac98a8293ed2bcf77bf68f980da20237c9961aca6c
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"$HOME/keep-alive.sh\": stat $HOME/keep-alive.sh: no such file or directory": unknown.
All I want the container to do is start up and run $HOME/keep-alive.sh. Can anyone spot where I'm going awry?
The $HOME environment variable will not be set when running the COPY instruction in your Dockerfile so your script was likely placed at /keep-alive.sh instead of /root/keep-alive.sh where your CMD instruction expects it to be.
Check the logs of your build and you'll likely see that line executed like:
=> [3/3] COPY keep-alive.sh /keep-alive.sh
instead of:
=> [3/3] COPY keep-alive.sh /root/keep-alive.sh
To fix this, you can explicitly set an environment variable to use in this command:
FROM ubuntu:21.04
ENV DIR /root
COPY keep-alive.sh $DIR/keep-alive.sh
CMD ["/bin/bash", "-c", "$DIR/keep-alive.sh"]
Another change that had to be made was to specify that you want the script to be run in a shell so it will expand environment variables. See this answer for more details about this issue.
Alternatively, if you didn't want to use environment variables at all, you could change that line to this if you know the script's path:
CMD [ "/root/keep-alive.sh" ]

File not found exception while running DockerFile

Here my DockerFile :-
FROM openjdk:10
ENV AQUILA_HOME /data/config
#USER root
#VOLUME /tmp
ADD a2i-web-1.0.0-SNAPSHOT.jar app.jar
#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","app.jar"]
My jar is spring boot application which refers configuration file from some directory [/data/config/config.properties]
I am building DockerFile successfully by command
sudo docker build -t dockImgName/a2i-web:v1 .
But while running it by command
sudo docker run -p 8080:8080 -t dockImgName/a2i-web:v1
giving exception as :
Caused by: java.io.FileNotFoundException: /data/config/config.properties (No such file or directory)
I am running this dcoker command from directory containing DockerFile and my jar
Do I need to set any configuration to get config file directory?
The error message is quite clear. When the container tries to run it is not able to find properties file.
You need to add config.properties file to your docker image.
ADD path_to_config_file/config.properties /data/config/config.properties
NOTE: path_to_config_file refers to the file path in your local where you are building the dockerfile

Docker Run error with ENTRYPOINT

I am very new to Docker and I am able to successfully create an Image from Docker file but when I pulled it and ran a docker start on that it threw following error:
Error response from daemon: oci runtime error:
container_linux.go:247: starting container process caused "exec: \"./Myfolder\": stat ./Myfolder: no such file or directory".
The only place that I mentioned /Myfolder is in docker File ENTRYPOINT
as below ENTRYPOINT ["./Myfolder"]
While my Dockerfile looks like following:
FROM microsoft/dotnet:1.1.1-runtime
WORKDIR /opt/outputDirectory
ENTRYPOINT ["./Myfolder"]
COPY output /opt/outputDirectory
and this is assuming my entry point is the ENTRYPOINT project's directory name (the one containing /bin) what could I be missing?
ENTRYPOINT specify the program that runs inside the container. You've put a directory.
See this example:
FROM microsoft/dotnet:1.0-runtime
WORKDIR /app
COPY out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"]

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