Docker builds the front end Java development environment

  • 2020-05-12 02:34:06
  • OfStack

1. Fix the pain

1. Do not build the back-end development environment.

2, the development environment changes only need to change the mirror to be able to synchronize the update.

3. eclipse and other IDE tools are not needed.

4. Switch development projects

2. Ideas

Launch Ubuntu mirroring with docker, build the development environment required by the project in the container, mount the local code into the container with the mount volume, compile and run the code with the environment in the container, and the host machine accesses the service in the container through the leaked port of docker. In this way, the front-end development machine only needs to deploy docker.

3. About docker

Understand docker

This article is not intended to cover the knowledge of docker in detail. There are many articles about docker. If you are interested, you can read this book.

The accelerator

daocloud accelerator

4. Build the environment

Once you've downloaded and installed docker, you can get started. We're talking about java, but the same goes for other environments.

Get the Ubuntu mirror.


docker pull ubuntu

Execution after completion docker images You'll see a mirror that you just updated.

Into the container


docker run -it ubuntu

5. Install software and configure environment variables

First, update apt-get


apt-get update

Then you can use it apt-get install * Install the software you need. If you don't, download the installation package and install it yourself, as well as configure the environment variables.

6. Start the service

Go into the tomcat directory, launch the service, and open 0.0.0.0:8080 in the browser. If this is correct, you will see that the server is not accessible. This is because the service we just started is in docker, and we cannot access the internal service of docker without doing some operations.

So, let's exit the container first


exit

Exit and execute docker ps -a , we can see that the container we just saw is still there. Probably most people who are new to docker will make this mistake, thinking that the container will be destroyed after exiting the container, but it is not.

If we want to re-enter the container, we can execute the following command. Please copy the container ID.


docker exec -it  The container ID bash

Although the container is still running, it is not persisted. In order to prevent 10000, it is persisted as soon as possible after we modify the contents of the container.


docker commit  The container ID java

This command means to persist our container to a new image called java.

Start the new image.


docker run -it -p 8080:8080 java

Notice the change in our startup command, one more -P. This command means to leak port 8080 from the container to the host.

To visit again 0.0.0.0:8080 We can see the little cat. It's so cute.

What if that container was taking up our memory? Kill it.


docker rm  The container ID

Now that we've done step 1, we're ready to integrate our code.

7. Integrate code

The container we just started is a completely separate black box that doesn't know where our code is, so we're going to use the mount volume of docker so that the host and the container can share directories.

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


docker run -it -v /Users/name/web:/opt/root -p 8080:8080 java

Our startup command adds a new member-v. This command means to hang the web directory in the user root directory into the container /opt/root directory.

After entering the directory, we can find the files in the web directory lying quietly inside, like Mary Sue waiting for you to call after sleeping for years.

Start calling.


mvn clean install -U -Plocal -DskipTests

After a period of time we will see the package success prompt, war package copy to tomcat webapps directory, you can access your project.

At this point our project is finally running, but there are a few problems.

1. Running this long every time? Good trouble.

2. It takes a long time to repackage each code change.

3. What about startup log? What 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 provides the following instructions

-y update maven package - compile - package - release - launch tomcat

-p compile-pack-release-start tomcat

- r restart tomcat

-c recompile java file - publish - start tomcat

-w listens to the vm file. By default, 5S is synchronized once

-l view tomcat log

- h help


docker run -it ubuntu
0

9. Promote to the team

After the three steps above, our tool is built, but how can we make it usable by others?

docker offers a cloud service, if our image is small enough we can push it to the cloud for the rest of the team to download and run, but our image exceeds 1G... So we can't do that.


docker run -it ubuntu
1

Using the above command, we can persist the image 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 run -it ubuntu
2

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

Integrate the shell script into the project root and you'll be happy to use it.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: