Docker to build the front-end Java development environment detailed _java

Source: Internet
Author: User
Tags docker ps docker run

One, the solution of the pain point

1, to avoid building back-end development environment.

2, the development of environmental changes only need to change the mirror can be synchronized updates.

3, do not need to eclipse and other IDE tools.

4. Switch development projects

Second, the solution to the idea

Using Docker to start Ubuntu mirroring, build the development environment that the project needs in the container, use the mount volume to mount the local code in the container, compile the running code using the environment in the container, and the host hosts to access the services in the container through the Docker port. This way the front-end development machine will only need to deploy the Docker to be done.

Iii. about Docker

About Docker

This article does not intend to detail the knowledge of Docker, related articles have a lot, if interested can read this book, for the use of Docker I also limited to the development of this tool, if there is a wrong place and everyone pointed out.

Accelerator

Daocloud Accelerator

Iv. setting up the environment

After downloading and installing the Docker we can start, we are talking about Java, but the other environment is the same.

Get an Ubuntu mirror.

Docker pull Ubuntu

When you are done docker images , you will see a mirror that has just been updated.

Enter the container

Docker run-it Ubuntu

V. Install software, configure environment variables

First Update Apt-get

Apt-get Update

Then you can use the apt-get install * installation of the software you need, if you do not download the installation package to install themselves, and configure the environment variables, here is not to repeat.

Six, start the service

Enter the Tomcat directory, start the service, open the 0.0.0.0:8080 in the browser, and if there is nothing wrong, you will see that the server is inaccessible. This is because we have just started the service is within the Docker, if we do not do some operations, we can not access to Docker internal services.

So, let's exit the container first.

Exit

After the exit execution docker ps -a , we can see that we have just the container is still in, probably most just contact with Docker people will make this mistake, think after the container after the withdrawal of the container destroyed, but it is not.

If we want to enter this container again can execute the following command, the container ID please copy your own.

Docker exec-it container ID bash

Although the container is still running, he has not persisted, in order to prevent in the event that we modify the contents of the container as soon as possible to persist.

Docker commit Container ID java

The meaning of this command is to persist our container into a new mirror, named Java.

Start this new mirror.

Docker Run-it-p 8080:8080 Java

Note that our launch command has changed, one more p This command means that the 8080 port in the container is leaking to the host.

Visit again 0.0.0.0:8080 , we can see that little tabby cat, really lovely.

That container is still occupied with our memory what to do, kill him.

Docker RM Container ID

Now that our first step has been completed, we are going to integrate our code.

Seven, integrated code

The container we just started is a completely separate black box, and it doesn't even know where our code is, so we're going to use Docker mount volumes to allow the host and container to share the directory.

Sorry, we're going to have to kill the container we just started.

Docker run-it-v/users/name/web:/opt/root-p 8080:8080 Java

Our launch command added new member-V. The meaning of this command is to hang the Web directory in the user's root directory under the/opt/root directory in the container.

After entering the catalogue, we can find the files in the Web directory lying in the inside, like a sleeping Marisu waiting for your call.

Let's start calling.

MVN Clean install-u-plocal-dskiptests

After a while we'll see a package successful tip, copy the war package to the Tomcat WebApps directory, and access your project.

At this point our project has finally run, but there are a few problems.

1, every time to run such a long order? Good trouble.

2, each change code must be repackaged, time is very long.

3. What do you think of the startup log? What about the error?

4, how to modify the front-end template files do not need to restart the service?

Based on these questions, we need to write a script to solve the problem.

Eight, Shell script

The script will provide the following instructions

-Y update maven Package-compile-pack-publish-start Tomcat

-P Compile-package-publish-start Tomcat

-R Restart Tomcat

-C recompile Java file-publish-start Tomcat

-W Listening VM files, default 5S sync once

-L View Tomcat logs

-H Help

# need to change the variable ################################################################# # environment variables, according to the Conf directory select dev= "local" ######### ######################################################## # no changing variable # WAR package address war_url= "/opt/root/target/*.war" # Tomcat address Tom_url= "/USR/SHARE/TOMCAT7" # Project Boot address tom_root= "${tom_url}/webapps" # File listener interval, per second wt=5 # copy VM wc_vm= ' Src/main /webapp/web-inf/tpl/usr/share/tomcat7/webapps/root/web-inf/"# Copy class Wc_java=" target/classes/usr/share/tomcat7/ webapps/root/web-inf/"# Common Method # # using new package function Newwar () {# delete old package rm-rf ${tom_root}/* # move WAR package MV ${war_url}
  ${tom_root}/root.war} # reboot Tomcat function restart () {# Close started program killall-9 Java # Start service ${tom_url}/bin/startup.sh # Input boot log tail-f ${tom_url}/logs/catalina.out} # Instruction processing while getopts ': Yprcwlh ' optname do case ' $optname ' in ' Y '
  echo "Update jar Pack" MVN clean install-u-p${dev}-dskiptests newwar restart;; "P") echo "repackage" MVN clean Package-p${dev}-dskiptests newwar restart;;
  "R") echo "restart Tomcat" restart;;
  "C") echo "Recompile and restart service" MVN clean Compile-p${dev}-dskiptests cp-r ${wc_java} restart;;
  "W") echo "starts listening for VM files" # Listening VM watch-n ${WT} cp-r ${WC_VM};;
  "L") echo "log" # monitor VM tail-f ${tom_url}/logs/catalina.out;; "H") echo-y update maven Package-compile-pack-publish-start one-stop service "echo"--compile package release start one-stop service "echo"--restart Tomcat "echo"--Re-java file
  and deploy the restart service "echo"-W to listen for VM files, default 5S sync Once "echo"-l View Log "echo"-H help;; Esac

Nine, to promote the team

After three steps, our tools have been built, but how can we make it available to other people as well?

Docker provides cloud services, and if our mirrors are small enough to push the mirrors to the cloud for other people to download and run, we have more than 1G of mirrors ... So we can't use that way anymore.

Docker save Java-o./java.tar

Using the above command, you can persist the mirror to the local file Java.tar, and then transfer it to other students ' machines by other means, we use airdrop for a few minutes.

Docker Load-i Java.tar

Other students can use this command to load our mirrors into his docker.

The shell script is then integrated into the project root directory and can be used happily.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.