https://docs.docker.com/engine/reference/builder/
Dockerfile的作用
Docker can build images automatically by reading the instructions from a Dockerfile
Docker可以通过读取Dockerfile中的指令来自动构建映像。
Docker build --help
案例
首先准备一个可以启动的jar包。并在里面编写一个很简单的接口。
@RestController
public class TestController {
@GetMapping("hello")
public String hello() {
return "dockerfile hello";
}
}
yml指定为9000端口
server:
port: 9000
将jar包放在Docker环境的服务器上,并在同一个目录创建Dockerfile并编写如下内容
FROM java:8
//指定作者名字
MAINTAINER author
//挂载点
VOLUME /tmp
//将jar包添加到镜像内部并重命名为abcd.jar
ADD docker-file-0.0.1-SNAPSHOT.jar abcd.jar
//执行命令
ENTRYPOINT ["java","-jar","abcd.jar"]
在Dockerfile文件目录执行如下命令编译Dockerfile
docker build -t abcde:1.0 .
查询镜像
docker images
启动容器
docker run -d -p 宿主机端口:容器内端口 /bin/bash
ENTRYPOINT
ENTRY entry 入口
POINT point 点
The exec form, which is the preferred form
ENTRYPOINT ["executable", "param1", "param2"]
The shell form
ENTRYPOINT command param1 param2
官方更加推荐上面这种格式。
EXPOSE
作用:暴露TCP或者UDP端口。
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime.
You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
the default is TCP。
指定udp
EXPOSE 80/udp
To expose on both TCP and UDP, include two lines。
EXPOSE 80/tcp
EXPOSE 80/udp
Regardless of the EXPOSE settings, you can override them at runtime by using the -p flag.
docker run -p 80:80/tcp -p 80:80/udp ...
这句话是说Dockerfile指定的EXPOSE 端口都会被 -p 命令覆盖,也就是以运行时设置的为主。
FROM
The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.
指令FROM初始化一个新的构建步骤并对基础镜像设置后续指令。
FROM [--platform=<platform>] <image> [AS <name>]
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
As such, a valid Dockerfile must start with a FROM instruction
因此有效的Dockerfile必须以 FROM 指令开始。