Dockerfile 基础
学习目标
- 理解 Dockerfile 的作用
- 掌握基本构建命令
- 编写简单 Dockerfile
什么是 Dockerfile
Dockerfile = 镜像的"菜谱"
定义了如何从基础镜像构建你的应用镜像
第一个 Dockerfile
# Dockerfile
FROM nginx:latest
COPY index.html /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
docker build -t my-nginx .
docker run -d -p 8080:80 my-nginx
常用指令
| 指令 | 作用 |
|---|
FROM | 基础镜像 |
COPY | 复制文件 |
RUN | 执行命令 |
CMD | 容器启动命令 |
EXPOSE | 声明端口 |
ENV | 环境变量 |
WORKDIR | 工作目录 |
构建命令
docker build -t name:tag .
docker build -f Dockerfile.prod -t my-app .
docker build --no-cache -t my-app .
docker build --build-arg VERSION=1.0 -t my-app .
小结
| 命令 | 作用 |
|---|
docker build -t | 构建镜像 |
-f | 指定 Dockerfile |
--no-cache | 不用缓存 |
--build-arg | 构建参数 |