For details on how to install and use Docker, check out these two previous articles. Docker is deployed from installation to use and connection of Hello World and Docker containers. This article focuses on deploying the SpringBoot project on Docker. How to create springboot project can look at this article springboot introductory tutorial (a detailed explanation of intellij IDEA build springboot

This article mainly introduces the docker deployment of Springboot three ways, respectively: the way to get started, jar package replacement deployment and script deployment, step by step to hand by hand tutorial. Note that these three names are my own and unofficial.

V Project Catalog

VDockerfile file

Create a Dockerfile, which you’ll use later.

# Docker image for springboot file run # VERSION 0.0.1 Toutou # base image uses Java FROM Java :8 # VOLUME to specify the temporary file directory as/TMP. Create a temporary file in /var/lib/docker Jar ADD learn-web-0.0.1- snapshot.jar app.jar # RUN bash -c 'touch # RUN bash -c 'touch /app.jar' ENTRYPOINT [" Java ","-jar","/app.jar"] ### EXPOSE 8301Copy the code

V File Deployment

Create a/ data/docker/ Hellolearn folder on the server (the file path can be customized) and copy the JAR package and Dockerfile created above to the new folder (/data/docker/ Hellolearn) on the server.

V Image generation

docker build -t hellolearn .

Docker build-t Image name: indicates the relative position of the Dockerfile label. The dot (.) indicates the current directory. The default value is latest without a tag.

V Start container

docker run -d -p 8301:8301 --name hellolearn hellolearn

docker start hellolearn

The -d argument allows the container to run in the background; –name specifies the name of the container. -p is used for port mapping. In this case, port 8301(before the colon) in the server is mapped to port 8301(after the colon) in the container.

V to access the WEB

Vjar package mapping deployment

Following the steps above is enough to easily navigate the whole process of Deploying SpringBoot for Docker. However, after the container is started, every time the JAR package needs to be updated, the image will have to be remade and the container will have to be remade. The process is extremely complicated and inefficient. How do you deploy the jar without updating the image or the container?

5.1 update Dockerfile

# Docker image for springboot file run # VERSION 0.0.1 EXPOSE 8301 ENTRYPOINT [" Java ","-jar","/data/ learn-web-0.0.1-snapshot.jar "]Copy the code

The ENTRYPOINT [” Java “,”-jar”,”/data/ learning-web-0.0.1-snapshot.jar “] will run the learning-web-0.0.1-snapshot.jar file in the /data directory of the container.

5.2 Using the Docker build command to Build an Image

docker build -t hellolearn .

5.3 Creating and Starting containers

docker run --name hellolearn -it -v /data/docker/newhellolearn/package:/data -d -p 8301:8301 hellolearn

-v Host directory Associated container directory. This hosting/data/docker newhellolearn/package with the docker/data directory mapping.

So, when the changes occurred in the jars, direct more shinjuku host/data/docker/newhellolearn/package jar packages in the directory, update the jar after the package need to restart the container.

V Script Deployment

Jar –>> Learn-web-1.0.1-snapshot.jar –> learn-web-1.0.1-snapshot.jar –> Learn-web-1.0.1-snapshot.jar What do I have to do about it? Here’s a third way to deploy through scripting.

6.1 Creating the Hellolearning. sh file

Java - jar/data/learn - web - 0.0.1 - the SNAPSHOT. The jar

Will hellolearn. Sh upload/data/docker hellolearn/scriptdeploy/package, this folder path can be custom, remember the folder path, also can use later.

6.2 Adding The Script Execution Permission

chmod +x hellolearn.sh

6.3 create Dockerfile

# Docker image for springboot file run # VERSION 0.0.1 EXPOSE 8301 CMD ["sh","-c","/data/ hellolearne.sh "]Copy the code

CMD [“sh”,”-c”,”/data/ hellolearning.sh “] starts the hellolearning.sh script created in 6.1 when the container starts.

CMD: ENTRYPOINT CMD: ENTRYPOINT CMD: ENTRYPOINT CMD: ENTRYPOINT CMD: ENTRYPOINT CMD

6.4 Uploading the JAR Package

Put jars in hellolearn. Sh in the same folder directory, namely the/data/docker hellolearn/scriptdeploy/package, create a container and then the directory (host machine directory) mapping with container/data directory..

6.5 File directory Structure Diagram

Here directory structure do not know whether we are dizzy dizzy, upload a directory structure map of my host machine, at a glance.

6.6 Creating a Mirror

docker build -t hellolearn-script .

The dot at the end represents the current directory, so the command to generate the image needs to be executed in the directory where the Dockerfile resides.

6.7 Creating and Starting a Container

docker run --name hellolearn-script -it -v /data/docker/hellolearn/scriptdeploy/package:/data -d -p 8302:8301 hellolearn-script

docker start hellolearn-script

Will be hosting/data/docker/hellolearn/scriptdeploy/package and containers/data directory mapping relation. In this case, when the JAR package changes, you can directly replace the JAR package in the shinjuku host directory. Even if the jar package name changes, the hellolearning.sh script needs to be updated again. Restart the container after the update.

6.8 Web Test Effect

V View docker logs

docker logs [OPTIONS] CONTAINER ID

The OPTIONS:

-f: tracks log output. --since: displays all logs at a certain start time. -t: displays the timestampCopy the code

7.1 Viewing Logs Generated After a specified time Only the last 100 Lines are displayed:

docker logs -f -t --since="2020-10-01" --tail=100 CONTAINER ID

7.2 Querying Logs of a specified period

docker logs -t --since="2020-10-01T19:00:00" --until "2020-10-01T19:00:00" CONTAINER ID

7.3 Viewing Logs Generated After a Specified Time:

docker logs -t --since="2020-10-01T19:00:00" CONTAINER ID

7.4 Viewing Logs Generated in the Last 5 Minutes:

docker logs --since 5m CONTAINER ID

7.5 Run the exec command to run bash on the specified container:

Docker exec hellolearn -it /bin/bash or docker exec it hellolearn bash

7.6 Viewing the Docker IP Address

docker inspect --format='{{.NetworkSettings.IPAddress}}' hellolearn

V problems encountered

Error response from daemon: driver failed programming external connectivity on endpoint flamboyant_leavitt (iptables failed: Iptables –wait -t NAT -a DOCKER -p TCP -d 0/0 –dport 8301 -j DNAT –to-destination 172.17.0.2:8301! -i docker0: iptables: No chain/target/match by that name.

Solution: Restart Docker. systemctl restart docker

Error Response from Daemon: Driver Failed Programming External Connectivity on endpoint mysql3308 (

V Source code address

Github.com/toutouge/ja…

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \