如何将 Spring Boot (.war) 应用程序部署到 Cloud 运行?
How to deploy SpringBoot (.war) aplication to Cloud Run?
我有一个 SpringBoot 项目并部署到 Google App Engine。它工作正常。该网站没有被访问太多。但是它的计费成本上升了。
所以我决定将我的 SpringBoot 项目移动到“Cloud 运行”。
我试过以下 link
https://cloud.google.com/run/docs/quickstarts/build-and-deploy/java
但是在上面的教程中,他们指定了 jar 文件。
Jave 8, SpringBoot 2.3.0.RELEASE 版本在这个项目中使用。
pom.xml 应用引擎
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<projectId>MY-SPRING-BOOT-APP</projectId>
<version>1</version>
<ssl-enabled>true</ssl-enabled>
</configuration>
</plugin>
Docker 文件
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.8-jdk-11 as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/myapp.war /target/myapp.war
# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/target/myapp.war"]
#CMD ["catalina.sh","run"]
有什么想法吗?
最后,我成功地将我的 spring boot 应用程序部署到云 运行 而无需更改包类型。
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.8-jdk-11 as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/myapp-*.war /myapp.war
# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/myapp.war"]
我有一个 SpringBoot 项目并部署到 Google App Engine。它工作正常。该网站没有被访问太多。但是它的计费成本上升了。
所以我决定将我的 SpringBoot 项目移动到“Cloud 运行”。
我试过以下 link https://cloud.google.com/run/docs/quickstarts/build-and-deploy/java
但是在上面的教程中,他们指定了 jar 文件。
Jave 8, SpringBoot 2.3.0.RELEASE 版本在这个项目中使用。
pom.xml 应用引擎
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<projectId>MY-SPRING-BOOT-APP</projectId>
<version>1</version>
<ssl-enabled>true</ssl-enabled>
</configuration>
</plugin>
Docker 文件
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.8-jdk-11 as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/myapp.war /target/myapp.war
# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/target/myapp.war"]
#CMD ["catalina.sh","run"]
有什么想法吗?
最后,我成功地将我的 spring boot 应用程序部署到云 运行 而无需更改包类型。
# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.8-jdk-11 as builder
# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
# Build a release artifact.
RUN mvn package -DskipTests
# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim
# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/myapp-*.war /myapp.war
# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/myapp.war"]