基本結構
Dockerfile 由一行行命令語句組成,並且支援以 # 開頭的註解行。
一般而言,Dockerfile 分為四部分:基底映像檔資訊、維護者資訊、映像檔操作指令和容器啟動時執行指令。
例如
1
# This dockerfile uses the ubuntu image
2
# VERSION 2 - EDITION 1
3
# Author: docker_user
4
# Command format: Instruction [arguments / command] ..
5
6
# 基本映像檔,必須是第一個指令
7
FROM ubuntu
8
9
# 維護者: docker_user <docker_user at email.com> (@docker_user)
10
MAINTAINER docker_user [email protected]
11
12
# 更新映像檔的指令
13
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
14
RUN apt-get update && apt-get install -y nginx
15
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
16
17
# 建立新容器時要執行的指令
18
CMD /usr/sbin/nginx
Copied!
其中,一開始必須指明作為基底的映像檔名稱,接下來說明維護者資訊(建議)。
接著則是映像檔操作指令,例如 RUN 指令,RUN 指令將對映像檔執行相對應的命令。每運行一條 RUN 指令,映像檔就會新增一層。
最後是 CMD 指令,指定執行容器時的操作命令。
下面來看一個更複雜的例子
1
# Nginx
2
#
3
# VERSION 0.0.1
4
5
FROM ubuntu
6
MAINTAINER Victor Vieux <[email protected]>
7
8
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
9
10
# Firefox over VNC
11
#
12
# VERSION 0.3
13
14
FROM ubuntu
15
16
# Install vnc, xvfb in order to create a 'fake' display and firefox
17
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
18
RUN mkdir /.vnc
19
# Setup a password
20
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
21
# Autostart firefox (might not be the best way, but it does the trick)
22
RUN bash -c 'echo "firefox" >> /.bashrc'
23
24
EXPOSE 5900
25
CMD ["x11vnc", "-forever", "-usepw", "-create"]
26
27
# Multiple images example
28
#
29
# VERSION 0.1
30
31
FROM ubuntu
32
RUN echo foo > bar
33
# Will output something like ===> 907ad6c2736f
34
35
FROM ubuntu
36
RUN echo moo > oink
37
# Will output something like ===> 695d7793cbe4
38
39
# You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
40
# /oink.
Copied!
Last modified 2yr ago
Copy link