Springcloud Tutorials | 11th: Docker Deployment Spring Cloud Project

Source: Internet
Author: User
Tags lua

Copyright NOTICE: This article for Bo Master original article, welcome reprint, reprint Please indicate the author, original hyperlink, Bo main address: Http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/article/details/70198649

Directory (?) [+]

Reprint please indicate the source:
http://blog.csdn.net/forezp/article/details/70198649
This article is from Fang Zhibong's blog

First, Docker introduction

Docker is an open-source engine that makes it easy to create a lightweight, portable, self-sufficient container for any application. Developers who compile tests on notebooks can be deployed in batches in a production environment, including VMS (virtual machines), bare metal, OpenStack clusters, and other underlying application platforms.
Docker is typically used for the following scenarios:

    • Automated packaging and publishing of Web applications;
    • Automated testing and continuous integration, release;
    • Deploy and tune databases or other back-end applications in a service-oriented environment;
    • Build your own PAAs environment by compiling from scratch or by extending your existing OpenShift or cloud foundry platform.

Benefits of Docker

    • 1. Simplified procedure:
      Docker allows developers to package their apps and dependencies into a portable container and publish them to any popular Linux machine for virtualization. Docker has changed the way virtualization is done, allowing developers to directly manage their results in Docker. Easy and fast is already the biggest advantage of Docker, in the past a few days or even weeks of tasks, in the Docker container processing, only a few seconds to complete.

    • 2, avoid the choice of phobia:
      If you have a choice of phobia, or a senior patient. Docker helps you pack your tangle! Docker mirrors, for example, include the runtime environment and configuration, so Docker can simplify the deployment of multiple application instances. For example, WEB applications, background applications, database applications, big data applications such as Hadoop clusters, message queues, and so on can all be packaged into a single mirrored deployment.

    • 3. Save money:
      On the one hand, the advent of the cloud computing era, so that developers do not have to pursue the effect of the high-quality hardware, Docker changed the high performance of the inevitable price of thinking. The combination of Docker and cloud makes the cloud space more fully utilized. It not only solves the problem of hardware management, but also changes the way of virtualization.

The above text refers to the relevant articles, and the installation and basic use of Docker see the relevant tutorials.

Ii. preparatory work

Environmental conditions:

    • Linux system, not recommended for Windows
    • Latest Docker version
    • JDK 1.8
    • maven3.0

The project used in this article comes from the project of the first article, using MAVEN to build the project and using Docker-maven-plugin to build the Docker image.

Third, reconstruction project, Construction mirror Reconstruction Eureka-server Project

Add a plugin to the Pom file:

<Build><Plugins><Plugin><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-maven-plugin</Artifactid></Plugin><!--tag::p lugin[]-<Plugin><Groupid>com.spotify</Groupid><Artifactid>docker-maven-plugin</Artifactid><version>0.4.3</Version><Configuration><Imagename>${docker.image.prefix}/${project.artifactid}</Imagename><Dockerdirectory>src/main/docker</Dockerdirectory><Resources><Resource><targetpath>/</targetpath> < directory>${project.build.directory}</ directory> <include>${ Project.build.finalname}.jar</include> </resource> </ resources> </configuration> Span class= "Hljs-tag" ></plugin> <!--end::p lugin[]-- > </plugins> </BUILD>             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

Spotify's Docker-maven-plugin plugin builds Docker images with the Maven plugin.

    • IMAGENAME Specifies the name of the mirror, in this case forep/eureka-server
    • Dockerdirectory Specify the location of the Dockerfile
    • Resources refers to the files that need to be put together with Dockerfile and used when building the image, and the general application jar package needs to be included.

Modify the following configuration file:

