说明:我的编译器是IDEA,操作系统是MacOS,所有的代码文章中都有,建议自己动手收获更多~
1、去springboot官网下载一个springboot项目(网址:https://start.spring.io/)
2、修改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.docker.example</groupId>
<artifactId>demo</artifactId>
<version>docker</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</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>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、配置application.properties文件
server.servlet.context-path=/test
4、编写自己的controller层
package com.docker.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Star
* @date 2020-05-09 18:28
*/
@RestController
public class DockerTestController {
@GetMapping("/docker")
public String dockerTest() {
return "hello docker hhh";
}
}
此时的项目结构为:(完整代码已上传至下载中心)
5、启动项目
6、在浏览器上输入http://localhost:8080/test/docker
7、使用maven进行打包
打包完成
8、查看jar包
9、把jar包放在固定的目录下,我的目录:/Users/xxx/Documents/lh/test/docker-demo
在当前目录下创建Dockerfile文件,文件内容如下
#获取base image
FROM adoptopenjdk/openjdk8:latest
#类似于执行 linux指令
RUN mkdir /opt/app
#类似于linux copy指令
COPY demo-docker.jar /opt/app/
#对外端口
EXPOSE 8080
#执行命令 java -jar /opt/app/demo-docker.jar
CMD ["java", "-jar", "/opt/app/demo-docker.jar"]
现在的目录效果:
$ ls
Dockerfile demo-docker.jar
10、创建Docker镜像
//命令
sudo docker build -t docker-demo .
执行过程需要输入本机密码,输入完之后回车,等候创建镜像成功
$ sudo docker build -t docker-demo .
Password:
Sending build context to Docker daemon 16.73MB
Step 1/5 : FROM adoptopenjdk/openjdk8:latest
latest: Pulling from adoptopenjdk/openjdk8
23884877105a: Pull complete
bc38caa0f5b9: Pull complete
2910811b6c42: Pull complete
36505266dcc6: Pull complete
3f0363b162ad: Pull complete
fe6480de91bf: Pull complete
Digest: sha256:66ca40f1847332a69624a5ed7444a51b522a6b7f2c6a984e6e33a9fbda1d0a50
Status: Downloaded newer image for adoptopenjdk/openjdk8:latest
---> 8d67cae7475e
Step 2/5 : RUN mkdir /opt/app
---> Running in c4de657f3bb3
Removing intermediate container c4de657f3bb3
---> d5b4d7fbf1d0
Step 3/5 : COPY demo-docker.jar /opt/app/
---> ca0bb0740376
Step 4/5 : EXPOSE 8080
---> Running in 788543ac21d6
Removing intermediate container 788543ac21d6
---> 12c5e03f6ddc
Step 5/5 : CMD ["java", "-jar", "/opt/app/demo-docker.jar"]
---> Running in a01e141a6b94
Removing intermediate container a01e141a6b94
---> 68d212d909bb
Successfully built 68d212d909bb
Successfully tagged docker-demo:latest
11、查看当前镜像
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-demo latest 68d212d909bb 8 minutes ago 317MB
adoptopenjdk/openjdk8 latest 8d67cae7475e 2 days ago 300MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
我们创建的镜像已经有了,名字就是docker-demo
12、使用镜像生成容器
$ docker run -it -p 8080:8080 docker-demo
部署成功
13、浏览器验证,出现下图就证明成功了
14、control+c停止到,使用命令使其在后台运行
$ docker run -d -it -p 8080:8080 docker-demo
b5290835e86a666ad8f60b102eacb74a3e2115a39e52bf6c7c41616678eb14da
15、查看当前运行的容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5290835e86a docker-demo "java -jar /opt/app/…" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp inspiring_heisenberg
16、进入容器
$ docker exec -it b5290835e86a /bin/bash
此时出现的效果如下图
这样就相当于我们进入容器里了,在最开始我们创建Dockerfile时拷贝过来的jar包
17、使用Docker应用查看日志
选择第一个,查看日志
18、谢谢博主,参考文档:https://www.jianshu.com/p/d998dc9d9685