I am building a Docker container using the following Dockerfile and actually the app is running on the created container.
FROM eclipse-temurin:17-jdk-jammy as builder
RUN addgroup demogroup; adduser --ingroup demogroup --disabled-password demo
USER demo
WORKDIR /app
# copy pom.xml, mvnw and source code
COPY .mvn/ .mvn
COPY mvnw ./
COPY pom.xml ./
COPY src/ src
#RUN dos2unix ./mvnw
RUN ./mvnw clean install <-- this line gives error
# Second stage: minimal runtime environment
FROM eclipse-temurin:17-jre-jammy
WORKDIR /app
# copy jar from the first stage
COPY --from=builder /app/target/*.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
When executing RUN ./mvnw clean install line, I get " /bin/sh: 1: ./mvnw: not found" error. I tried many things, but cannot fix it. Is there any problem in my Dockerfile?
RUN mvn
runs mvn inside the docker image.
The docker image is basically a pared down Linux image. Or a Windows image, or whichever OS was used as the base image.
Presuming your base image is Linux, after docker build completed, at your project folder, you can do
docker run -it
and you will be in your dockerized Linux image, where you can run basic Linux commands (provided your image has those basic commands installed).
You can then navigate and inspect if your docker image.
To solve the problem, include Maven build in your Dockerfile instead of running maven command from project folder via mvnv:
COPY src/main/resources/data/ ./src/data
Related
I need to replicate the build in our CI/CD tool in the dev workstations of the developers by making Intellij IDEA build within a docker container instead of the current system. Is this doable? I find similar threads with launching the app inside (that i don't need), i need it only as a build environment. I already have the Docker plugin installed but i fail to see how to make it as a build environment.
I have installed the docker plugin, prepared the image. I have docker installed on the workstation.
You are expecting something like this?
FROM eclipse-temurin:17-jdk-alpine as build
WORKDIR /workspace/app
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
COPY src src
RUN ./mvnw package -DskipTests
FROM eclipse-temurin:17-jdk-alpine
ARG JAR_FILE=target/*.jar
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/target/dependency
COPY --from=build /workspace/app/${JAR_FILE} /app/
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar ${0} ${#}"]
You can use the maven container as a build container to save maven installation time in case it takes.
I am building a Docker container using the following Dockerfile and actually the app is running on the created container.
FROM eclipse-temurin:17-jdk-jammy as builder
RUN addgroup demogroup; adduser --ingroup demogroup --disabled-password demo
USER demo
WORKDIR /app
# copy pom.xml, mvnw and source code
COPY .mvn/ .mvn
COPY mvnw ./
COPY pom.xml ./
COPY src/ src
#RUN dos2unix ./mvnw
RUN ./mvnw clean install <-- this line gives error
# Second stage: minimal runtime environment
FROM eclipse-temurin:17-jre-jammy
WORKDIR /app
# copy jar from the first stage
COPY --from=builder /app/target/*.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
When executing RUN ./mvnw clean install line, I get " /bin/sh: 1: ./mvnw: not found" error. I tried many things, but cannot fix it. Is there any problem in my Dockerfile?
RUN mvn
runs mvn inside the docker image.
The docker image is basically a pared down Linux image. Or a Windows image, or whichever OS was used as the base image.
Presuming your base image is Linux, after docker build completed, at your project folder, you can do
docker run -it
and you will be in your dockerized Linux image, where you can run basic Linux commands (provided your image has those basic commands installed).
You can then navigate and inspect if your docker image.
To solve the problem, include Maven build in your Dockerfile instead of running maven command from project folder via mvnv:
COPY src/main/resources/data/ ./src/data
I am new to java and maven. I have built an application that executes a flink job. I have created a base docker image but I am not sure how to excecute/run like I run the application in the terminal.
I currently run the application in the terminal as follows:
mvn package exec:java `-D exec.args="--runner=FlinkRunner --flinkMaster=localhost:8081 --filesToStage=.\target\maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar `" -P flink-runner`
Here is my docker file
FROM maven:latest AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package
FROM openjdk:14
COPY --from=build /usr/src/app/target/maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar /usr/app/maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar
WORKDIR /usr/app
EXPOSE 8080
ENTRYPOINT ["java","-jar","maven_benchmark-1.0-SNAPSHOT-jar-with-dependencies.jar"]
Any suggestions?
Thanks in advance!
You are running your app with a maven plugin and with a maven profile. You need your app to be runnable outside of maven first.
Then, you need to cleanup your docker steps a bit, here are some suggestions:
Move copy src after coyping pom and downloading dependencies
Do not use root user for runnable image
Use slimer base image for runnable image
Use exploded jars instead of fat jars to get slimmer layers
Make use of .dockerignore to prevent copying unwanted things to the image
Here is a sample Dockerfile:
FROM maven:3.6.3-openjdk-14-slim AS build
WORKDIR /build
# copy just pom.xml (dependencies and dowload them all for offline access later - cache layer)
COPY pom.xml .
RUN mvn dependency:go-offline -B
# copy source files and compile them (.dockerignore should handle what to copy)
COPY . .
RUN mvn package
# Explode fat runnable JARS
ARG DEPENDENCY=/build/target/dependency
RUN mkdir -p ${DEPENDENCY} && (cd ${DEPENDENCY}; jar -xf ../*.jar)
# Runnable image
FROM openjdk:14-alpine as runnable
VOLUME /tmp
VOLUME /logs
ARG DEPENDENCY=/build/target/dependency
# Create User&Group to not run docker images with root user
RUN addgroup -S awesome && adduser -S awesome -G awesome
USER awesome
# Copy libraries & meta-info & classes
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
# Run application
ENTRYPOINT ["java","-cp","app:app/lib/*","com.myawesomeness.Application"]
Then your app, must be runnable outside of maven first.
I am getting this error when I am trying to copy the generated jar file from the target folder to /usr/share/ folder in the Docker image. I have scoured Docker forum sites and people are having the exact same issue but there are no clear answer that solves this problem.
Step 9/10 : COPY target/${JAR_FILE} /usr/share/${JAR_FILE}
ERROR: Service 'myservice' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder558103764/target/myservice.jar: no such file or directory
Here's my Dockerfile:
----------------------- begin ---------
FROM adoptopenjdk/openjdk11:alpine
MAINTAINER XXX XXX <ramil.xxxxx#xxxx.ai>
# Add the service itself
ARG JAR_FILE="myservice-1.0.0.jar"
RUN apk add maven
WORKDIR /app
COPY . /app/
RUN mvn -f /app/pom.xml clean install -DskipTests
WORKDIR /app
COPY target/${JAR_FILE} /usr/share/${JAR_FILE}
ENTRYPOINT ["java", "-jar", "/usr/share/myservice-1.0.0.jar"]
------ end snip -------
Here's how I run this from the base folder where I have my Dockerfile on my Mac.
docker build -t service-image .
So I guess you are trying to move your src to image and run a mvn build and copy built file from target to share folder.
If so, every thing seems to be fine except this line
COPY target/${JAR_FILE} /usr/share/${JAR_FILE}
COPY takes in a src and destination. It only lets you copy in a local
file or directory from your host (the machine building the Docker
image) into the Docker image itself
I think your intention is to copy file inside your container's /target to /usr/share folder. try this
RUN cp target/${JAR_FILE} /usr/share/${JAR_FILE}
Regrading error which you see its because with COPY command Docker will try to get the file from docker default path in your HOST
i.e /var/lib/docker/tmp/docker-builder558103764/
where /target folder doesn't exist
I had similar issue because of my .dockerignore had following exclusion:
target
Possible solutions are:
do not ignore target
define ignore exception
!target/*.jar
What has also worked after scouring the Docker forums is using multi-stage build. The multi-stage build not only forces you to use the folders where you specify the WORKDIR in your build stage but also significantly reduces the size of your base images up to 5x-6x less than just using the one stage build like I have shown above. Below is what my solution to finding this jar in the folder I specified as my WORKDIR and use that jar file for my smaller base image. Single-stage build in my initial solution produces 560 MB image. This multi-stage Dockerfile below builds image that is only 108 MB.
FROM adoptopenjdk/openjdk11:alpine as compile
MAINTAINER XXXX <ramil.xxxx#xxxx.ai>
# Build the jar using maven
RUN apk add maven
WORKDIR /app
COPY . /app/
RUN mvn -f pom.xml clean package -DskipTests
FROM adoptopenjdk/openjdk11:alpine-jre
# Copy the packaged jar app file to a smaller JRE base image
COPY --from=compile "/app/target/service-1.0.0.jar" /usr/share/
ENTRYPOINT ["java", "-jar", "/usr/share/service-1.0.0.jar"]
It turns out that the Dockerfile I was using wanted to be executed in a folder different from where the Dockerfile was located.
So given a build_config_folder/Dockerfile...
So instead of
docker build --build-arg SOME_ARG=value build_config_folder
# or
cd build_config_folder
docker build --build-arg SOME_ARG=value .
It needed to be
docker build --build-arg SOME_ARG=value -f build_config_folder/Dockerfile .
This is because the final path positional argument determines where the COPY command will look for files and folders.
Hope that helps someone.
I'm building a docker image out of a micronaut application.
But whenever I run the docker build command
docker build -f Dockerfile -t micronaut .
I get this error
/bin/sh: /bin/gu: No such file or directory
Here's the content of my docker file anyway:
FROM oracle/graalvm-ce:20.0.0-java11 as graalvm
RUN $GRAALVM_HOME/bin/gu install native-image
COPY . /home/app/micronautguide
WORKDIR /home/app/micronautguide
RUN $GRAALVM_HOME/bin/native-image --no-server -cp build/libs/complete-*-all.jar
FROM frolvlad/alpine-glibc
RUN apk update && apk add libstdc++
EXPOSE 8080
COPY --from=graalvm /home/app/micronautguide/micronautguide /micronautguide/micronautguide
ENTRYPOINT ["/micronautguide/micronautguide", "-Xmx68m"]
Though by running gu command from the terminal works.
Your context does not contain RUN $GRAALVM_HOME/bin/gu . What is $GRAAL_VM_HOME? Whatever this variable is, it is set as null. Also, $GRAALVM_HOME/bin/gu needs to be part of docker image.
$GRAALVM_HOME does not exists in the oracle/graalvm-ce:20.0.0-java11 container. Thus, make suer to specify the right path. In this case it is /bin/gu
I think $GRAALVM_HOME is a variable you use in the local machine and gu tool is already installed and available at /bin in the docker image. So I have kept the $GRAALVM_HOME unchanged in the second build stage.
So, the Dockerfile should be like below,
FROM oracle/graalvm-ce:20.0.0-java11 as graalvm
RUN /bin/gu install native-image
COPY . /home/app/micronautguide
WORKDIR /home/app/micronautguide
RUN /bin/native-image --no-server -cp build/libs/complete-*-all.jar
FROM frolvlad/alpine-glibc
RUN apk update && apk add libstdc++
EXPOSE 8080
COPY --from=graalvm /home/app/micronautguide/micronautguide /micronautguide/micronautguide
ENTRYPOINT ["/micronautguide/micronautguide", "-Xmx68m"]