成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

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

Docker簡介

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

Dockerfiles

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

Dockerfile 語法

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

什么是語法?

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

Dockerfile 語法示例

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

 

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

 

一個簡單的例子:

 

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

 

#p#

Dockerfile 命令

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

ADD

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

 

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

 

CMD

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

 

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

 

ENTRYPOINT

ENTRYPOINT 幫助你配置一個容器使之可執行化,如果你結合CMD命令和ENTRYPOINT命令,你可以從CMD命令中移除“application”而僅僅保留參數,參數將傳遞給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命令用于設置環境變量。這些變量以”key=value”的形式存在,并可以在容器內被腳本或者程序調用。這個機制給在容器中運行應用帶來了極大的便利。

 

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

 

EXPOSE

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

 

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

 

FROM

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

 

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

 

MAINTAINER

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

 

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

 

RUN

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

 

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

 

USER

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

 

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

 

VOLUME

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

 

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

 

WORKDIR

WORKDIR命令用于設置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 示例一:創建一個MongoDB的鏡像

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

創建一個Dockerfile

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

  1. sudo nano Dockerfile 

 

定義文件和它的目的

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

 

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

 

設置基礎鏡像

 

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

定義作者

 

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

 

設置命令與參數下載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 ##################### 

 

設置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 

 

構建鏡像

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

 

  1. sudo docker build -t my_mongodb . 

 

#p#

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

Nginx簡述

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

和上個例子一樣,我們還是從基礎鏡像開始,運用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并且替換掉默認的配置文件

 

  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自動構建Nginx容器

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

 

  1. sudo nano nginx.conf 

 

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

 

  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和配置文件來構建鏡像。

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

責任編輯:Ophira 來源: oschina.net
相關推薦

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

開源開源項目編寫文檔

2018-10-15 10:13:00

網絡拓撲結構

2019-04-19 08:04:57

程序員Dockerfile容器

2021-06-29 12:10:00

CRC校驗碼C語言

2011-06-16 17:54:30

Qt Mplayer

2009-09-03 10:52:41

C#遞歸樹

2024-03-07 11:39:24

HadolintDockerfile工具

2009-08-13 14:36:40

C#結構體構造函數

2009-08-12 16:38:35

C#讀取XML節點

2009-07-06 18:16:00

Servlet程序Cookie

2023-10-19 11:53:53

2011-07-05 17:54:43

QT Sqlite ARM

2024-05-31 12:38:32

2010-03-18 20:00:35

Java socket

2019-08-14 08:03:49

LinuxShell腳本web服務

2009-10-21 09:07:47

Linux發行版
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99视频在线免费观看 | 91精品国产综合久久婷婷香蕉 | 久久99精品国产 | h视频在线免费观看 | 国产成人麻豆免费观看 | 国产一区精品在线 | 自拍偷拍中文字幕 | 亚洲品质自拍视频网站 | 女女百合av大片一区二区三区九县 | 午夜激情国产 | 成人av在线播放 | 成人国产精品久久 | 91精品国产一区二区三区 | 麻豆国产一区二区三区四区 | 国产精品久久久久久久久久久久久久 | eeuss国产一区二区三区四区 | 国产精品国产成人国产三级 | 9191在线播放 | 老司机午夜性大片 | 午夜一区二区三区视频 | 亚洲高清视频在线观看 | 美国黄色毛片 | 久久久久国产成人精品亚洲午夜 | 一级特黄网站 | 激情欧美日韩一区二区 | av在线伊人 | 正在播放国产精品 | 天天操夜夜操 | 人人艹人人 | 好姑娘高清在线观看电影 | 亚洲一区二区三区在线 | 国产美女在线精品免费 | 色桃网| 亚洲欧美日韩中文字幕一区二区三区 | 中文字幕在线精品 | 成人av资源在线 | 激情五月婷婷 | 在线观看av不卡 | 99视频在线免费观看 | 欧美激情a∨在线视频播放 成人免费共享视频 | 亚洲欧美国产一区二区三区 |