Dockerfile 的介绍
Dockerfile
Docker can build images automatically by reading the instructions from a
Dockerfile. A Dockerfile is atextdocument that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
- Dockerfile 是用于构建 docker 镜像的文件
- Dockerfile 里包含了构建镜像所需的“指令”
- Dockerfile 有其特定的语法规则
Dockerfile 基本结构
容器及进程,所以镜像就是一个运行这个进程所需要的环境。
举例:执行一个 Python 程序。假如我们要在一台 ubuntu 21.04 上运行下面这个 hello.py 的 Python 程序
hello.py 的文件内容:
print("hello docker")第一步,准备 Python 环境
$ apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip第二步,运行 hello.py
$ python3 hello.py
hello dockerDockerfile 文件结构:
FROM ubuntu:22.04
RUN apt-get update DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip
COPY hello.py /
CMD ["python3", "/hello.py"]Backlinks
Docker Notes
- Dockerfile 的介绍 [[dockerfile-intro]]
通过 commit 创建镜像
例子:在 [[dockerfile-intro#Dockerfile 基本结构]] 中 执行 Python 程序