Dockerfile 簡版大全,附贈編寫實例

pkdi 9年前發布 | 11K 次閱讀 Dockerfile

 基礎鏡像可以用于創建Docker容器。鏡像可以非常基礎,僅僅包含操作系統;也可以非常豐富,包含靈巧的應用棧,隨時可以發 布。當你在使用Docker構建鏡像的時候,每一個命令都會在前一個命令的基礎上形成一個新層。這些基礎鏡像可以用于創建新的容器。本篇文章將手把手教您 如何從基礎鏡像,一步一步,一層一層的從Dockerfile構建容器的過程。

 

Docker簡介

Docker項目提供了構建在Linux內核功能之上,協同在一起的的高級工具。其目標是幫助開發和運維人員更容易地跨系統跨主機交付應用程序和他 們的依賴。Docker通過Docker容器,一個安全的,基于輕量級容器的環境,來實現這個目標。這些容器由鏡像創建,而鏡像可以通過命令行手工創建或 者通過Dockerfile自動創建。

Dockerfiles

Dockerfiles是由一系列命令和參數構成的腳本,這些命令應用于基礎鏡像并最終創建一個新的鏡像。它們簡化了從頭到尾的流程并極大的簡化了 部署工作。Dockerfile從FROM命令開始,緊接著跟隨者各種方法,命令和參數。其產出為一個新的可以用于創建容器的鏡像。

 

Dockerfile 語法

在我們深入討論Dockerfile之前,讓我們快速過一下Dockerfile的語法和它們的意義。

什么是語法?

非常簡單,在編程中,語法意味著一個調用命令,輸入參數去讓應用執行程序的文法結構。這些語法被規則或明或暗的約束。程序員遵循語法規范以和計算機 交互。如果一段程序語法不正確,計算機將無法識別。Dockerfile使用簡單的,清楚的和干凈的語法結構,極為易于使用。這些語法可以自我釋義,支持 注釋。

Dockerfile 語法示例

Dockerfile語法由兩部分構成,注釋和命令+參數

# Line blocks used for commenting
command argument argument ..

一個簡單的例子:

# Print "Hello docker!"
RUN echo "Hello docker!"

Dockerfile 命令

 

Dockerfile有十幾條命令可用于構建鏡像,下文將簡略介紹這些命令。

 

ADD

ADD命令有兩個參數,源和目標。它的基本作用是從源系統的文件系統上復制文件到目標容器的文件系統。如果源是一個URL,那該URL的內容將被下載并復制到容器中。

# Usage: ADD [source directory or URL] [destination directory]
ADD /my_app_folder /my_app_folder

CMD

和RUN命令相似,CMD可以用于執行特定的命令。和RUN不同的是,這些命令不是在鏡像構建的過程中執行的,而是在用鏡像構建容器后被調用。

# Usage 1: CMD application "argument", "argument", ..
CMD "echo" "Hello docker!"

 

ENTRYPOINT

ENTRYPOINT 幫助你配置一個容器使之可執行化,如果你結合CMD命令和ENTRYPOINT命令,你可以從CMD命令中移除“application”而僅僅保留參數,參數將傳遞給ENTRYPOINT命令,

# Usage: ENTRYPOINT application "argument", "argument", ..
# Remember: arguments are optional. They can be provided by CMD
# or during the creation of a container. 
ENTRYPOINT echo
# Usage example with CMD:
# Arguments set with CMD can be overridden during *run*
CMD "Hello docker!"
ENTRYPOINT echo

ENV

ENV命令用于設置環境變量。這些變量以”key=value”的形式存在,并可以在容器內被腳本或者程序調用。這個機制給在容器中運行應用帶來了極大的便利。

# Usage: ENV key value
ENV SERVER_WORKS 4

 

EXPOSE

EXPOSE用來指定端口,使容器內的應用可以通過端口和外界交互。

# Usage: EXPOSE [port]
EXPOSE 8080

FROM

FROM命令可能是最重要的Dockerfile命令。改命令定義了使用哪個基礎鏡像啟動構建流程。基礎鏡像可以為任意鏡 像。如果基礎鏡像沒有被發現,Docker將試圖從Docker image index來查找該鏡像。FROM命令必須是Dockerfile的首個命令。

# Usage: FROM [image name]
FROM ubuntu

MAINTAINER

我建議這個命令放在Dockerfile的起始部分,雖然理論上它可以放置于Dockerfile的任意位置。這個命令用于聲明作者,并應該放在FROM的后面。

# Usage: MAINTAINER [name]
MAINTAINER authors_name

 

RUN

RUN命令是Dockerfile執行命令的核心部分。它接受命令作為參數并用于創建鏡像。不像CMD命令,RUN命令用于創建鏡像(在之前commit的層之上形成新的層)。

# Usage: RUN [command]
RUN aptitude install -y riak

 

USER

USER命令用于設置運行容器的UID。

# Usage: USER [UID]
USER 751

 

VOLUME

VOLUME命令用于讓你的容器訪問宿主機上的目錄。

