Docker Learns java Application Doker Deployment

The java micro-service deployment servers we wrote before are cumbersome. You need to install java on the server, configure environment variables, and then upload the war, jar package to the server deployment.Especially if you didn't use springboot before, you should install tomact, which is docker deployment today.

Preparing java services

pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mike</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Startup class:

package com.mike.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

Write a simple api interface:

package com.mike.demo;

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

@RestController
@RequestMapping("/api")
public class Hello {
    
    @GetMapping("/say/{word}")
    public String say(@PathVariable("word") String word) {
        return String.format("heelo,%s", word);
    }
}

Write dockerfile

Dockerfile:

FROM java:8
MAINTAINER mike 342746156@qq.com
COPY target/demo-0.0.1-SNAPSHOT.jar hello.jar
ENTRYPOINT ["java","-jar","/hello.jar"]
EXPOSE 8080

Dockerfile specifies the steps for building a docker image.

FROM: On the first level we need a jdk and we declare that using java:8, the docker engine will help us automatically download from the public repository and work in our running container environment.

MAINTAINER: Declare the author and may not write

COPY: Add the maven packaged jar to the container and rename it hello.jar

ENTRYPOINT: Command to execute and parameters.Start our application with java-jar

EXPOSE: Port 8080 in the leak container.This allows us to map the host's port to port 8080 to access the applications in the container

Start Mirroring

The first step is to use mvn install in the project directory to generate the jar package for the application, which will be generated in the target directory.Our jar packages are also available in the local maven repository.

Now start making mirrors.Execute the docker command in the directory where the Dockerfile is located:

docker build -t mike-hello .

Note. Indicates the current directory and is essential.So our mirrors are made.Run the docker command:

docker images

You can see the mirror we've built, but our app hasn't started yet.Next, use this image to launch a container to run our application

Boot Mirror

Run the docker command:

docker run -d -p 127.0.0.1:8080:8080 mike-hello

-d declares the way the daemon runs, -p declares the port mapping within the host and container, and maps the host's local port 8080 to the container's internal port 8080, which we leaked out while mirroring.Now run the docker command:

docker ps

You can see the currently started container, and you will see our application.Now test access on the host machine: http://localhost:8080/api/say/docker

You see the return message: heelo, docker.Test Successful

Now let's go inside the container and see:

Docker exec-ti our container id bash

This allows you to view the directories in the container and use the curl command to test our applications inside the container

curl http://localhost:8080/api/say/docker

You can also see that our message returned normally, and now if you want to deploy one, it's very simple:

docker run -d -p 127.0.0.1:8081:8080 mike-hello
docker run -d -p 127.0.0.1:8082:8080 mike-hello
docker run -d -p 127.0.0.1:8083:8080 mike-hello

Summary

Doker simplifies the deployment of our applications. As long as you have a mirror, how much you want to deploy is a command, without having to configure the application environment again and again.And with a private repository, you can start an application directly from multiple other servers, relying on a single command.Now we have a simple idea of how to use the docker, but it's still a bit cumbersome to build a deployment by losing the command yourself. We just want to finish writing our code and upload it to the repository, and the rest is done at a click.Then next lesson, use Jenkins to implement CICD and completely free you.

Welcome to my medium size: mike wants to do everything, share and learn

Tags: Linux Docker Java Maven Spring

Posted on Sun, 04 Aug 2019 19:29:51 -0700 by vikette