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.

https://docs.docker.com/engine/reference/builder/

  • Dockerfile是用于构建docker镜像的文件
  • Dockerfile里包含了构建镜像所需的“指令”
  • Dockerfile有其特定的语法规则

举例:执行一个Python程序

容器即进程,所以镜像就是一个运行这个进程所需要的环境。

假如我们要在一台ubuntu 22.04上运行下面这个hello.py的Python程序

hello.py的文件内容:

  1. print("hello docker")

第一步,准备Python环境

  1. apt-get update && \
  2. DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.10 python3-pip python3.10-dev

第二步,运行hello.py

  1. $ python3 hello.py
  2. hello docker

一个Dockerfile的基本结构

Dockerfile

  1. FROM ubuntu:22.04
  2. RUN apt-get update && \
  3. DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.10 python3-pip python3.10-dev
  4. ADD hello.py /
  5. CMD ["python3", "/hello.py"]