dockerbuilddocker-composemultiarch

Building two differently tagged docker images with docker-compose


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


Solution

  • 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