The demo environment

CentOS 7.2

Docker 1.13.1

Spring Boot 2.1.1. RELEASE

Prepare a Spring Boot project

There are many ways to build a Spring Boot project that are not the focus of our discussion, so I won’t describe them here. We prepare a simple HelloController in the project, where a sayHello() method maps to the path where we access the project and returns: Hello Jerome, This is docker image! .

package top.nikk.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("")
    public String sayHello(a) {
        return "Hello Felix, This is docker image !"; }}Copy the code

Package the project and generate the executable JAR

Go to the root directory of the project and run the MVN Clean Package to package the project. After running it, an executable JAR file of xxx.jar will be generated in the target directory.

Write Dockerfile

# Docker image for springboot file run # VERSION 1.0.0 # Author: Felix # Base image using Java FROM Java :8 # author MAINTAINER Felix<[email protected]> # VOLUME specifies the temporary file directory as/TMP. Create a temporary file in /var/lib/docker Jar ADD felix-admin.jar app.jar RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]Copy the code

Explanation of the above command:

Fetching the OpenJDK from the Docker repository as the container VOLUME for our project points to a directory of/TMP. Since Spring Boot uses the built-in Tomcat container, Tomcat uses/TMP as the working directory by default. The effect is to create a temporary file in the host’s /usr/local/apps directory and connect to the container’s/TMP. The docker-Demo-0.0.1. jar of the project is added to the container as app.jar. ENTRYPOINT executes the project app.jar. To shorten Tomcat startup time, add a system attribute pointing to /dev/urandom as the Entropy Source from Java :8 pulls a docker image with JDK 1.8

  1. Maintainer is written by Felix maintainer

  2. Felix -admin.jar is the jar package you uploaded. Change the name to app.jar

  3. App.jar is the name you rename the JAR package to run in the container

  4. Expose is the number of ports on which the JAR runs in the container

  5. Java -jar demo.jar is used to start the jar container

Build the Docker image

Go to /usr/local/apps and run the Docker command to build the image

Docker build-t top.nikk/docker-demo:1.0.0Copy the code

Notice that there is a. Command at the end

Parameter Description:

-t: specifies the name of the target image to be created. : specifies the directory where the Dockerfile file resides. You can specify the absolute path of the DockerfileCopy the code

This command uses the Docker build command to build the image, and gives the image a name top.nikk/docker-demo with a TAG of 1.0.0 in the current folder.

We use Docker images to view all the images.

After performing

Docker run-id -p 8080:8080 --name demo top.nikk/docker-demo:1.0.0Copy the code