4

I am currently on the way to deploying a Java application with Docker and K8s. As I am using a Raspberry Pi Kubernetes Cluster I want to generate two images, one for the x86 platform, and one for the arm32v7 (for testing on the Raspberry cluster). The goal is to generate two differently tagged docker images with one Dockerfile and push the resulting images to Docker Hub. I use the following Dockerfile:

FROM openjdk:8-alpine as x86

RUN mkdir -p /usr/src/app
COPY project/generated/distributions/executable/launch.jar /usr/src/app
WORKDIR /usr/src/app
CMD java -jar launch.jar


FROM arm32v7/adoptopenjdk:8-jre-hotspot-bionic as arm32

RUN mkdir -p /usr/src/app
COPY project/generated/distributions/executable/launch.jar /usr/src/app
WORKDIR /usr/src/app
CMD java -jar launch.jar

My docker-compose.yml looks like this:

version: '3.7'
services: 
  x86:
    build:
      context: .
      dockerfile: Dockerfile
      target: project:x86_64
  arm32:
    build:
      context: .
      dockerfile: Dockerfile
      target: project:arm32

Using docker build . works, but results in two unnamed, untagged images. I have tried numerous things like hardcoding the path to the Dockerfile and such stuff. Despite my efforts I am getting the following error:

ERROR: failed to reach build target project:x86_64

Any idea is appreciated.

Edit: I took the idea from here

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
A7exSchin
  • 382
  • 3
  • 17

1 Answers1

5

For anyone wondering, I figured it out with a little help.

The target definiton inside the build part of the docker-compose.yml is NOT to define the target image. It defines the target stage. To specify a image add the image portion to the multiple stages. Also no blank lines between the commands inside the Dockerfile, the interpreter will stop after a blank line. Here is the corrected, working code:

Dockerfile:

FROM openjdk:8-alpine as x86
RUN mkdir -p /usr/src/app
COPY project/generated/distributions/executable/launch.jar /usr/src/app
WORKDIR /usr/src/app
CMD java -jar launch.jar


FROM arm32v7/adoptopenjdk:8-jre-hotspot-bionic as arm32
RUN mkdir -p /usr/src/app
COPY project/generated/distributions/executable/launch.jar /usr/src/app
WORKDIR /usr/src/app
CMD java -jar launch.jar

And docker-compose.yml:

version: '3.7'
services: 
  x86:
    build:
      context: .
      dockerfile: Dockerfile
      target: x86
    image: foo.bar.example:x86_64
  arm32:
    build:
      context: .
      dockerfile: Dockerfile
      target: arm32
    image: foo.bar.example:arm32
A7exSchin
  • 382
  • 3
  • 17
  • Very useful. I've been looking for a solution to reuse an image built in multi-stage builds for a few hours and this is the most suitable answer I've found. Thank you. – ImanGM Feb 07 '21 at 15:52