# Usage: VOLUME ["/dir_1", "/dir_2" ..]
VOLUME ["/my_files"]

WORKDIR

WORKDIR命令用于設置CMD指明的命令的運行目錄。

# Usage: WORKDIR /path
WORKDIR ~/

如何使用Dockerfiles

使用Dockerfiles和手工使用Docker Daemon運行命令一樣簡單。腳本運行后輸出為新的鏡像ID

# Build an image using the Dockerfile at current location
# Example: sudo docker build -t [name] .
sudo docker build -t my_mongodb .


Dockerfile 示例一:創建一個MongoDB的鏡像

在這部分中,我們講一步一步創建一個Dockfile,這個Dockerfile可用于構建MongoDB鏡像進而構建MongoDB容器。

 

創建一個Dockerfile

使用nano文本編輯器,讓我們創建Dockerfile

sudo nano Dockerfile

 

定義文件和它的目的

讓閱讀者明確Dockerfile的目的永遠是必要的。為此,我們通常從注釋開始寫Dockerfile。

############################################################
# Dockerfile to build MongoDB container images
# Based on Ubuntu
############################################################

 

設置基礎鏡像

# Set the base image to Ubuntu
FROM ubuntu

定義作者

# File Author / Maintainer
MAINTAINER Example McAuthor

設置命令與參數下載MongoDB

################## BEGIN INSTALLATION ######################
# Install MongoDB Following the Instructions at MongoDB Docs
# Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
# Add the package verification key
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
# Add MongoDB to the repository sources list
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
# Update the repository sources list once more
RUN apt-get update
# Install MongoDB package (.deb)
RUN apt-get install -y mongodb-10gen
# Create the default data directory
RUN mkdir -p /data/db
##################### INSTALLATION END #####################

設置MongoDB端口

# Expose the default port
EXPOSE 27017
# Default port to execute the entrypoint (MongoDB)
CMD ["--port 27017"]
# Set default container command</span>
ENTRYPOINT usr/bin/mongod

保存Dockerfile,下面的代碼是Dockerfile的完整版本

############################################################
# Dockerfile to build MongoDB container images
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Example McAuthor
# Update the repository sources list
RUN apt-get update
################## BEGIN INSTALLATION ######################
# Install MongoDB Following the Instructions at MongoDB Docs
# Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
# Add the package verification key
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
# Add MongoDB to the repository sources list
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
# Update the repository sources list once more
RUN apt-get update
# Install MongoDB package (.deb)
RUN apt-get install -y mongodb-10gen
# Create the default data directory
RUN mkdir -p /data/db
##################### INSTALLATION END #####################
# Expose the default port
EXPOSE 27017
# Default port to execute the entrypoint (MongoDB)
CMD ["--port 27017"]
# Set default container command
ENTRYPOINT usr/bin/mongod

構建鏡像

使用上述的Dockerfile,我們已經可以開始構建MongoDB鏡像

sudo docker build -t my_mongodb .

Dockerfile 示例二:創建一個Nginx的鏡像

Nginx簡述

Nginx是一個高性能的 HTTP 和 反向代理 服務器。它因為它的輕量級,易用,易于擴展而流行于業界。基于優良的架構設計,它能夠比之前的類似軟件處理更多的請求。它也可以用來提供靜態文件服務,比如圖片,腳本和CSS。

和上個例子一樣,我們還是從基礎鏡像開始,運用FROM命令和MAINTAINER命令

############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Maintaner Name

 

安裝Nginx

# Install Nginx
# Add application repository URL to the default sources
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx

Bootstrapping

安裝Nginx后,我們需要配置Nginx并且替換掉默認的配置文件

# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute
# when creating a new container
CMD service nginx start

最后的Dockerfile

############################################################
# Dockerfile to build Nginx Installed Containers
# Based on Ubuntu
############################################################
# Set the base image to Ubuntu
FROM ubuntu
# File Author / Maintainer
MAINTAINER Maintaner Name
# Install Nginx
# Add application repository URL to the default sources
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
# Update the repository
RUN apt-get update
# Install necessary tools
RUN apt-get install -y nano wget dialog net-tools
# Download and Install Nginx
RUN apt-get install -y nginx
# Remove the default Nginx configuration file
RUN rm -v /etc/nginx/nginx.conf
# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/
# Append "daemon off;" to the beginning of the configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute
# when creating a new container
CMD service nginx start

使用Dockerfile自動構建Nginx容器

因為我們命令Docker用當前目錄的Nginx的配置文件替換默認的配置文件,我們要保證這個新的配置文件存在。在Dockerfile存在的目錄下,創建nginx.conf:

sudo nano nginx.conf


然后用下述內容替換原有內容:

worker_processes 1;
events { worker_connections 1024; }
http {
     sendfile on;
     server {
         listen 80;
         location / {
              proxy_pass http://httpstat.us/;
              proxy_set_header X-Real-IP $remote_addr;
         }
     }
}


讓我們保存nginx.conf。之后我們就可以用Dockerfile和配置文件來構建鏡像。

Via:digitalocean

 本文由用戶 pkdi 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!