Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement idea and use docker plug-in to realize one-click automatic deployment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to achieve one-click automatic deployment with docker plug-ins in idea". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Environment:

1. Docker enables remote connection access

Install the docker plug-in that configures idea

Create a project and configure

First, create a project

Second, configure the project

Maven packaging to generate an image

Create a container and deploy the project to docker

Modify the project and deploy with one click

Referenc

Environment:

Jdk1.8 or above.

Maven 3.2 +

Idea

Docker

1. Docker enables remote connection access

First of all, we need to turn on remote connection access to docker. Make sure that it is not the server where docker is located, and that you can access docker remotely.

Linux version of docker:

Modify docker.service file to add listening port-H tcp://0.0.0.0:2375

Vi / usr/lib/systemd/system/docker.service

Find ExecStart and add-H tcp://0.0.0.0:2375 at the end, as shown in the following figure

Restart docker

Systemctl daemon-reload systemctl start docker

If we have fire prevention, remember to add firewall policy or turn off the firewall.

Docker for window

Find the docker icon in the lower left corner of the computer and select settings with the right mouse button. Check Expose daemon on tcp://localhost:2375 without TLS under the General menu. No reboot is required.

Install the docker plug-in that configures idea

Search for Docker in File-- > Settings-- > Plugins in the input box, select and install. Restart Docker after the installation is complete.

Configure docker

Find Docker in File- > Settings- > Build,Execution,Deployment

Create a new docker instance, and then fill in the IP port number of the docker in Engine API URL. If Connection successful is shown below, it proves that the connection to docker is successful. If it fails, it may be that our previous step docker failed to open a remote connection.

After we have finished setting up, we go back to the idea main interface and see a docker window at the bottom of the page. Click the green arrow to connect to docker. When connected, it shows that Containers and Images are the containers and images we already have in docker.

Create a project and configure it 1. Create a project

I'll demonstrate it with a simple Eureka project.

File- > New-- > Project-- > Spring Initializr

Second, configure the project

Modify the pom.xml file to introduce the configuration of the docker-maven-plugin plug-in. Change the configuration to the label

Com.spotify docker-maven-plugin 1.0.0 build-image package build ${project.artifactId} Latest ${project.basedir} / src/main/resources http://127.0.0.1:2375 / {project.build.directory} ${project.build.finalName} .jar

Configure the basic configuration of the project. (this is not the point, just mention it)

① modifies the application.properties to add project-related information.

# whether the port number and IP address initiated by the project will be registered with the registry by server.port=9090eureka.instance.hostname=127.0.0.1#, and whether the service will be retrieved by falseeureka.client.register-with-eureka=false# if it is not a cluster environment. If it is standalone, it will be falseeureka.client.fetch-registry=falseeureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/.

② finds the project startup class and adds the @ EnableEurekaServer annotation

@ EnableEurekaServer@SpringBootApplicationpublic class EurekaserverApplication {public static void main (String [] args) {SpringApplication.run (EurekaserverApplication.class, args);}}

Add a Dockerfile file.

We add a file named Dockerfile in the EeurekaServer\ src\ main\ resources directory.

If we do not have the java:8 image in docker, please use docker pull java:8 to pull down the image first.

FROM java:8VOLUME / tmpADD * .jar app.jarEXPOSE 9090ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]

We added a VOLUME pointing to "/ tmp" because this is where the Spring Boot application defaults to where Tomcat creates the working directory. The effect is to create a temporary file on the host under "/ var/lib/docker" and link it to the container under "/ tmp". This step is optional for the simple application we wrote here, but may be necessary for other Spring Boot applications if it needs to be actually written in the file system.

To reduce the startup time of Tomcat, we added a system attribute pointing to "/ dev/urandom" as the entropy source. If you use the "standard" version of Tomcat (or any other Web server), you do not need a newer version of Spring Boot.

Maven packaging to generate an image

Use maven to package. We configured it in pom.xml, and if we are using maven's package, then we will automatically use the Dockerfile file to build.

We can see from the console that an image file with the same name as our project has been built for me.

We can see multiple eurekaserver:latest images in our mirror library in the docker window.

Create a container and deploy the project to docker

In the docker window, we find the image file we just created, right-click, select Create container, and we modify the configuration needed to create the container.

Modify Container name (container name) Bind ports (Ip and port mapping) in the Create Docker Configuration pop-up window

I'll add 127.0.0.1 here, 8080, 9090. Use local port 8080 to access 9090 of the container.

After we click Run, the container is automatically created and launched.

We can see that in the docker plug-in, there is an extra eurekaServer container, and the startup is successful, and the port number is 9090.

Access items in the container

What we set up earlier is to use port 8080 to access the project of container 9090.

We visit the project using 127.0.0.1VOF 8080 and the following page appears, which proves that the project started successfully.

We have basically completed all the configurations here.

Modify the project and deploy with one click

In the future, we can start the project as shown in the following figure, and as soon as we start our project, it will run in the docker container.

If we modify the project and start the project in the docker plug-in, we will find that the project is still the same before it was started, because we only started the container and did not repackage the modified project and generate the images of docker. If we want to run the package and start the project directly at startup, we can do the following.

We modify the configuration of the Docker Images we created earlier.

two。 Find Before launch:Activate tool window in the configuration startup item, where we add a Run Maven Gold.

We add a command package to the Command line here.

Save when the configuration is complete. In the future, when we start the project, we will execute the maven package command, automatically package us and generate the project docker image file to start.

In the future, if we modify the project, we can use the following figure to start. It automatically packages to create a docker image and starts the project.

If we just need to start the project, go to the docker plug-in window and start the corresponding project container.

This is the end of the introduction to "how to implement idea to automate deployment with one click with docker plug-ins". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report