目录
-
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.Date;
-
- @RestController
- public class Hello {
- @RequestMapping(value = "/hello", method = RequestMethod.GET)
- public String hello() {
- return "Hello world, " + new Date();
- }
- }
其中application.properties下指定服务端口未8089:
- # 应用名称
- spring.application.name=springboothelloworld
- # 应用服务 WEB 访问端口
- server.port=8089
将jar包上传到docker所在的服务器上,并编写Dockerfile文件

- # java8镜像是容器的运行环境
- FROM java:8 as ship
- # 为了安全起见,在生产环境运行容器时不要用指root帐号和群组
- RUN addgroup --system app && adduser --system --ingroup app app
- # 指定容器的运行帐号
- user app
- # 指定容器的工作目录
- WORKDIR /home/app/
- # 将jar包复制到容器内
- ADD springboot-0.0.1-SNAPSHOT.jar /home/app/app.jar
- # 容器启动命令
- ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
为了安全起见,在生产环境运行容器时不要用指root帐号和群组,新建app用户作为运行账号。
执行Dockerfile中的配置,将springboot-helloworld-0.0.1-SNAPSHOT.jar制作成镜像
Dockerfile所在的目录 . 表示当前目录。
docker build -t springboot-helloworld .

查看构建的镜像
docker images | grep hello

采用docker指令启动容器:
docker run -itd --name springboot-helloworld -p 8089:8089 springboot-helloworld

查看日志:
docker logs -f springboot-helloworld

![]()