Use docker to deploy springboot service

In the previous article, docker was built in centos7 and tried basic image mirroring and container container command operations.
In this article, I will try to start and use the springboot service in the docker container. The basic steps are to prepare the jar, configure Docker, build the image, start the container, and test the service function.

Prepare the springboot jar

For a simple demonstration, I created a new springboot project and only wrote a controller interface for viewing the effect. The controller interface code is as follows:

@RestController
@RequestMapping
public class TestController {
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}

After confirming that the above code can run normally and the browser is normally accessed, generate a springboot runnable jar, which I named here demo.jar, and then upload this file to the directory where the docker image is to be built in the linux virtual machine, for example /root/docker/demo.

Configure Dockerfile

After you have the jar, you can configure the Dockerfile file. I came here in this order because the jar file needs to be specified in the Dockerfile.
In fact, the Dockerfile file is just a static file, and there is no problem with the configuration before the jar. It only needs to keep the name and path of the jar generated later consistent with the Dockerfile.
In the directory where the jar above is located, use the vi command to create and edit the Dockerfile:

vi Dockerfile

Edit file content:

FROM java:8
VOLUME /tmp
COPY demo.jar app.jar
RUN bash -c “touch /app.jar”
ENTRYPOINT ["java","-jar","/app.jar"]

The meaning of the above content is as follows:

The first line, the image image generated by this Dockerfile, is based on java8; the
third line, demo.jarcopies the files in the current directory to the docker container, and renames them at the same time app.jar; the
fourth and fifth lines, run app.jar, that is to start spingboot service.

Careful people will find that the above explanation does not mention the second line. This is because this line is not necessary. It is only used when the file system needs to be used, that is, the program in docker needs to be configured when directly operating files. This one.
Moreover, this way of writing is just one of the ways that docker and host files are associated. There are other ways of writing, and we will learn more later.

The above configuration is similar to that written in many online tutorials. To a certain extent, it may cause some misunderstandings for people who are not clear, and they will think that many of them are fixed writing, but in fact, many of the above content can be written in other ways.
First of all, the words at the beginning of each line above are all uppercase, but like the sql keywords when using mysql query, the keywords here do not have to be uppercase, and lowercase can also be used.
Secondly, if the second line is needed, the directory pointed to by the back does not have to use tmp, and others can also be used.
Then there is the operation of adding local files to the container, which can be used copyor used add. Both require two parameters, one is the local file path and file name, and the other is the file name in the docker container.
Therefore, the app.jar in the back can naturally be called by other names, as long as it is consistent when using it.
Therefore, the wording of the configuration above can also be changed to the following:

from java:8
volume /test
add demo.jar demo.jar
run bash -c "touch /demo.jar"
entrypoint ["java","-jar","/demo.jar"]

image build

Now that you have the jar you need to run and the configured Dockerfile file, the preparations are complete, and then you can build an image image, use the following command:

docker build -t docker-spring .

Pay special attention to the dot symbol at the end of the above command .. It is easy to be ignored. After executing the above command, you will see the following output:

Sending build context to Docker daemon  16.52MB
Step 1/6 : from java:8
 ---> d23bdf5b1b1b
Step 2/6 : volume /test
 ---> Running in 5533951f04ca
Removing intermediate container 5533951f04ca
 ---> 0965f253bac5
Step 3/6 : add demo.jar demo.jar
 ---> bff720047ccb
Step 4/6 : run bash -c "touch /demo.jar"
 ---> Running in 900c847c14a4
Removing intermediate container 900c847c14a4
 ---> edb32f0ef654
Step 5/6 : expose 8080
 ---> Running in c25a898a3e37
Removing intermediate container c25a898a3e37
 ---> 55bbf5e81907
Step 6/6 : entrypoint ["java","-jar","/demo.jar"]
 ---> Running in b891f3caeeb3
Removing intermediate container b891f3caeeb3
 ---> 8caaec5d617b
Successfully built 8caaec5d617b
Successfully tagged docker-spring:latest

The above output means that the image has been successfully built. If it is the first time that docker is used on this machine and there is no java8 mirror, the execution of the above command will be slower, and the java8 mirror will be pulled from the remote mirror library, if it is not the first The second time will be much faster, because this mirror already exists locally and will be used directly.

Start container

After the image is completed, you can specify the image to start the container. Because the container itself will start a port after running the service, but it does not directly use the port of the physical machine, it is necessary to map the service port in the docker with the external port when starting. At the same time, in order to facilitate the operation, you can also use the background running mode, use -d, then the final start command is as follows:

docker run -d -p 8000:8080 docker-spring

The above operation will start the docker-spring image, use port 8080 in the docker container, and map port 8080 to port 8000 of the virtual machine.
It should be noted that in actual use, the above parameter specification must be located before the mirror name. If the parameter is specified after the mirror name is written, the parameter will not take effect.

verification

After the container and service are started, you can use the virtual machine ip and mapped port in the browser to access the services in docker. For example, my virtual machine is 192.168.139.91. According to the interface code at the beginning, you can enter the following url in the browser:

http://192.168.139.91:8000/hello

You will see that the browser returns a hellostring normally , which proves that the docker's springboot service is deployed successfully.

Guess you like

Origin blog.csdn.net/tuzongxun/article/details/108401296