https://docs.docker.com/get-started/part2/
https://docs.docker.com/engine/reference/builder/
使用者可以透過Dockerfile來定義Docker container的開發環境。
Dockerfile的基本格式:
# comment
INSTRUCTION arguments
INSTRUCTION arguments
引用官方範例如下:
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
FROM
FROM <image>[:<tag>]
引入container所需要的Base Image。
WORKDIR
WORKDIR /path/to/workdir
指定docker執行指令的工作目錄
ADD
ADD <src>... <dst>
將<src>的檔案及目錄複製到image的<dst>中
RUN
RUN <command>
RUN ['executable', 'param1, 'param2']
在當前的image上會再建立新的一層image,並在新的image執行RUN的command
EXPOSE
EXPOSE <port> [<port>/<protocol>...]
指定container對外部開放的port
ENV
ENV <key> <value>
ENV <key>=<value> ...
建立build stage時的environment variable
CMD
CMD ["executable","param1","param2"]
CMD ["param1","param2"]
CMD command param1 param2
image被啟動時所執行的command,在Dockerfile中只有最後一個CMD會被執行。
沒有留言:
張貼留言