Spring Boot with Docker deployment

Source: Internet
Author: User
Tags docker hub docker run

Spring Boot and Docker deployment Open Docker remote access

You first need to turn on the Docker remote access feature so that you can operate remotely.

    • CentOS 6

Modify the/etc/default/docker file and take effect after reboot (service Docker restart).

Docker_opts= "-h=unix:///var/run/docker.sock-h=0.0.0.0:2375"
    • CentOS 7

Open the/usr/lib/systemd/system/docker.service file and modify the Execstart line.

Execstart=/usr/bin/dockerd-  H tcp://0.0.0.0:2375-  H unix:///var/run/docker.sock

Effective after reboot

  Systemctl Daemon-reload      

Whether the test takes effect

Curl Http://127.0.0.1:2375/info
New MAVEN Project

The Pom.xml configuration is as follows:

<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> <groupId>test.springboot.docker</groupId> <artifactid >docker-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <gro Upid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid > <version>1.5.3.RELEASE</version>
<docker.image.prefix>springboot</docker.image.prefix> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</ar tifactid> </dependency> <dependency> <groupid>org.springframework.boot</g Roupid> <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> <plugin> <groupid>com.spotify& Lt;/groupid> <artifactId>docker-maven-plugin</artifactId> <version>0.4.14</version> <configuration> <imagename>${docker.image.prefix}/${project.artifactid}</image Name> <dockerDirectory>src/main/docker</dockerDirectory> <dockerho St>http://192.168.1.200:2375</dockerhost> <resources> &LT;RESOURC E> <targetPath>/</targetPath> <directory>${proj Ect.build.directory}</directory> <include>${project.build.finalname}.jar</includ e> </resource> </resources> </configuration> </plugin> </plugins> </build></project>

imageName:The name of the mirror is specified

dockerDirectory:Specify the location of the Dockerfile

dockerHost:Specify Docker Remote API address

resources:Refers to the files that need to be put together with dockerfile and used when building the image, the general application jar package needs to be included

Creating Java Classes
Package Hello;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RestController, @SpringBootApplication @restcontrollerpublic class application {    @RequestMapping ("/") Public    String Home () {        return ' Hello Docker world ';    }    public static void Main (string[] args) {        springapplication.run (application.class, args);}    }
Create Dockerfile

In the Src/main/docker directory, create a file named Dockerfile, which is configured as follows:

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

From Java: refers to the official Java image provided on the Docker hub, and with this base image, the Dockerfile can FROM get its state directly by instruction-that java is, it is already installed in the container. Next, run the Spring boot app with a custom command.

volume/tmp: Create the/tmp directory and persist it to the Docker Data folder because the inline Tomcat container used by spring boot is used by default /tmp as the working directory.

add Docker-springboot-0.0.1-snapshot.jar app.jar: Copy the App jar package to/app.jar

EntryPoint: represents a command executed by default after a container is run

The full directory structure is as follows:

Run the following command to create the Docker image:

Package Docker:build

  

Docker Boot Mirror

See if the project was uploaded successfully

Boot Mirror
Docker Run-p 8888:8080 Springboot/docker-springboot

Access via browser

Spring Boot with Docker deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.