docker_practice/dockerfile/basic_structure.md

77 lines
2.1 KiB
Markdown
Raw Normal View History

## 基本結構
Dockerfile 由一行行命令語句組成,並且支援以 `#` 開頭的註解行。
2014-10-10 09:30:53 +00:00
2014-11-20 02:22:16 +00:00
一般而言Dockerfile 分為四部分:基底映像檔資訊、維護者資訊、映像檔操作指令和容器啟動時執行指令。
2014-10-10 09:30:53 +00:00
例如
```
# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..
2014-11-20 02:22:16 +00:00
# 基本映像檔,必須是第一個指令
2014-10-10 09:30:53 +00:00
FROM ubuntu
2014-11-20 02:22:16 +00:00
# 維護者: docker_user <docker_user at email.com> (@docker_user)
2014-10-10 09:30:53 +00:00
MAINTAINER docker_user docker_user@email.com
2014-11-20 02:22:16 +00:00
# 更新映像檔的指令
2014-10-10 09:30:53 +00:00
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
2014-11-20 02:22:16 +00:00
# 建立新容器時要執行的指令
2014-10-10 09:30:53 +00:00
CMD /usr/sbin/nginx
```
2014-11-20 02:22:16 +00:00
其中,一開始必須指明作為基底的映像檔名稱,接下來說明維護者資訊(建議)。
2014-10-10 09:30:53 +00:00
2014-11-20 02:22:16 +00:00
接著則是映像檔操作指令,例如 `RUN` 指令,`RUN` 指令將對映像檔執行相對應的命令。每運行一條 `RUN` 指令,映像檔就會新增一層。
2014-10-10 09:30:53 +00:00
2014-11-20 02:22:16 +00:00
最後是 `CMD` 指令,指定執行容器時的操作命令。
2014-10-10 09:30:53 +00:00
下面來看一個更複雜的例子
2014-10-10 09:30:53 +00:00
```
# 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.
```