Spring Boot and 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> </parent> <properties> <java.version& gt;1.8</java.version> </properties> <dependencies> <dependency> <groupi        D>org.springframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId>           </dependency> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPRING-BOOT-STARTER-TEST&LT;/ARTIFAC        tid> <scope>test</scope> </dependency> </dependencies> <build>                <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin&                Gt                <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.14</version> <configuration> <imagename>${d Ocker.image.prefix}/${project.artifactid}</imagename> <dockerdirectory>src/main/docker</ Dockerdirectory> <dockerHost>http://192.168.1.200:2375</dockerHost> & Lt;reSources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <includ E>${project.build.finalname}.jar</include> </resource> </resou rces> </configuration> </plugin> </plugins> &LT;/BUILD&GT;&LT;/PR Oject>

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:

From Javavolume/tmpadd docker-springboot-0.0.1-snapshot.jar app.jarrun bash-c ' Touch/app.jar ' ENV JAVA_OPTS= "" entrypoint ["Sh", "-C", "Java $JAVA _opts-djava.security.egd=file:/dev/./urandom-jar/app.jar"]

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 and 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.