harrymt / spring-boot-dockerized

A spring boot application that is Dockerized.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spring Boot Dockerized

A spring boot maven application that is Dockerized.

How to Dockerize a Spring Boot application

  1. Create/Take a basic Spring Boot application
  2. Create a Dockerfile in the root
  3. Make note of the follow pom.xml field:
<!-- Name of the Spring Boot application -->
<artifactId>harrymt-spring-boot-docker</artifactId>
  1. Add the following plugin to your pom file:
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
        <id>default</id>
        <goals>
            <goal>build</goal>
        </goals>
        </execution>
    </executions>
    <configuration>
        <repository>${project.artifactId}</repository>
        <tag>latest</tag>
        <buildArgs>
            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>
  1. Now you are ready to build a docker image with:
$ mvn package
  1. You should have created a docker image called harrymt-spring-boot-docker!
  2. Type the docker command to list all images
$ docker images
  1. Now run the image with port networking from the Spring boot port (8080) to our port 8081
$ docker run -p 8081:8080 harrymt-spring-boot-docker
  1. View the spring application at http://localhost:8081

  2. Now for cleanup:

# View running containers
$ docker ps -a
# Copy the first 4 chars of the CONTAINER ID running

# Stop and remove the running container
$ docker stop <the copied CONTAINER ID>
$ docker rm <the copied CONTAINER ID>

# Remove image we packaged
$ docker rmi harrymt-spring-boot-docker

Summary

$ git clone https://github.com/harrymt/spring-boot-dockerized .
$ mvn package

# Test that it works without being in a container
$ java -jar target/harrymt-spring-boot-docker-0.1.0.jar

# Create image
$ mvn package

# Run image and view it at http://localhost:8081
$ docker run -p 8081:8080 harrymt-spring-boot-docker

FAQ

  • Could not build image: Connection Refused?
    • If you are on Docker for Windows: Tick 'Expose daemon on tcp://localhost:2375 without TLS' on Docker->Settings->General

About

A spring boot application that is Dockerized.


Languages

Language:Java 100.0%