1:建一个最简单的springcloud应用。
2:在根目录下新建dockerfile,文件如下:
FROM openjdk:8-jdk-alpine VOLUME /tmp ARG JAR_FILE COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
3:pom.xml中填加依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.longdb</groupId> <artifactId>docker-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>docker-demo</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <docker.image.prefix>longdbclass</docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <!--项目打包的名称--> <finalName>docker-demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </build> </project>
4:在idea下terminal下执行打包镜像指令:mvn install dockerfile:build 。成功后会返回镜像id,也可以在cmd中用docker -images指令看新打包的镜像id。我电脑上已经安装了docker for window.
5:运行打包好的springcloud项目。指令:docker run --name Test-v1.2 -d -p 9001:8080 3750bb60f76c
(Test-v1.2--为运行镜像时取的一个名称,-d 在后台运行, -p 端口映射,3750bb60f76c 镜像id)
6:在本机访问docker内的应用:http://localhost:9001/user/find
最基本的应用部署ok了,后面可能尝试下.net core webapi 部署到docker,本地docker镜像上传到腾讯云运行,本地docker镜像上传docker hub(https://www.cnblogs.com/longdb/p/10500891.html)。
最后感谢 永恒||承诺 兄弟的远程帮忙。