There are several main steps involved in deploying a Spring Boot project into a Docker container:
Prepare Docker Image
- First, you need to select a basic image, usually an image containing the Java runtime environment, such as OpenJDK. These images can be obtained from Docker Hub or other image repositories.
- Next, you need to build an image containing the Spring Boot application on the base image. This includes adding the application's JAR files, dependencies, and configuration files to the image.
Writing Dockerfile
- Dockerfile is a text file that contains instructions for building Docker images. A Dockerfile is required to specify how to build an image containing a Spring Boot application.
- In Dockerfile, you can use the COPY directive to copy local files, including JAR files and configuration files for Spring Boot applications, into the image.
- Use the RUN directive to run commands inside a container, such as installing dependencies or executing an application's build.
- Use the CMD or ENTRYPOINT directive to specify the command to run when the container starts, usually the command to start a Spring Boot application.
Building Docker Images
- Use the docker build command to build a Docker image from the Dockerfile. For example: docker build -t my-spring-boot-app ., where the -t option is used to specify the name and label of the image.
Run Docker container
- Use the docker run command to run your Docker image. For example: docker run -p 8080:8080 my-spring-boot-app, where the -p option is used to map the port of the host to the port of the container so that the application can be accessed externally.
- You can also use other options to configure containers, such as mount volumes, set environment variables, etc.
Access the application
- Once the container is running, the Spring Boot application running in the Docker container can be accessed by accessing the host's port (8080 in the above example).
The principle of Docker containerized deployment of Spring Boot projects involves packaging applications and their dependencies into a Docker image and then running the image within the container. This makes applications more portable and isolated in different environments, while also making them easier to deploy and scale. Using Docker can greatly simplify the application deployment process, provide better resource management and isolation, and higher scalability.
Less gossip and learn from technology! ! !
Method 1: Directly build a mirror of the jar package running
- The packaged program will be uploaded to the specified directory of the server
- For example: /home/www/spring-boot-image/spring-boot-docker-1.
- Create a Dockerfile file in this directory
FROM openjdk:8u312 MAINTAINER zhangt ADD spring-boot-docker-1. EXPOSE 8520 ENTRYPOINT ["java","-jar",""]
Dockerfile content explanation:
FROM openjdk:8u312
This line specifies the basic image fromopenjdk:8u312
Mirror build. It uses a mirror with version number 312 of OpenJDK 8 as the basis. This is a basic image containing the Java runtime environment.MAINTAINER zhangt
This line sets the maintainer information, although in newer versions of Docker,MAINTAINER
It is no longer recommended, but can be usedLABEL
to add similar metadata information.ADD spring-boot-docker-1.
This line usesADD
The command will be localspring-boot-docker-1.
Copy the file to the image and rename it to. This JAR file contains the executable code of the Spring Boot application.
EXPOSE 8520
This line usesEXPOSE
The command declares the port number that the container will listen to, which is specified here as 8520. Note that this is just a metadata declaration, which does not automatically map the port to the host.ENTRYPOINT ["java","-jar",""]
This line sets the commands to be executed when the container starts. In this case, the container willjava -jar
The command starts, which runs the Spring Boot application.java
The command will start the Java virtual machine (JVM) and execute itexecutable code in .
The purpose of this Dockerfile is to build a Docker image containing Spring Boot application based on the OpenJDK 8u312 image. Once built, you can use this image to run the container of the Spring Boot application, which will listen to port 8520, allowing external access to the application through appropriate port mapping.
After creating the Dockerfile file, execute theBuild a mirror
docker build -t zhangt .
Pay attention to the last one.
Indicates that the Dockerfile is in the current file directory. zhangt represents the built image. You can use ** after the build is successful.docker images
**Command to view the mirror.-t
Options are used to specify the name and label of the image, which you can usezhangt
Replace with the name and label you want.
After the image is successfully built, the container can be run
docker run -d --restart=always --name zhangt -p 8520:8520 zhangt
The meaning of each parameter:
docker run
: Commands used to start a Docker container.-d
: This is an option that indicates running the container in the background (daemon mode). The container will run in the background and will not occupy the terminal.--restart=always
: This is another option that means the container always restarts when exiting. Docker tries to automatically restart the container even if the container stops due to an error or other reason.--name zhangt
: This is an option to give a name to the container. The name of the container is set to "zhangt".-p 8520:8520
: This is an option to map host ports to container ports. This option maps the host's port 8520 to the container's port 8520. This way, external access to the host's port 8520 can access applications running in the container.zhangt
: This is the name or image name of the container, indicating that the container to be run is created based on a Docker image named "zhangt". If "zhangt" is a mirror name, Docker will look for the image and run it in the container.
The purpose of this command is to run a Docker container in the background, created using a "zhangt" image and map the host's port 8520 to the container's port 8520. The name of the container is set to "zhangt-p", and if the container exits under any circumstances, Docker will automatically restart it. This is often used to deploy applications to ensure that applications can automatically recover in unexpected situations.
You can use it after starting the container **docker ps
**Command to view the started containerdocker logs -f --tail 1000 container id
, you can view the service log.
If you want to update the jar package, you only need to usedocker cp spring-boot-docker-1. Container ID:/
, you can copy spring-boot-docker-1. into the container and rename it, thendocker restart container ID
Restart the container.
Method 2: Run the container based on jdk image to get the jdk image in the server
docker pull openjdk:8u181
Create a directory and upload the jar package to that directory
cd /home/ mkdir www/spring-boot-docker
In the directory of the same level placed by Jar, write a shell script named:
#!/bin/bash echo -e "\n################ build service start #########################" # delete docker container echo -e "\n1, delete docker container [developer-platform-basic-dev] start ......" sudo docker rm -f spring-boot-docker-1.0 # docker run # docker run developer-platform-basic-1.0.0 echo -e "\n2, docker run build container [spring-boot-docker-1.0] start ......" sudo docker run --name spring-boot-docker-1.0 -d -p 8741:8741 \ -v /home/www/spring-boot-docker:/jar openjdk:8u181 \ java -jar /jar/spring-boot-docker-1. --=dev echo -e "\n3, docker ps container [spring-boot-docker-1.0] start ...." sudo docker ps -a | grep spring-boot-docker-1.0 echo -e "\n4, docker logs container [spring-boot-docker-1.0] start ...." sudo docker logs -f -t spring-boot-docker-1.0 > ./logs/log_$(date +%Y%m%d).out 2>&1 & echo -e "\n################ build service end #########################"
Core script explanation:
1.sudo docker run
This is the command used to run containers in Docker. It is usually necessary to use sudo permissions to execute Docker commands to ensure that there are enough permissions to manage the container.
2.--name spring-boot-docker-1.0
: This is the name specified for the Docker container, the name of the container is set to "spring-boot-docker-1.0".
3.-d
: This is an option that means running the container in the background (i.e. running in daemon mode) rather than in foreground interactive mode.
4.-p 8741:8741
: This option is used to map the port of the host to the port of the container. Specifically, the 8741 port of the host is mapped to the 8741 port of the container, so that the external access to the application in the container can be accessed through the 8741 port of the host.
5.-v /home/www/spring-boot-docker:/jar
: This option is used to mount the host's file system directory with the directory in the container. In this case, mount the /home/www/spring-boot-docker directory on the host to the /jar directory inside the container. This is often used to copy the application's code and resource files from the host into a container to run the application inside the container.
6.openjdk:8u181
: This is the name and label of the Docker image to be used in the container. Here, a Java image based on OpenJDK 8u181 is used, which contains the Java runtime environment.
7.java -jar /jar/spring-boot-docker-1. --=dev
: This is a command that runs within the container. It starts the Java Virtual Machine (JVM) and runs a Spring Boot application within the JVM. Specifically, it runs /jar/spring-boot-docker-1. This JAR file and specifies the activation configuration of a Spring configuration file via –=dev.
The purpose of this script is to create a Docker container called "spring-boot-docker-1.0" that runs a Spring Boot-based Java application that listens for port 8741 and mounts the /home/www/spring-boot-docker directory on the host to the /jar directory inside the container for use by the application. In this way, the Spring Boot application running in the container can be accessed through the host's port 8741.
Run the scriptsh
In the future, you only need to replace the jar package in the host directory and restart the container.
This is the article about implementing Docker containerized deployment based on SpringBoot project. For more related content on SpringBoot Docker containerized deployment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!