Docker Notes
T

Dockerfile 的介绍

Dockerfile

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document 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 reference | Docker Documentation

  • 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 docker

Dockerfile 文件结构:

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"]
Show Graph Visualisation