server:  port: 8761eureka:  instance:    prefer-ip-address: true  client:    registerWithEureka: false fetchRegistry: false
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
To write a dockerfile file:
from frolvlad/alpine-oraclejdk8:slimvolume/tmpadd eureka-server -0.0.1 -snapshot.jar app.jar #RUN bash -c  ' Touch/app.jar ' entrypoint [ "java", - Djava.security.egd=file:/dev/./urandom ", "-jar ", "/app.jar " Span class= "Hljs-preprocessor" >]expose 8761       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
Docker File Authoring Instructions:
    • From
    <image>    FROM <image>:<tag> FROM <image> <digest>
    • 1
    • 2
    • 3
    • 4

The from directive must be specified and needs to be preceded by dockerfile other directives, the underlying image specified can be in the official remote repository, or it can be located in the local repository. Subsequent directives are dependent on the image specified by the directive. Multiple from directives can be used when multiple mirrors are created in the same dockerfile.

    • VOLUME

The format is:

     VOLUME ["/data"]
    • 1

Enables a directory in a container to have the ability to persist stored data, which can be used by the container itself or shared with other containers. This directive can be used in Dockerfile when the application in the container has the need to persist data.

    • ADD

Copy files from the SRC directory to the container's dest. Where SRC can be a relative path to the directory where the dockerfile resides, or it can be a URL, or it can be a compressed package

    • EntryPoint

Specifies the command that is executed when the Docker container starts, and can be set multiple times, but only the last one is valid.

    • EXPOSE

Set the external port number for the Docker container. At startup, you can use the-p option or the-P option.

Build image

To perform the build Docker image maven command:

package docker:build
    • 1
    • 2
    • 3

Build Eureka-server Mirror successfully.

Similarly build Service-hi mirrors
    • Pom file import with Eurek-server
    • Modify the following configuration file:
eureka:  client:    serviceUrl:      defaultZone: http://eureka-server:8761/eureka/ # 这个需要改为eureka-serverserver:  port: 8763spring: application: name: service-hi
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

In this case, the host of the Defaultzone Discovery service is changed to the mirror name.

    • Dockefile writing with Eureka-server

    • To build the image:

package docker:build
    • 1
    • 2
    • 3

At this point we run Docke's eureka-server and Service-hi mirrors:

-p 8761: 8761 -t forezp/eureka-serverdocker run -p 8763: 8763 -t forezp/service-hi
    • 1
    • 2
    • 3

Visit localhost:8761

Four, using docker-compose boot image

Compose is a tool for defining and running Docker applications with multiple containers. With compose, you can configure your app's services in one configuration file (Yaml format), and then use a single command to create and launch all the services referenced in the configuration. Let's enter compose's actual combat.

Use Docker-compose to format the image and start mirroring:

‘3‘services:  eureka-server:    image: forezp/eureka-server    restart: always    ports:      - 8761:8761 service-hi: image: forezp/service-hi restart: always ports: - 8763:8763
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

Input command: Docker-compose up

2 images were found to be started in the specified order.

SOURCE Download: Https://github.com/forezp/SpringCloudLearning/tree/master/chapter11

V. Using Docker-compose to orchestrate and start mirroring

Docker-compose can also build mirrors, and now we use docker-compose to build mirrors.

Now take Eureka-server as an example:
Move Dockerfile to Eureka-server's home directory, overwriting the relative path to add:

from frolvlad/alpine-oraclejdk8:slimvolume/tmpadd ./target/eureka -server-0.0 .1-snapshot.jar App.jar #RUN bash -c  ' Touch/app.jar ' entrypoint [" java ",  "-djava.security.egd=file:/dev/./urandom",  "-jar",  "/app.jar" ]EXPOSE 8761  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Similarly modify the Service-hi directory;

Write the build image Docker-compose-dev file:

‘3‘services:  eureka-server:    build: eureka-server    ports:      - 8761:8761 service-hi: build: service-hi ports: - 8763:8763
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

command to build the image and start:

docker-compose -f docker-compose.yml -f docker-compose-dev.yml up 
    • 1
    • 2

Springcloud Tutorials | 11th: Docker Deployment Spring Cloud Project

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.