Building two differently tagged docker images with docker-compose

≯℡__Kan透↙ 提交于 2021-02-11 16:01:31

问题


I am currently on the way to deploy 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 plattform, 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.mI have tried numerous things like hardcoding the path to the dockerfile and such stuff. Despite the efforts I am getting a pretty undefined error:

ERROR: failed to reach build target project:x86_64

Any idea is appreciated.

Edit: I took the idea from here


回答1:


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


来源:https://stackoverflow.com/questions/61017559/building-two-differently-tagged-docker-images-with-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!