(1) First have a working SpringBoot application.

Clone the Github repository locally from Jerry’s Github:

CD go to the project folder and run the MVN spring-boot:run command

If Tomcat started on port: 5030(HTTP) is displayed on the console, the SpringBoot application is successfully started locally.

Then use the following url can access this SpringBoot application, if everything is normal, http://localhost:5030/commerce/product

You can see Hello World in your browser:

The SpringBoot application listens on port 5030. If you want to change it to another port, go to application.properties.

(2) The next step is to log in to Ali Cloud server and package the SpringBoot into a Docker image.

My Github repository has written a Dockerfile file, Docker image is based on the Dockerfile for production.

The FROM command on the first line specifies that our image will be based on the OpenJDK image.

The VOLUME command on the second line defines a persistent store that points to the TMP folder in the container. The default working directory created by the SpringBoot application for the built-in Tomcat server instance is TMP. Using this command, you can create a temporary directory in the host directory /var/lib/docker where Docker is running, and mount it to TMP inside the container.

This step can be omitted if your SpringBoot application does not perform persistent writes.

In line 3, add the jar file from the target folder in the local directory to the container and rename it app.jar.

Line 4: The ENV command sets environment variables. In complex usage scenarios, we might need to start the JVM with various parameters that are passed into the Java command through environment variables set by the ENV command. You can omit setting environment variables in this simple example.

Line 5: ENTRYPOINT, as the name implies, is the starting point at which the container image runs.

Now that we know what this Dockerfile does and the syntax, we use a Docker build to generate an image based on this Dockerfile.

docker build -t jerry/springbootexample:v1 .

The “. “at the end of the command line is not a punctuation mark, but a “.” in Linux, which represents the current directory.

After executing the command line above, you can see that the 5 commands defined in Dockerfile are executed in sequence. The first is to download the basic image of openJDK:

Then follow the remaining steps in turn.

If the message “Successfully Built” is displayed, the image is Successfully created.

Using the Docker images command line, you can see this image, which is 136MB in size.

(3) Finally, use the docker run command to execute the image.

The run command takes many arguments, such as running the image interactively:

docker run -it jerry/springbootexample:v1

In this way, the output of the mirror processing the user’s request is automatically redirected to the host console.

The -p parameter can realize port mapping. The meaning of the following command line is to map the port monitored by the SpringBoot application in Docker to port 8000 of the host. Thus, when the user accesses the browser, the port used should be the host port 8000.

docker run -p 8000:9000 –name jerrydockerdemo -d jerry/springbootexample:v1

You can use the docker ps command to get the ID of the running image, and then use the docker stop command to stop the running image.

You can also go inside a running container using the command docker exec-it:

sudo docker exec -it 8302db78f838 /bin/sh

TMP created with the VOLUME command when writing Dockerfile. In TMP, data generated in the working directory of its built-in Tomcat instance during SpringBoot execution is found.

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: