Multistage dockerfile COPY failed - spring-boot

Dockerfile:
FROM azul/zulu-openjdk-alpine:11 as builder
COPY server-1.0.0.jar application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM azul/zulu-openjdk-alpine:11
COPY –-from=builder dependencies/ ./
COPY –-from=builder spring-boot-loader/ ./
COPY –-from=builder snapshot-dependencies/ ./
COPY –-from=builder application/ ./
...
Results in:
Step 5/14 : COPY –-from=builder dependencies/ ./
COPY failed: file not found in build context or excluded by .dockerignore: stat –-from=builder: file does not exist
Note that running java -Djarmode=layertools -jar server-1.0.0.jar extract creates the dependencies folder.
Tutorial source

I've copied the docker code from another site and it seems it contained an invalid dash unicode char: \u2013
So instead of COPY –-from it should be COPY --from

Related

Dockerfile copy file without version

In my project, after the build, there are two files
build/libs
backend-api-0.0.1-SNAPSHOT.jar
backend-api-0.0.1-SNAPSHOT-plain.jar
How can I get rid of the jar version designation when building a docker image
COPY --from=builder /home/source/build/libs/backend-api-0.0.1-SNAPSHOT.jar /home/api/
You can rename the file while copying.
COPY --from=builder \
/home/source/build/libs/backend-api-*-SNAPSHOT.jar \
/home/api/backend.jar
Dockers COPY instruction is using Go’s filepath.Match rules.

Creating Docker image with springboot layertools - does not find profile specific application properties

I am following the tutorial here and here on how to create a layered Docker image from my springboot backend. I end up with the following Dockerfile:
FROM openjdk:8-jre-slim as builder
WORKDIR application
ARG JAR_FILE=target/*-exec.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM openjdk:8-jre-slim
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
The problem is that when I run this in my docker-compose with a Spring profile TST, that it does not find application-tst.properties. I can see from the logs that Spring profile TST is active on startup, yet it only loads the properties from application.properties.
As a sanity check I copied the properties from application-tst.properties over to application.properties and rebuilt my image, which then worked fine (it connects to the database container etc).
I extracted the contents of my executable jar (which is the jar from line 3 in the Dockerfile) and can confirm that application-tst.properties is present there. Anyone know what the issue is?
I found the answer and it's a really dumb one... The Spring profile is case sensitive, so
SPRING_PROFILES_ACTIVE=TST
should become
SPRING_PROFILES_ACTIVE=tst

Docker multi-stage build not running all stages

I am trying to build an image from this Dockerfile:
FROM openjdk:14-slim-buster AS builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM openjdk:14-slim-buster
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/resources/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
The problem is it never reaches the second stage. I can see the total steps in the counter when the image is being built, but it just stops and executes this as a new container which is running the actual application
RUN java -Djarmode=layertools -jar application.jar extract
To answer my own question, it was happening because of the Spring Boot version which was not ready to handle multistage builds, but after upgrading the service to 2.3.x i can build.
I think it is because of the Jar file not in supported form.
That's why jarmode can't process it.
Jarmode is a special system used to extracting Layered Jars.
You can check out: https://spring.io/blog/2020/01/27/creating-docker-images-with-spring-boot-2-3-0-m1 for detail info.

Packaging war file into docker image in multiple layers

I'm trying to put a Spring Boot application with war packaging into a docker image. The simplest way to get this can be using the following Dockerfile:
FROM adoptopenjdk/openjdk8:alpine-slim
VOLUME /tmp
COPY target/demo.war app.war
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.war"]
Following this approach I'm creating a big layer every time I change the project's source code. An alternative to this is to split the war into different docker layers as it is explained in this post: https://spring.io/blog/2018/11/08/spring-boot-in-a-container#a-better-dockerfile
However, I'm afraid this is only possible with jar packaging and not with war. Am I right?
Thanks in advance.
This is the way I get a multilayer docker image with a war applicaton.
First open the war file to find out which folders have to be copied to the image:
jar -xf youapp.war
In my project the war file is composed of these folders:
/WEB-INF
/META-INF
/resources
/org
Based on this, I created the following Dockerfile:
FROM openjdk:8-jdk-alpine AS builder
WORKDIR target/dependency
ARG APPWAR=target/*.war
COPY ${APPWAR} app.war
RUN jar -xf ./app.war
RUN mv WEB-INF/lib-provided lib-provided
RUN mv WEB-INF/lib lib
FROM openjdk:8-jre-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY --from=builder ${DEPENDENCY}/lib /app/WEB-INF/lib
COPY --from=builder ${DEPENDENCY}/lib-provided /app/WEB-INF/lib-provided
COPY --from=builder ${DEPENDENCY}/org /app/org
COPY --from=builder ${DEPENDENCY}/resources /app/resources
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=builder ${DEPENDENCY}/WEB-INF /app/WEB-INF
ENTRYPOINT ["java","-cp","/app/WEB-INF/classes:/app/WEB-INF/lib/*:/app/WEB-INF/lib-provided/*","com.company.project.Application"]
I hope it is useful.
Try using the Tomcat Docker image to deploy your warfile. For example:
FROM tomcat:latest
COPY target/demo.war /usr/local/tomcat/webapps/
Reference:
https://hub.docker.com/_/tomcat

Docker copy doesn't seem to work with stopped container and wildcard

I'm building a maven project inside a docker container:
docker run build-image "mvn clean package -f ./pom.xml"
The source and workdir in the Dockerfile for "build-image" are located at /src/ (having two subfolders with each containing one submodule with pom.xml, parent pom at /src/pom.xml)
When the build is finished, the container exits. Then, I want to extract the build artifacts:
docker cp <container-id>:/src/module1/target/*.war ./
I get an error:
Error response from daemon: Could not find the file /src/<module1>/target/*.war in container silly_nobel
When I specify the file without using the wildcard:
docker cp <container-id>:/src/<module1>/target/<module1>##0.1.0-SNAPSHOT.war ./
it succeeds..
Is this intended behaviour or a bug? I googled a bit but could not find a similar post with that problem. docker version is 18.02.0-ce, build fc4de44
Best regards
docker cp does not support wildcards at the moment (github discussion)
But you can get what you want by using multi stage builds. In multi stage builds you can use COPY which allows wildcards.
This is a short example:
FROM alpine as builder
RUN touch foo.txt
RUN touch bar.txt
FROM alpine
COPY --from=builder /*.txt ./
Build an run image:
$ docker build -t foobar .
$ docker run foobar ls
bar.txt
bin
dev
etc
foo.txt
...

Resources