iT邦幫忙

2022 iThome 鐵人賽

DAY 6
0
DevOps

從認識Docker到精通系列 第 6

DockerFile-3 Recap getting-started

  • 分享至 

  • xImage
  •  

昨天我們實際寫過了一個簡單的網頁,今天讓我們回到 getting-started 這個一開始就講到的映像,並檢視他的 dockerfile 做了什麼吧~

建議同時打開他的 github repo頁面,方便檢視檔案結構,讓理解過程更快速

dockerfile from getting-started

# Install the base requirements for the app.
# This stage is to support development.
FROM python:alpine AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

FROM node:12-alpine AS app-base
WORKDIR /app
COPY app/package.json app/yarn.lock ./
COPY app/spec ./spec
COPY app/src ./src

# Run tests to validate app
FROM app-base AS test
RUN apk add --no-cache python3 g++ make
RUN yarn install
RUN yarn test

# Clear out the node_modules and create the zip
FROM app-base AS app-zip-creator
COPY app/package.json app/yarn.lock ./
COPY app/spec ./spec
COPY app/src ./src
RUN apk add zip && \
    zip -r /app.zip /app

# Dev-ready container - actual files will be mounted in
FROM base AS dev
CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]

# Do the actual build of the mkdocs site
FROM base AS build
COPY . .
RUN mkdocs build

# Extract the static content from the build
# and use a nginx image to serve the content
FROM nginx:alpine
COPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zip
COPY --from=build /app/site /usr/share/nginx/html

FROM python:alpine AS base ~ FROM node:12-alpine AS app-base

  • 目的為安裝建制環境和測試環境所需的相依原件
  • 首先定義了工作目錄為 /app,並複製 requirements.txt 至目錄下,以 pip 安裝所需的套件
  • 接著pull node的alpine映像,建立app的基礎環境

FROM app-base AS test ~ FROM app-base AS app-zip-creator

  • 執行 test 檢測 app-base 的環境
  • app/package.json, app/yarn.lock, app/spec, app/src,複製到映像中並打包成 zip

FROM base AS dev

  • 測試環境,不解釋

FROM nginx:alpine

  • 為實際的deploy環境
  • app-zip-creator 壓縮出的 app.zip 複製到目錄,作為網站內容
  • build 的 /app/site 複製到目錄,作為網站架構

上一篇
DockerFile-2
下一篇
第七天-Multi-stage build
系列文
從認識Docker到精通30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言