自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

云計算
Dockerfiles是由一系列命令和參數(shù)構(gòu)成的腳本,這些命令應(yīng)用于基礎(chǔ)鏡像并最終創(chuàng)建一個新的鏡像。鏡像可以非?;A(chǔ),僅僅包含操作系統(tǒng);也可以非常豐富,包含靈巧的應(yīng)用棧,隨時可以發(fā)布。當(dāng)你在使用Docker構(gòu)建鏡像的時候,每一個命令都會在前一個命令的基礎(chǔ)上形成一個新層。這些基礎(chǔ)鏡像可以用于創(chuàng)建新的容器。本篇文章將手把手教您如何從基礎(chǔ)鏡像,一步一步,一層一層的從Dockerfile構(gòu)建容器的過程。

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

基礎(chǔ)鏡像可以用于創(chuàng)建Docker容器。鏡像可以非?;A(chǔ),僅僅包含操作系統(tǒng);也可以非常豐富,包含靈巧的應(yīng)用棧,隨時可以發(fā)布。當(dāng)你在使用Docker構(gòu)建鏡像的時候,每一個命令都會在前一個命令的基礎(chǔ)上形成一個新層。這些基礎(chǔ)鏡像可以用于創(chuàng)建新的容器。本篇文章將手把手教您如何從基礎(chǔ)鏡像,一步一步,一層一層的從Dockerfile構(gòu)建容器的過程。

Docker簡介

Docker項目提供了構(gòu)建在Linux內(nèi)核功能之上,協(xié)同在一起的的高級工具。其目標(biāo)是幫助開發(fā)和運維人員更容易地跨系統(tǒng)跨主機交付應(yīng)用程序和他們的依賴。Docker通過Docker容器,一個安全的,基于輕量級容器的環(huán)境,來實現(xiàn)這個目標(biāo)。這些容器由鏡像創(chuàng)建,而鏡像可以通過命令行手工創(chuàng)建或者通過Dockerfile自動創(chuàng)建。

Dockerfiles

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

Dockerfile 語法

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

什么是語法?

非常簡單,在編程中,語法意味著一個調(diào)用命令,輸入?yún)?shù)去讓應(yīng)用執(zhí)行程序的文法結(jié)構(gòu)。這些語法被規(guī)則或明或暗的約束。程序員遵循語法規(guī)范以和計算機交互。如果一段程序語法不正確,計算機將無法識別。Dockerfile使用簡單的,清楚的和干凈的語法結(jié)構(gòu),極為易于使用。這些語法可以自我釋義,支持注釋。

Dockerfile 語法示例

Dockerfile語法由兩部分構(gòu)成,注釋和命令+參數(shù)

 

  1. # Line blocks used for commenting 
  2. command argument argument .. 

 

一個簡單的例子:

 

  1. # Print "Hello docker!" 
  2. RUN echo "Hello docker!" 

 

#p#

Dockerfile 命令

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

ADD

ADD命令有兩個參數(shù),源和目標(biāo)。它的基本作用是從源系統(tǒng)的文件系統(tǒng)上復(fù)制文件到目標(biāo)容器的文件系統(tǒng)。如果源是一個URL,那該URL的內(nèi)容將被下載并復(fù)制到容器中。

 

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

 

CMD

和RUN命令相似,CMD可以用于執(zhí)行特定的命令。和RUN不同的是,這些命令不是在鏡像構(gòu)建的過程中執(zhí)行的,而是在用鏡像構(gòu)建容器后被調(diào)用。

 

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

 

ENTRYPOINT

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

 

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

ENV

ENV命令用于設(shè)置環(huán)境變量。這些變量以”key=value”的形式存在,并可以在容器內(nèi)被腳本或者程序調(diào)用。這個機制給在容器中運行應(yīng)用帶來了極大的便利。

 

  1. # Usage: ENV key value 
  2. ENV SERVER_WORKS 4 

 

EXPOSE

