dockerfile介紹 和 docker build常用命令介紹
docker使用教程相關系列 目錄

目錄
什么是 dockerFile
docker鏡像制作的工作邏輯
DockerFile的基本結構
Dockerfile格式 案例:
Build鏡像
我們之前學習,docker的鏡像都是官方給定義好的,我們可不可以 自己創造鏡像呢。
什么是 dockerfile
dockerfile,相當于是一個文檔,客戶可以基于dockerfile生成新的容器
dockerfile 是用來制作鏡像的源碼文件,是構建容器過程中的指令。
docker能夠讀取dockerfile的指定進行自動構建容器,基于dockerfile制作鏡像
每一個指令都會創建一個鏡像層,即鏡像都是多層疊加而成,因此,層越多,效率越低,創建鏡像,層越少越好。因此能在一個指令完成的動作盡量通過一個指令定義。
白話文:名為Dockerfile 的文件,里面寫了 Dockerfile的必需存在的幾點,然后build一下 他就變成了鏡像。
docker鏡像制作的工作邏輯
首先需要有一個制作鏡像的目錄,該目錄下有個文件,
名稱必須為Dockerfile,Dockerfile有指定的格式
這個是在docker的convention,如果用過C語言的makefile,應該就會了解。都是一些編程語言的約定。固定的名字,docker程序代碼中寫好的。
自己命名的話: docker build -t test -f dockerfile .
#號開頭為注釋。指定默認用大寫字母來表示,以區分指令和參數。
docker build 讀取Dockerfile是按順序依次Dockerfile里的配置,且第一條非注釋指令必須是FROM 開頭,表示基于哪個基礎鏡像來構建新鏡像。可以根據已存在的任意鏡像來制作新鏡像。
Dockerfile的基本結構
Dockerfile 一般分為四部分:
基礎鏡像信息
維護者信息
鏡像操作指令
容器啟動時執行指令
接下來詳細介紹
‘#’ 是 Dockerfile 中的注釋。
Docker以從上到下的順序運行Dockerfile的指令。為了指定基本映像,第一條指令必須是FROM。一個聲明以#字符開頭則被視為注釋。可以在Docker文件中使用RUN,CMD,FROM,EXPOSE,ENV等指令。
vim Dockerfile
Dockerfile格式 案例:
執行順序是從上到下,依次執行
Build鏡像
docker build 是一條docker的命令,用于使用 Dockerfile 創建鏡像。
Build方式需要寫一個配置文件,然后利用當前是已存在的image,按照配置文件進行調整生成新的image。
參數格式:
docker build [OPTIONS] PATH |URL| -[flags]
Options:
-t, --tag list # 鏡像名稱
-f, --fire string # 指定 Dockerfile文件位置
# Options 其他參數 docker build --help
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
# 添加自定義主機到ip的映射(主機:ip)
--build-arg list Set build-time variables
# 設置構建時變量
--cache-from strings Images to consider as cache sources
# 要考慮作為緩存源的圖像
--cgroup-parent string Optional parent cgroup for the container
# 容器的可選父cgroup
--compress Compress the build context using gzip
# 使用gzip壓縮構建上下文
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
# 限制CPU CFS(完全公平調度程序)周期
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
# 限制CPU CFS(完全公平調度程序)配額
-c, --cpu-shares int CPU shares (relative weight)
# CPU份額(相對權重)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
# 允許執行的cpu (0- 3,0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
# 允許執行的MEMs (0- 3,0,1)
--disable-content-trust Skip image verification (default true)
# 跳過圖像驗證(默認為真)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
# Dockerfile的名稱(默認為‘PATH/Dockerfile’)
--force-rm Always remove intermediate containers
# 總是移除中間容器
--iidfile string Write the image ID to the file
# 將圖像ID寫入文件
--isolation string Container isolation technology
# 容器隔離技術
--label list Set metadata for an image
# 設置圖像的元數據
-m, --memory bytes Memory limit
# 存儲容量極限 / 內存限制
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
# 交換限制等于內存加交換:'-1'以啟用無限交換
--network string Set the networking mode for the RUN instructions during build (default "default")
# 在構建期間為運行指令設置連網模式(默認為“default”)
--no-cache Do not use cache when building the image
# 在構建映像時不使用緩存
--pull Always attempt to pull a newer version of the image
# 總是嘗試拉一個較新的版本的圖像
-q, --quiet Suppress the build output and print image ID on success
# 如果成功,則禁止生成輸出并打印圖像ID
--rm Remove intermediate containers after a successful build (default true)
# 成功構建后刪除中間容器(默認為true)
--security-opt strings Security options
# 安全選項
--shm-size bytes Size of /dev/shm
# 大小 /dev/shm
-t, --tag list Name and optionally a tag in the 'name:tag' format
# 名稱和“Name:tag”格式的標記(可選)
--target string Set the target build stage to build.
# 設置要構建的目標構建階段。
--ulimit ulimit Ulimit options (default [])
# Ulimit選項(默認[])
Docker 容器
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。