简体   繁体   中英

Spring Boot and Docker-compose setting arguments

I'm probably new on the docker area and try to combine my Spring-Boot project with docker and docker-compose, but somehow docker-compose does not work.

I'm using the com.spotify maven plugin as described in the spring boot documentation. I can build and run docker images with the spotify plugin and also from command line using the docker command, but not when I try to use docker-compose up.

Here I always get the error 'Error: Invalid or corrupt jarfile /app.jar'.

I'm sure that it has to do with the ARG JAR_FILE which is passed from the spotify plugin to the docker command. I don't know how to setup this ARG_FILE in the docker-compose.yml so that i'm able to start with docker-compose up and also run only the docker image using the spotify plugin.

Here is my setup:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>eu.devroyal</groupId>
<artifactId>someProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>someProject</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <postgres.version>42.2.2</postgres.version>
    <docker.image.prefix>eu.devroyal</docker.image.prefix>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>



    <!--dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgres.version}</version>
        <scope>runtime</scope>
    </dependency-->
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgres.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.196</version>

    </dependency>


</dependencies>

<build>
    <finalName>someProject-app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.6</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

Dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8188

docker-compose.yml:

 version: '3'
services:
   web:
        environment:
            - JAR_FILE=target/someProject-app.jar
        build: ./
        ports:
            - "8188:8188"

At the time you build the dockerfile, you must add this parameter

docker build --build-arg JAR_FILE=jar_file_path .

alternative way to do it in the docker-compose.yml

version: '3'
services:
   web:
      build:
        context: ./
        args:
          JAR_FILE: jar_file_path
      ports:
        - "8188:8188"

I would recommend this way

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/someproject-app.jar /app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8188

When Dockerfile gets executed the respective jar in the target will be copied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM