docker实例一
基于springboot2.0.0部署,没有使用springboot的docker插件
@RestController
@RequestMapping("/docker")
public class DockerController {
@GetMapping("/test1")
public String test1() {
return "docker test1";
}
}
写Dockerfile
FROM java:8
VOLUME /app
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/docker-test1.jar"]
构建
docker build -t sosop/java-app1:v1.0.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:34959eed5df6bfe74a17fbf459ed29505d51a48d5cb4a39f215b613c65b8023b
Status: Downloaded newer image for java:8
---> d23bdf5b1b1b
Step 2/4 : VOLUME /app
---> Running in ece3f8cca292
Removing intermediate container ece3f8cca292
---> 86c85d1c101e
Step 3/4 : EXPOSE 8080
---> Running in d2c5b86091b2
Removing intermediate container d2c5b86091b2
---> ccd096eab953
Step 4/4 : ENTRYPOINT ["java", "-jar", "/app/docker-test1.jar"]
---> Running in a0f619ef071e
Removing intermediate container a0f619ef071e
---> 7e6adf484301
Successfully built 7e6adf484301
Successfully tagged sosop/java-app1:v1.0.0
下面解释下各条指令:
- FROM: 指定基础镜像,这里是需要java环境,所以依赖jdk8
- VOLUME: 定义匿名卷,VOLUME ["<路径1>", "<路径2>"…]或VOLUME <路径>,为了防止运行时用户忘记将动态文件所保存目录挂载为卷,在 Dockerfile 中,我们可以事先指定某些目录挂载为匿名卷
- EXPOSE:指令是声明运行时容器提供服务端口,这只是一个声明,在运行时并不会因为这个声明应用就会开启这个端口的服务
- ENTRYPOINT:与CMD很相似,运行命令,但是可以让镜像变成像命令一样使用,也可以做一些应用运行前的准备工作
运行容器:
docker run -d -p 80:8080 -v /data:/app --name app1 sosop/java-app1:v1.0.0
直接访问:http://localhost/docker/test1
网友评论