EXPOSE用來指定端口,使容器內(nèi)的應(yīng)用可以通過端口和外界交互。

 

  1. # Usage: EXPOSE [port] 
  2. EXPOSE 8080 

 

FROM

FROM命令可能是最重要的Dockerfile命令。改命令定義了使用哪個基礎(chǔ)鏡像啟動構(gòu)建流程?;A(chǔ)鏡像可以為任意鏡像。如果基礎(chǔ)鏡像沒有被發(fā)現(xiàn),Docker將試圖從Docker image index來查找該鏡像。FROM命令必須是Dockerfile的首個命令。

 

  1. # Usage: FROM [image name] 
  2. FROM ubuntu 

 

MAINTAINER

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

 

  1. # Usage: MAINTAINER [name] 
  2. MAINTAINER authors_name 

 

RUN

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

 

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

 

USER

USER命令用于設(shè)置運行容器的UID。

 

  1. # Usage: USER [UID] 
  2. USER 751 

 

VOLUME

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

 

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

 

WORKDIR

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

 

  1. # Usage: WORKDIR /path 
  2. WORKDIR ~/ 

 

#p#

如何使用Dockerfiles

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

 

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

 

Dockerfile 示例一:創(chuàng)建一個MongoDB的鏡像

在這部分中,我們講一步一步創(chuàng)建一個Dockfile,這個Dockerfile可用于構(gòu)建MongoDB鏡像進而構(gòu)建MongoDB容器。

創(chuàng)建一個Dockerfile

使用nano文本編輯器,讓我們創(chuàng)建Dockerfile

  1. sudo nano Dockerfile 

 

定義文件和它的目的

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

 

  1. ############################################################ 
  2. # Dockerfile to build MongoDB container images 
  3. # Based on Ubuntu 
  4. ############################################################ 

 

設(shè)置基礎(chǔ)鏡像

 

  1. # Set the base image to Ubuntu 
  2. FROM ubuntu

定義作者

 

  1. # File Author / Maintainer 
  2. MAINTAINER Example McAuthor 

 

設(shè)置命令與參數(shù)下載MongoDB

 

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

 

