How to use Windows version Docker and run the Spring Cloud project with Docker at IntelliJ idea

Source: Internet
Author: User
Tags docker ps docker run docker machine

How to use Windows version Docker and use Docker to run Spring Cloud project in IntelliJ Idea # #: Prerequisite Preparation

1.1 First please make sure your computer is WINDOWS10 Pro or Enterprise Edition, only these two versions are only available with Hyper-V

#2: Introduction

In the past, if we wanted to use Docker on Windows, we used virual box to create virtual machines, and since Windows10 was released, Microsoft announced a series of Linux software landing windows, including Docker, Now we can use the Hyper-V virtual machine that comes with Windows to create the Docker service that runs.

Inteliij idea also supports Docker as the most practical IDE available today.

#3: Installing Docker for Windows

3.1 Download Docker for windows,https://store.docker.com/editions/community/docker-ce-desktop-windows from the official website and enter the installation interface after downloading. Docker will install automatically, the interface flashes over, the computer runs well, and after installation, Docker will pop a window to tell you that Hyper-V is not turned on, like this.

But if you now click OK is basically no use, you have to go to the BIOS to turn on hardware virtualization, the machine is HP's machine, open the tap F10 into the BIOS, other brands of the machine self-search into, like this

When you restart your computer, Docker will automatically run and still pop up the Hyper-V open window, which you can click OK to let Docker help you turn on Hyper-V. or be yourself in Control Panel-programs-programs and features-turn Hyper-V on or off in Windows features

To this, our Docker for Windows is already installed. Enter Docker--version on the command line to view the installed Docker version

3.2 Using a mirror in Docker

3.2.1 First use the official image as an example

Search for a corresponding image using Docker search <imageName>

Then use the Docker pull < mirror name:tag> such as Docker pulls Nginx:latest, tag does not enter the default fetch is the latest

After the image has been downloaded, we can view all the local images via the Docker images command.

I've downloaded the Java and nginx images here, as well as the image of the spring Cloud Eureka registry that I've packaged.

Using the Docker Run command to run the image, I'm running the nginx image here.

Using Docker to run Nginx successful access to the LOCALHOST:80 will be able to access the Nginx homepage, indicating that we have already run our first image in Docker, although the official image, but the sense of accomplishment is not low.

OK, after running the first image, we're going to start using Docker in IntelliJ idea and build our first spring boot program into Docker to run

#4: The preparation of idea

1:docker plugin, you first need to install the Docker plugin in your idea, navigate to file-setting-plugins and search for Docker integration installation.

2: Configure the Docker server to locate File-setting-build,execution,deployment-docker in idea

If you don't use Docker machine to manage the VM's needs, we're OK with the default Docker daemon, but before we do, we need to set up Docker

Set Docker to local connection to not require TLS encryption.

After completing this step, you can see in the idea's configuration window that you have successfully connected to the Docker on this machine

Here we have completed the configuration of Docker and then we can go into the real implementation phase.

#5: Create a spring Cloud project

1. First create a spring boot project in idea, how to create it and don't repeat it.

After the creation is complete, we add the dependencies in the Pom.xml

The Spring Cloud Registry project is written locally, so you need to add

The spring version needs to correspond to the version number of Spring cloud, and the detailed correspondence can be http://projects.spring.io/spring-cloud/to see

Since this is simply a demonstration of how to deploy the Spring boot project to Docker in idea, it is OK to simply configure the Eureka registry in the project,

Add annotations in the startup class to indicate that this is a Eureka Registry project

To configure a port in a configuration file

Then we completed the project, we can start to see if the project can start, after the launch we visit http://localhost:8761/, we can see that our Eureka Registry has been started, project writing no problem

And then there's the question of how to deploy the project to Docker.

#6: Deploy the project to Docker

First we need to write the Dockerfile file, create a new Docker folder in the Src-main directory, and then create a new Dockerfile file in it

The file contents are as follows

Where the red box is the name of the jar package after the item is packaged, the default is Artifactid-version.jar, and we can see that there is a running tag on the left, yes, this is the button that is used to build the jar into the image in idea and then put it in the Docker. But we need to configure it first.

We first configure the image name and the container name

Then you need to configure the port number that the Docker container needs to map

Then we click Run, we can see that the error is very quick, because Dockerfile is not in the same folder as the jar package we generated.

In order to solve this problem, I found two kinds of solutions:

Scenario 1: Use the MAVEN command first

MVN Clean Package

Package The project, you can see the packaged jar package in the target directory after the command is completed

Then put the jar package in the directory where the Dockerfile is located, like this

Then click on Run in Dockerfile,

In the Deploylog window, you can see that the build image is successful, and in the log window you can see the logging information that our project has played during the run.

It was clear that the build and deployment were successful, and the visit to http://localhost:8761/, there was something we wanted to see.

Use the Docker PS command at the command line to view the container information that is running

As you can see, the project we wrote in idea has been run into Docker.

Scenario 2: Use the Docker-maven-plugin plugin to configure the plug-in in Pom.xml

Then run the mvn clean package-dskiptests=true docker:build command in Ternimal, package the project and build the image, and the command executes to see

Under the Docker window, the image we've built has already appeared in the window.

Right click to create a new container

Jump to our deployment configuration, only need to be configured as in Scenario 1, click Run to OK, access to http://localhost:8761/, you can also see our Eureka running information. The Docker PS command also shows that our container is already running.

#7: summary

OK, here we are first installing the Windows version of Docker and then using idea to create a spring cloud project and deploy the project to Docker in idea, this is the end of this essay.

If you are interested in this blog, Welcome to share and discuss

If you have any questions, please contact [email protected],com

How to use Windows version Docker and run the Spring Cloud project with Docker at IntelliJ idea

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.