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="" xmlns:xsi=""
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>






Startup class:

package com.mike.demo;

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

public class DemoApplication {

    public static void main(String[] args) {, 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;

public class Hello {
    public String say(@PathVariable("word") String word) {
        return String.format("heelo,%s", word);

Write dockerfile


FROM java:8
COPY target/demo-0.0.1-SNAPSHOT.jar hello.jar
ENTRYPOINT ["java","-jar","/hello.jar"]

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 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 mike-hello
docker run -d -p mike-hello
docker run -d -p mike-hello


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