設(shè)置MongoDB端口

 

  1. # Expose the default port 
  2. EXPOSE 27017 
  3. # Default port to execute the entrypoint (MongoDB) 
  4. CMD ["--port 27017"
  5. # Set default container command</span> 
  6. ENTRYPOINT usr/bin/mongod 

 

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

 

  1. ############################################################ 
  2. # Dockerfile to build MongoDB container images 
  3. # Based on Ubuntu 
  4. ############################################################ 
  5. # Set the base image to Ubuntu 
  6. FROM ubuntu 
  7. # File Author / Maintainer 
  8. MAINTAINER Example McAuthor 
  9. # Update the repository sources list 
  10. RUN apt-get update 
  11. ################## BEGIN INSTALLATION ###################### 
  12. # Install MongoDB Following the Instructions at MongoDB Docs 
  13. # Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ 
  14. # Add the package verification key 
  15. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 
  16. # Add MongoDB to the repository sources list 
  17. RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list 
  18. # Update the repository sources list once more 
  19. RUN apt-get update 
  20. # Install MongoDB package (.deb) 
  21. RUN apt-get install -y mongodb-10gen 
  22. # Create the default data directory 
  23. RUN mkdir -p /data/db 
  24. ##################### INSTALLATION END ##################### 
  25. # Expose the default port 
  26. EXPOSE 27017 
  27. # Default port to execute the entrypoint (MongoDB) 
  28. CMD ["--port 27017"
  29. # Set default container command 
  30. ENTRYPOINT usr/bin/mongod 

 

構(gòu)建鏡像

使用上述的Dockerfile,我們已經(jīng)可以開始構(gòu)建MongoDB鏡像

 

  1. sudo docker build -t my_mongodb . 

 

#p#

Dockerfile 示例二:創(chuàng)建一個Nginx的鏡像

Nginx簡述

Nginx是一個高性能的 HTTP 和 反向代理 服務(wù)器。它因為它的輕量級,易用,易于擴展而流行于業(yè)界?;趦?yōu)良的架構(gòu)設(shè)計,它能夠比之前的類似軟件處理更多的請求。它也可以用來提供靜態(tài)文件服務(wù),比如圖片,腳本和CSS。

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

 

  1. ############################################################ 
  2. # Dockerfile to build Nginx Installed Containers 
  3. # Based on Ubuntu 
  4. ############################################################ 
  5. # Set the base image to Ubuntu 
  6. FROM ubuntu 
  7. # File Author / Maintainer 
  8. MAINTAINER Maintaner Name 

 

安裝Nginx

 

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

 

Bootstrapping

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

 

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

 

最后的Dockerfile

 

  1. ############################################################ 
  2. # Dockerfile to build Nginx Installed Containers 
  3. # Based on Ubuntu 
  4. ############################################################ 
  5. # Set the base image to Ubuntu 
  6. FROM ubuntu 
  7. # File Author / Maintainer 
  8. MAINTAINER Maintaner Name 
  9. # Install Nginx 
  10. # Add application repository URL to the default sources 
  11. RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list 
  12. # Update the repository 
  13. RUN apt-get update 
  14. # Install necessary tools 
  15. RUN apt-get install -y nano wget dialog net-tools 
  16. # Download and Install Nginx 
  17. RUN apt-get install -y nginx 
  18. # Remove the default Nginx configuration file 
  19. RUN rm -v /etc/nginx/nginx.conf 
  20. # Copy a configuration file from the current directory 
  21. ADD nginx.conf /etc/nginx/ 
  22. # Append "daemon off;" to the beginning of the configuration 
  23. RUN echo "daemon off;" >> /etc/nginx/nginx.conf 
  24. # Expose ports 
  25. EXPOSE 80 
  26. # Set the default command to execute 
  27. # when creating a new container 
  28. CMD service nginx start 

 

使用Dockerfile自動構(gòu)建Nginx容器

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

 

  1. sudo nano nginx.conf 

 

然后用下述內(nèi)容替換原有內(nèi)容:

 

  1. worker_processes 1; 
  2. events { worker_connections 1024; } 
  3. http { 
  4.      sendfile on; 
  5.      server { 
  6.          listen 80; 
  7.          location / { 
  8.               proxy_pass http://httpstat.us/; 
  9.               proxy_set_header X-Real-IP $remote_addr; 
  10.          } 
  11.      } 

 

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

原文鏈接:http://www.oschina.net/news/64396/dockerfile-instructions
 

責(zé)任編輯:Ophira 來源: oschina.net
相關(guān)推薦

2009-11-07 11:18:57

2011-04-06 09:39:49

mysql5存儲

2015-07-21 12:43:58

Dockerfile命令實例

2022-01-07 06:12:08

RPC框架限流

2020-03-30 17:43:13

開源開源項目編寫文檔

2019-04-19 08:04:57

程序員Dockerfile容器

2018-10-15 10:13:00

網(wǎng)絡(luò)拓撲結(jié)構(gòu)

2021-06-29 12:10:00

CRC校驗碼C語言

2011-06-16 17:54:30

Qt Mplayer

2024-03-07 11:39:24

HadolintDockerfile工具

2009-09-03 10:52:41

C#遞歸樹

2009-08-12 16:38:35

C#讀取XML節(jié)點

2009-08-13 14:36:40

C#結(jié)構(gòu)體構(gòu)造函數(shù)

2009-07-06 18:16:00

Servlet程序Cookie

2010-03-18 20:00:35

Java socket

2011-07-05 17:54:43

QT Sqlite ARM

2023-10-19 11:53:53

2024-05-31 12:38:32

2019-08-14 08:03:49

LinuxShell腳本web服務(wù)

2009-08-20 09:58:06

C#操作文本文件
點贊
收藏

51CTO技術(shù)棧公眾號