0

I’m new with docker. Plaining: to automate build process and create docker image and store the same on dockerHub. This entire build process I want to configure in DockerFile.

here is my Dockerfile

# the first stage of our build will use a maven 3.6.1 parent image
FROM maven:3.6.1-jdk-8-alpine AS MAVEN_BUILD
 
# copy the pom and src code to the container
WORKDIR ./

COPY ./ ./
 
# package our application code
RUN mvn clean package -X


FROM java:8-jdk-alpine

WORKDIR ./
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "config-consumer-0.0.1-SNAPSHOT.jar"]

build report on running automation build from Docker hub

figuring mojo 'org.sprin
gframework
.boot:spring-boot-maven-plugin:2.2.7.RELEASE:repackage' with basic configurator -->
[DEBUG] (f) attach = true
[DEBUG] (f) excludeDevtools = true
[DEBUG] (f) excludes = []
[DEBUG] (f) executable = false
[DEBUG] (f) finalName = config-consumer-0.0.1-SNAPSHOT
[DEBUG] (f) includeSystemScope = false
[DEBUG] (f) includes = []
[DEBUG] (f) outputDirectory = /target
[DEBUG] (f) project = MavenProject: com.my.config:config-consumer:0.0.1-SNAPSHOT @ /pom.xml
[DEBUG] (f) skip = false
[DEBUG] -- end configuration --
[INFO] Replaci
ng main arti
fact with repackaged archive
[INFO] -------------------------------------------------------
-----------------
[INFO] BUILD SUCCESS
[INFO] --------------------------
----------------------------------------------
[INFO] Total time: 42.126 s
[INFO] Finished at: 2020-06-02T10:38:05Z
[INFO
] ------------------------------------------------------------------------
Removing intermediate container 1ad5140601c5
---> f68f9fc066f1
Step 5/8 : FROM java:8-jdk-alpine
---> 3fd9dd82815c
Step 6/8 : WORKDIR ./
---> Using cache
---> ef8bf11c31c1
Step 7/8 : EXPOSE 8080
---> Using cache
---> cc296c743b8b
Step 8/8 : ENTRYPOINT ["java", "-jar", "config-consumer-0.0.1-SNAPSHOT.jar"]
---> Using cache
---> 726792fb0fd9
Successfully built 726792fb0fd9
Successfully tagged user/microservicedummy:latest
Pushing index.docker.io/user/microservicedummy:latest...
Done!
Build finished

error report on running container.

sudo docker run -p 3333:8080  user/microservicedummy
Unable to find image 'user/microservicedummy:latest' locally
latest: Pulling from user/microservicedummy
709515475419: Pull complete 
38a1c0aaa6fd: Pull complete 
5b58c996e33e: Pull complete 
Digest: sha256:7cf1809d2ea43c16a103985db626435715e589b898808fb87c565eaa096e52cd
Status: Downloaded newer image for user/microservicedummy:latest
Error: Unable to access jarfile config-consumer-0.0.1-SNAPSHOT.jar

container details are mention below...

CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS               NAMES
38b0a07f9677        user/microservicedummy   "java -jar config-co…"   15 minutes ago      Exited (1) 15 minutes ago                       mystifying_burnell

1 Answers1

2

Your Dockerfile is a multi-stage build with two stages. When you reach the second FROM line, Docker essentially starts over; nothing will be in your final image that isn't COPYed in or otherwise built after the final FROM line.

You need to COPY the jar file from the first stage:

COPY --from=MAVEN_BUILD /target/config-consumer-0.0.1-SNAPSHOT.jar .

I'd put this immediately after the WORKDIR line.

You should be able to verify the same thing by running

docker build -t nitishk58/microservicedummy .
docker run nitishk58/microservicedummy

offline; I'd expect you'd get the same results.

I also might change the final image to be a JRE-based image (FROM java:8-jre-alpine) to not include the full JDK; this will result in a significantly smaller image.

David Maze
  • 130,717
  • 29
  • 175
  • 215