《Docker —— 從入門到實踐­》正體中文版
  • 前言
  • Docker 簡介
    • 什麼是 Docker
    • 為什麼要用 Docker
  • 基本概念
    • 映像檔
    • 容器
    • 倉庫
  • 安裝
    • Ubuntu
    • CentOS
  • 映像檔
    • 取得映像檔
    • 列出
    • 建立
    • 儲存和載入
    • 移除
    • 實作原理
  • 容器
    • 啟動
    • 常駐執行
    • 終止
    • 進入容器
    • 匯出與匯入
    • 刪除
  • 倉庫
    • Docker Hub
    • 私有倉庫
    • 設定檔案
  • 資料管理
    • 資料卷
    • 資料卷容器
    • 備份、恢復、遷移資料卷
  • 使用網路
    • 外部存取容器
    • 容器互連
  • 進階網路設定
    • 快速設定指南
    • 設定 DNS
    • 容器存取控制
    • 埠號映射實作
    • 設定 docker0 橋接器
    • 自訂橋接器
    • 工具與範例
    • 編輯網路設定檔案
    • 實例:創造一個點對點連線
  • 實戰案例
    • 使用 Supervisor 來管理程式
    • 建立 tomcat/weblogic 集群
    • 多台實體主機之間的容器互連
    • 標準化開發測試和生產環境
  • 安全
    • 核心命名空間
    • 控制組
    • 伺服端防護
    • 核心能力機制
    • 其他安全特性
    • 總結
  • Dockerfile
    • 基本結構
    • 指令
    • 建立映像檔
    • 從映像檔產生 Dockerfile
  • 底層實作
    • 基本架構
    • 命名空間
    • 控制組
    • Union 檔案系統
    • 容器格式
    • 網路
  • 附錄一:命令查詢
  • 附錄二:常見倉庫介紹
    • Ubuntu
    • CentOS
    • MySQL
    • MongoDB
    • Redis
    • Nginx
    • WordPress
    • Node.js
  • 附錄三:資源連結
Powered by GitBook
On this page

Was this helpful?

  1. Dockerfile

基本結構

Dockerfile 由一行行命令語句組成,並且支援以 # 開頭的註解行。一般而言,Dockerfile 分為四部分:基底映像檔資訊、維護者資訊、映像檔操作指令和容器啟動時執行指令。例如:

# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..

# 基本映像檔,必須是第一個指令
FROM ubuntu

# 維護者: docker_user <docker_user at email.com> (@docker_user)
MAINTAINER docker_user docker_user@email.com

# 更新映像檔的指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# 建立新容器時要執行的指令
CMD /usr/sbin/nginx

其中,一開始必須指明作為基底的映像檔名稱,接下來說明維護者資訊(建議)。接著則是映像檔操作指令,例如 RUN 指令,RUN 指令將對映像檔執行相對應的命令。每運行一條 RUN 指令,映像檔就會新增一層。最後是 CMD 指令,指定執行容器時的操作命令。下面來看一個更複雜的例子:

# Nginx
#
# VERSION               0.0.1

FROM      ubuntu
MAINTAINER Victor Vieux <victor@docker.com>

RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server

# Firefox over VNC
#
# VERSION               0.3

FROM ubuntu

# Install vnc, xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
RUN mkdir /.vnc
# Setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox (might not be the best way, but it does the trick)
RUN bash -c 'echo "firefox" >> /.bashrc'

EXPOSE 5900
CMD    ["x11vnc", "-forever", "-usepw", "-create"]

# Multiple images example
#
# VERSION               0.1

FROM ubuntu
RUN echo foo > bar
# Will output something like ===> 907ad6c2736f

FROM ubuntu
RUN echo moo > oink
# Will output something like ===> 695d7793cbe4

# You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
# /oink.
PreviousDockerfileNext指令

Last updated 1 year ago

Was this helpful?