12

How to deploy a Spring Boot application with Docker

 3 years ago
source link: https://marco.dev/2018/04/30/deploy-spring-with-docker/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to deploy Spring Boot with Docker

Dockerfile

For our image we use the alpine linux listribution. This is a minimalistic distribution ideal for our deployment.

We copy the jar file to the /opt directory as suggested by the FHS

## alpine linux with JRE
FROM openjdk:8-jre-alpine

## copy the spring jar
COPY target/*.jar /opt/myApp.jar

CMD ["/usr/bin/java", "-jar", "/opt/myApp.jar"]

To have a runnable jar you need to repackage the default jar.

Here you can find the official documentation: spring-boot:repackage

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
        </plugin>
    </plugins>
</build>

Build and run

You can build your Dockerfile with e.g.:

docker build -t spring-example .

and run it with:

docker run -it -p 8080:8080 spring-example

Error without repackaging

If you get this error no main manifest attribute, in [name of your app] something went wrong with the spring-boot-maven-plugin.

Verifiy that it is correctly configured.

Alpine image size

The alpine image size is smaller compared to other distributions.

The version with JRE (8-jre-alpine) is 82MB, the JDK’s version is 102MB.

Author

Marco Molteni

Marco Molteni Blog


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK