java not found error when running docker container [duplicate] - docker

I have simple Dockerfile as below :
FROM centos:centos7
FROM adoptopenjdk/openjdk8:jre8u352-b05-ea-ubuntu-nightly
EXPOSE 8080
WORKDIR /home/centos
COPY ./abc.jar /tmp/abc.jar
CMD ["java" "-jar", "/tmp/abc.jar"]
Now, after building the image , trying to run the container using docker run command , but it is giving error
/bin/sh: 1: [java: not found
I simply don't understand what's the issue here. I'm assuming that , openjdk image will create the java environment and set all PATHS .
Please suggest

Related

java not found error when running docker container

I have simple Dockerfile as below :
FROM centos:centos7
FROM adoptopenjdk/openjdk8:jre8u352-b05-ea-ubuntu-nightly
EXPOSE 8080
WORKDIR /home/centos
COPY ./abc.jar /tmp/abc.jar
CMD ["java" "-jar", "/tmp/abc.jar"]
Now, after building the image , trying to run the container using docker run command , but it is giving error
/bin/sh: 1: [java: not found
I simply don't understand what's the issue here. I'm assuming that , openjdk image will create the java environment and set all PATHS .
Please suggest

Docker unable to run a shell script inside a container using dockerfile

I am facing a highly trivial problem wherein the shell script "httpd-service.sh" is not getting executed in the below Dockerfile.
FROM localhost:5000/httpd_custom_image:v6
WORKDIR /opt
ADD httpd-service.sh .
EXPOSE 80
CMD ["/bin/sh","/opt/httpd-service.sh"]
Below is the content of the httpd-service.sh
#!/bin/bash
/usr/local/apache2/bin/httpd
The script httpd-service.sh is getting copied inside /opt in container and has permissions.
Below is the docker version,
[root#vfecare-3 temp]# docker --version
Docker version 17.03.0-ce, build 3a232c8
Can someone let me know what is breaking here?
Got it working in the foreground with the below lines in the Dockerfile.
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]

Error response from daemon: Dockerfile parse error line 1: unknown instruction: #

Im new to docker and trying to learn it.
Im following this tutorial: https://docs.docker.com/get-started/part2/#apppy
So I installed Docker on Windows.
Created 3 files, app.py, Dockefile and requirements.txt
My docker file looks like this
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
CMD ["python", "app.py"]
When I run it in powershell
docker build -t friendlybuild .
But as result it gives this:
Error response from daemon: Dockerfile parse error line 1: unknown instruction: #
Like it doesnt work
I have no idea why it doesnt work
I forgot to have a whitespace in ENTRYPOINT["java",
It should be ENTRYPOINT ["java",
I just tested the same and by default VSCode seems to save the Dockerfile with UTF-16 LE encoding.
Resaving the file as UTF-8 allowed docker build to run without error.
Solved by removing the dockerfile and creating it with Notepad instead of Visual Code
I had an extra line break in my Dockerfile. Didn't even notice it until I read some of these comments and realized it could be a problem. Originally my file was:
FROM openjdk:8
COPY . /usr/src/Main
WORKDIR /usr/src/Main
ENTRYPOINT ["java", "-Xmx700m","-classpath", ".:./resources/:./lib/*",
"org.spark.docker.demo.DockerMultilayerPerceptronClassifier"]
and the error I was seeing was:
$ docker build -t docker-classifier .
Sending build context to Docker daemon 248.3MB
Error response from daemon: Dockerfile parse error line 5: unknown instruction: "ORG.SPARK.DOCKER.DEMO.DOCKERMULTILAYERPERCEPTRONCLASSIFIER"]
Took me a while to figure it out until I read some of these comments above and looked into the line formatting and realized "org.spark.docker.demo.DockerMultilayerPerceptronClassifier"] was on a line of it's own. Once I removed the line break before it everything worked fine. I assumed the parser would ignore it.
When doing it from Windows, I had to make sure that I have line breaks in my Dockerfile configured for Linux (LF) and not for Windows (CRLF) when editing it from a text editor.
In Google Cloud Platform in console project using nano the comand work for me
1º-nano
2-
# The Dockerfile defines the image's environment
# Import Python runtime and set up working directory
FROM python:2.7-alpine
WORKDIR /app
ADD . /app
# Install any necessary dependencies
RUN pip install -r requirements.txt
# Open port 80 for serving the webpage
EXPOSE 80
# Run app.py when the container launches
CMD ["python", "app.py"]
save the file...
I experienced this issue when working on a React app setup on Docker.
Sending build context to Docker daemon 1.143MB
Error response from daemon: Dockerfile parse error line 1: unknown instruction: +#
Here's how I solved it
The issue was that I had another file named Dockerfile (capital case D) which had some instructions in it, and it was conflicting with the original dockerfile (lower case d) in my project root directory.
I fixed this by deleting the Dockerfile and running the command:
docker build t myapp:latest .
to build the docker image from the dockerfile instead.
That's all.
I hope this helps
While running, appended some text in the start of the file. Removed those using vi in terminal and working fine.
i run docker compose in intellij idea, solved this by remove the number in docker parent folder name.
For mac users who face this issue:
Just edit/create Dockerfile using Python IDLE and remove extension.

Running simple Java Gradle app in Docker

I have a simple Java server app with a Gradle build. It works perfectly with gradle run on my host machine. However, I want to build this in a docker image and run as a docker container.
I'm using docker-machine (version 0.13.0):
docker-machine create --driver virtualbox --virtualbox-memory 6000 default
docker-machine start
eval $(docker-machine env default)
I have the following Dockerfile image build script in ./serverapp/Dockerfile:
FROM gradle:4.3-jdk-alpine
ADD . /code
WORKDIR /code
CMD ["gradle", "--stacktrace", "run"]
I can build perfectly:
➜ docker build -t my-server-app .
Sending build context to Docker daemon 310.3kB
Step 1/4 : FROM gradle:4.3-jdk-alpine
---> b803ec92baec
Step 2/4 : ADD . /code
---> Using cache
---> f458b0be79dc
Step 3/4 : WORKDIR /code
---> Using cache
---> d98d04eda627
Step 4/4 : CMD ["gradle", "--stacktrace", "run"]
---> Using cache
---> 869262257870
Successfully built 869262257870
Successfully tagged my-server-app:latest
When I try to run this image:
➜ docker run --rm my-server-app
FAILURE: Build failed with an exception.
* What went wrong:
Could not create service of type ScriptPluginFactory using BuildScopeServices.createScriptPluginFactory().
> Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.createCrossBuildFileHashCache().
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.internal.service.ServiceCreationException: Could not create service of type ScriptPluginFactory using BuildScopeServices.createScriptPluginFactory().
at org.gradle.internal.service.DefaultServiceRegistry$FactoryMethodService.invokeMethod(DefaultServiceRegistry.java:797)
<snip>
... 60 more
Caused by: org.gradle.api.UncheckedIOException: Failed to create parent directory '/code/.gradle/4.3' when creating directory '/code/.gradle/4.3/fileHashes'
at org.gradle.util.GFileUtils.mkdirs(GFileUtils.java:271)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.open(DefaultPersistentDirectoryStore.java:56)
Why would it have trouble creating that directory?
This should be a very easy task, can anyone tell me how they get this simple scenario working?
FYI, running current versions of everything. I'm using Gradle 4.3.1 on my host, and the official Gradle 4.3 base image from docker hub, I'm using the current version of JDK 8 on my host and the current version of docker, docker-machine, and docker-compose as well.
The fix was to specify --chown=gradle permissions on the /code directory in the Dockerfile. Many Docker images are designed to run as root, the base Gradle image runs as user gradle.
FROM gradle:4.3-jdk-alpine
ADD --chown=gradle . /code
WORKDIR /code
CMD ["gradle", "--stacktrace", "run"]
Ethan Davis suggested using /home/gradle rather than code. That would probably work as well, but I didn't think of that.
The docker image maintainer should have a simple getting started type reference example that shows the recommended way to get basic usage.
Based on the openjdk base image to the gradle image we can see that gradle projects are setup to run in /home/gradle. Check the code out here. gradle run is having trouble running in your new working directory, /code, because the .gradle folder is in the /home/gradle folder. If you copy/add your code into /home/gradle you should be able to run gradle run. This worked for me.

Running .net core docker image, says application started, but on browsing gives "This site can’t be reached" error

I am having Windows 10 Home operating system. I have installed Docker toolbox.
My docker file is as follows:
FROM microsoft/dotnet:1.1-sdk-projectjson
COPY . /app
WORKDIR /app
RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT ["dotnet", "run"]
I have created docker image by using following command:
$ docker build -t test:webApp3 .
I tried running the image by using following commands:
$ docker run -d -p 8080:5000 -t test:webApp3
$ docker run test:webApp3
Command says application started, but on browsing gives "This site can’t be reached" error.
Can someone please tell me, why its giving me specified error?
Plus can someone tell me, where does docker save created images on Windows 10 OS?
I was able to fix this by referring to following link:
https://medium.com/trafi-tech-beat/running-net-core-on-docker-c438889eb5a#.s3krizvtw
I was required to add, .UseUrls("http://*:5000/") in Main() of Program.cs.
Then I was able to browse, by using following URL:
http://192.168.99.100:5000/api/values

Resources