Publish a spring cloud app with Docker

Source: Internet
Author: User
Tags docker ps docker run

The items covered in this article are:

Cloud-simple-docker: A simple Spring boot application

Docker is a virtual machine technology, exactly the Linux virtual machine technology LXC on the basis of a layer, can be regarded as a LXC-based container technology. You can think of a container as a simple version of the Linux environment (including root user rights, process space, user space, and network space) and the applications running in it. Containers are used to carry things, Docker can load the application itself and its running environment into the container, this is a small file, and then throw this file to any compatible server to run, also based on this point, Docker can simultaneously make the application deployment, testing and distribution have become unprecedented the efficiency and ease!

The following examples refer to the "Spring Boot with Docker" official example.

1) Create a simple application with only one class, containing the main method, with the following code:

@SpringBootApplication

@RestController

public class Application {

@RequestMapping ("/")

Public String Home () {

Return "Hello Docker World";

}

public static void Main (string[] args) {

Springapplication.run (Application.class, args);

}

}

2) Establish Dockerfile

# based on that image

From Daocloud.io/java:8

# Attach a local folder to the current container (Tomcat uses)

Volume/tmp

# Copy files to container

ADD Cloud-simple-docker-1.0.0.jar/app.jar

# Open Service Port

EXPOSE 8080

# Configure the command to execute after the container starts

entrypoint ["Java", "-djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]

It is important to note here that from the use of domestic Docker image, if even the foreign Docker image download, the basic is unlikely to download, the reason everyone knows.

With Dockerfile, you can deploy Docker.

3) Deploying Docker samples

The deployment is divided into 2 steps, namely to create the mirror and run it.

    • Creating mirrors

Test the compiled jar file to a server directory, which is the TMP directory. The dockerfile is then tested to the directory, and finally into the directory to run the command:

Docker build-t Local/cloud-docker-hello.

Don't drop the "." In the back. Symbol, this symbol indicates the directory, this command executes successfully, you will see the following interface:

After successful operation, a mirror is created and can be viewed using Docker images.

    • Run mirror

With the image you can run it and run it with the following command:

Docker Run-p 8080:8080–t Local/cloud-simple-docker

Where 8,080:8,080 indicates that the native port is mapped to the Docker instance port. If the native port is not open, you also need to open the port and open the port using the Firewall-cmd command in CentOS 7:

Firewall-cmd--zone=public--add-port=8080/tcp--permanent

Firewall-cmd–reload

After success, you can use Docker ps–a to view the mirroring operation:

Local/cloud-docker-hello | Latest | 3ef51d55eb27 | Minutes ago | 667.2 MB

You can see that this web App image, which includes the Java runtime, is 667MB.

The results of this step run are as follows:

Finally, you can test the app directly by using the Curl command or by opening it directly in the browser. Use the Curl command as follows:

Curl http://localhost:8080

Back to: Hello Docker World

Docker application, the water is deep, here is just a Hello world class example. The Spring Boot with Docker example also provides a MAVEN plug-in Docker-maven-plugin, which compiles the project directly into a docker image. In addition, this plugin allows you to complete continuous integration and automated deployment of your project in conjunction with Jenkins. Specifically, Jenkins automatically pulls the project code through git or SVN, then compiles it using MAVEN, and then calls the Dockerfile in the project to generate the image. So, no matter what the application, just need to deploy Docker, those configurations, the clutter of the environment is not necessary to build.

Publish a spring cloud app with Docker

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.