部署过程分为如下几个步骤:java
在IntelliJ IDEA中File->New->Project:
而后点Next->Finish.web
pom.xml中添加依赖:spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
添加一个控制器:docker
@Controller public class HelloWorld{ @GetMapping("/hello") public void helloworld(HttpServletResponse response) throws IOException { response.getWriter().write("Hello Spring-boot"); } }
运行项目,访问: http://localhost:8080/hello,若是出现: Hello Spring-boot
第二步完成.浏览器
在项目目录建立Dockerfile文件: src/main/docker/Dockerfile(没有后缀),内容以下:springboot
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD demo-0.0.1-SNAPSHOT.jar app.jar RUN sh -c 'touch /app.jar' ENV JAVA_OPTS="" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
其中 demo-0.0.1-SNAPSHOT.jar
是项目打包后 /target/
里面的文件名ruby
<properties> <docker.image.prefix>ramer</docker.image.prefix> </properties> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.11</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build>
cmd进入到当前目录:oracle
cd Z:/Desktop/springboot-demo
打包,建立镜像:app
mvn package -Dmaven.test.skip=true docker:build
注意: 请确保maven已添加到path中;而且已安装docker,若是没有安装,请访问: https://www.docker.com/community-edition#/download 下载合适的版本.dom
##最好添加--name参数 docker run --name=springboot-docker-demo -p 8080:8080 -t ramer/demo
在浏览器访问: http://localhost:8080/hello
cmd: docker ps
cmd: docker start/stop/restart/rm CONTAINER_ID/NAME
其中:
CONTAINER_ID: 是容器id,执行 docker ps
可查看 NAME: 是容器的名称,也就是docker run –name后面的名字