1. The pain points to be solved
1. No need to build a back-end development environment.
2. Changes in the development environment only require changing the mirror to synchronous updates.
3. No need for IDE tools such as eclipse.
4. Switch development projects
2. Solutions
Use docker to start Ubuntu images, build the development environment required by the project in the container, mount the local code into the container, use the mount volume to compile and run the code using the environment in the container, and the host accesses the services in the container through the port exposed by docker, so that the front-end development machine only needs to deploy docker and do it.
3. About docker
Understand docker
This article does not intend to explain the knowledge of docker in detail. There are many related articles. If you are interested, you can read this book. I am limited to the development of this tool for the use of docker. If there is any wrong thing, please point it out.
accelerator
daocloud accelerator
4. Build an environment
After downloading and installing docker, we can start. What we are talking about below is Java, but the same applies to other environments.
Get Ubuntu image.
docker pull ubuntu
After completing it, execute docker images and you will see a newly updated image.
Enter the container
docker run -it ubuntu
5. Install software and configure environment variables
First update apt-get
apt-get update
Next, you can use apt-get install * to install the software you need. If not, download the installation package and install it yourself. At the same time, configure the environment variables. I will not go into details here.
VI. Start the service
Enter the tomcat directory, start the service, and open 0.0.0.0:8080 in the browser. If nothing is wrong, you will see that the server cannot be accessed. This is because the service we just started is in docker. If we don’t do some operations, we will not be able to access the service inside docker.
So, let's exit the container first
exit
After exiting, execute docker ps -a and you can see that the container we just now is still there. Most people who are new to docker will make this mistake and think that the container will be destroyed after exiting the container, but in fact it is not the case.
If we want to enter this container again, we can execute the following command, please copy your own container ID.
docker exec -it container ID bash
Although the container is still running, it is not persisted. To prevent it from happening, it will be persisted as soon as possible after we modify the contents in the container.
docker commit container ID java
This command means to persist our container into a new image, called java.
Start this newly created image.
docker run -it -p 8080:8080 java
Pay attention to the change in our startup command, and there is an additional -P command. This command means to expose the 8080 port in the container to the host.
Visit 0.0.0.0:8080 again and we can see the little cat, it's so cute.
What should I do if the container was still occupying our memory just now? Kill him.
docker rm container ID
Our first step has been completed, and next we will integrate our code.
7. Integrated code
The container we just started is a completely independent black box, which has no idea where our code is, so we need to use docker's mount volume so that the host and container can share the directory.
Sorry, we are going to kill the container we just started again.
docker run -it -v /Users/name/web:/opt/root -p 8080:8080 java
Our startup command has added a new member -v. The meaning of this command is to hang the web directory under the user's root directory into the container in the /opt/root directory.
After entering the directory, we can find that the files in the web directory are lying quietly inside, like Mary Sue who has been sleeping for many years is waiting for your call.
Start calling.
mvn clean install -U -Plocal -DskipTests
After a period of time, we will see a prompt for successful packaging. Copy the war package to the tomcat webapps directory and you can access your project.
At this point, our project finally started running, but there are several problems.
1. Do you have to run such a long command every time? Very troublesome.
2. Every time you change the code, it has to be repackaged, which takes a long time.
3. How to read the startup log? What should I do if I report an error?
4. How to modify the front-end template file without restarting the service?
Based on these problems, we need to write a script to solve them.
8. Shell script
The script will provide the following instructions
-y Update maven package-compile-package-release-start tomcat
-p Compile-package-release-start tomcat
-r Restart tomcat
-c Recompile java file - Release - Start tomcat
-w listens to vm files, default 5S synchronization once
-l View tomcat log
-h Help
# Variables that need to be changed##################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################### Project startup address TOM_ROOT="${TOM_URL}/webapps"# File listening interval, unit WT=5# Copy vmWC_VM="src/main/webapp/WEB-INF/tpl /usr/share/tomcat7/webapps/ROOT/WEB-INF/"# Copy classWC_JAVA="target/classes /usr/share/tomcat7/webapps/ROOT/WEB-INF/"# General method# # Use new package function newwar(){ # Delete old package rm -rf ${TOM_ROOT}/* # Move war package mv ${WAR_URL} ${TOM_ROOT}/ROOT.war}# Restart tomcatfunction restart(){ # Close the started program killall -9 java # Start the service ${TOM_URL}/bin/startup.sh # Enter the startup log tail -f ${TOM_URL}/logs/catalina.out}# Directive processing while getopts ":yprcwlh" optnamedo case "$optname" in "y") echo "Update jar package" 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 the service" mvn clean compile -P${DEV} -DskipTests cp -R ${WC_JAVA} restart ;; "w") echo "Start listen to vm files" # Listen to VM watch -n ${WT} cp -R ${WC_VM} ;; "l") echo "Login" # Listen to VM tail -f ${TOM_URL}/logs/catalina.out ;; "h") echo " -y Update maven package-compile-package-release-start one-stop service" echo " -p Compile and package release starts one-stop service" echo " -r Restart tomcat" echo " -c Re-java file and deploy restart service" echo " -w listen to vm file, default 5S synchronization once" echo " -l View logs" echo " -h Help" ;; esac 9. Promote to the team
After the above three steps, our tools have been built, but how can others use them?
docker provides cloud services. If our image is small enough, we can push the image to the cloud for others to download and run, but our image has exceeded 1G. . . So we can't use this method.
docker save java -o ./java.tar
Use the above command to persist the image to the local file java.tar, and then transfer it to other classmates' machines through other means. We use AirDrop, a few minutes of things.
docker load -i java.tar
Other students can use this command to load our image into his docker.
Then integrate the shell script into the project root directory and you can use it happily.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.