日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Dockerfile镜像的制作

發布時間:2025/7/25 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dockerfile镜像的制作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Dockerfile鏡像的制作


如果學習Docker,那么制作鏡像這一步肯定不能少的,別人給你的是環境,而你自己做的才是你最終需要的東西,接下來就記錄一下如何制作一個滿足自己的鏡像,我們使用docker一般就是部署我們的應用,那么我就制作一個鏡像來發布我們的應用,制作方式我們就選擇Dockerfile的方式

Dockerfile:

docker其實就像是一個腳本文件,或者你可以直接把他看成一個腳本或者就是看成一條條命令的集合,在Dockerfile文件中我們一般分為四個部分:基礎鏡像、創建者信息(可省略)、制作鏡像過程(操作鏡像指令)、啟動容器的命令

接下來我們先了解一下這四部分分別是什么?

準備:獲取基礎鏡像

我們可以從鏡像倉庫中獲取適合我們的基礎鏡像使用docker search 查詢鏡像,使用docker pull 獲取,如:我要獲取一個jdk8的鏡像那么我們

#查詢鏡像倉庫中jdk8相關的鏡像 docker search java8 #獲取名字是corvis/java8的鏡像 docker pull corvis/java8

如下圖:這個鏡像我已經下載過了所以顯示的已經存在

全部下載完成之后我們可以查一下我們的本地會有一個名字是corvis/java8的鏡像

#查詢本地鏡像 docker images

到這里我們的基礎鏡像已經獲取到了,我們把創建的Dockerfile文件放到一個單獨的目錄下,并把我們的項目打好的包也放在該目錄下,接下里我們開始寫我們的Dockerfile文件

制作的步驟

  • 指定基礎鏡像

    #指定基礎鏡像(我們所有的操作都是在這個鏡像的基礎上做的) FROM corvis/java8
  • 寫入創建者信息

    MAINTAINER SunArmy
  • 把我們的項目copy到鏡像中,并暴露端口,這里我直接用springBoot寫了一個空的項目打包test.jar,端口開的80

    #從本地copy到鏡像的/usr/local目錄下,使用ADD和COPY都可以 ADD test.jar /usr/local/ #暴露項目端口到宿主機 EXPOSE 80
  • 啟動容器我們需要執行的命令(啟動項目的命令)

    #使用CMD和ENTRYPOINT都可以 CMD ["java","-jar","/usr/local/test.jar"]

  • FROM corvis/java8 MAINTAINER SunArmy COPY test.jar /usr/local/ EXPOSE 80 CMD ["java","-jar","/usr/local/test.jar"]
  • 我們已經寫好了Dockerfile文件那么我們直接在當前目錄下執行命令來生成鏡像

    #根據Dockerfile文件制作鏡像 docker build -t test:1.0 . #查看本地鏡像 docker images

  • 至此,我們的鏡像已經做好了

    Dockerfile常用指令,語法

    1、FROM <鏡像名image>:<版本號tag>一般是Dockerfile的第一行,指定基礎鏡像2、MAINTAINET <創建者信息>指明該鏡像的創建者信息3、RUN <命令>容器中需要執行的命令,如:在容器創建之后需要在根目錄創建一個logs目錄 RUN mkdir /logs4、COPY <本地文件> <容器路徑>復制本地文件到鏡像中,本地路徑是以Dockkerfile所在目錄為根目錄 如:把test.jar復制到/usr/local目錄COPY test.jar /usr/local5、ADD <本地文件> <容器路徑>復制本地文件到鏡像中,本地路徑是以Dockkerfile所在目錄為根目錄,區別與COPY的可以從URL復制,可以直接解壓.tar.gz并把解壓之后的文件復制到鏡像 如:把test.jar復制到/usr/local目錄ADD test.jar /usr/local從URL復制到/usr/localADD URL /usr/local解壓test.tar.gz復制到鏡像/usr/localADD test.tar.gz /usr/local6、ENV <key> <value>設置環境變量,可以直接用的環境變量有如下$HOME 用戶主目錄$HOMENAME 容器主機名$PATH 容器環境變量(這個會經常用到)如:我們要把/usr/bin添加到環境變量中ENV PATH $PATH:/usr/bin7、EXPOSE [<port>...]Docker服務端暴露端口,如:我們鏡像中有8080和8081兩個端口需要暴露出去EXPOSE 8080 80818、CMD ["",""]括號中的是需要執行的命令指定啟動容器時執行的命令,每個Dockerfile只能有一條CMD指令,如果指定了多條指令,則最后一條執行(如果啟動的時候指定了命令會被啟動時指定的命令覆蓋)9、ENTRYPOINT ["",""]和CMD指令一樣,唯一不同的是即使啟動的時候指定了命令,該命令也不會被覆蓋10、VOLUME ["/data"]作用是創建在本地主機或其他容器可以掛載的數據卷,用來存放數據。11、USER username指定容器運行時的用戶名或UID,后續的RUN也會使用指定的用戶。要臨時使用管理員權限可以使用sudo。在USER命令 之前可以使用RUN命令創建需要的用戶。12、WORKDIR /path為后續的RUN CMD ENTRYPOINT指定配置工作目錄,可以使用多個WORKDIR指令,若后續指令用得是相對路徑,則會基 于之前的命令指定路徑。13.ONBUILD [INSTRUCTION]該配置指定當所創建的鏡像作為其他新建鏡像的基礎鏡像時所執行的指令。

    以上就是寫Dockerfile文件所需要的基本指指令

    寫好之后我們切入Dockerfile目錄下執行 docker build 即可制成我們自己的鏡像,如:使用當前目錄的Dockerfile文件創建鏡像并設置標簽,-t參數設置標簽
    docker build -t test:1.0 .

    Usage: docker build [OPTIONS] PATH | URL | -Build an image from a DockerfileOptions:--add-host list Add a custom host-to-IP mapping (host: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--compress Compress the build context using gzip--cpu-period int Limit the CPU CFS (Completely FairScheduler) period--cpu-quota int Limit the CPU CFS (Completely FairScheduler) quota-c, --cpu-shares int CPU shares (relative weight)--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)--disable-content-trust Skip image verification (default true)-f, --file string Name of the Dockerfile (Default is'PATH/Dockerfile')--force-rm Always remove intermediate containers--iidfile string Write the image ID to the file--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--network string Set the networking mode for the RUNinstructions during build (default "default")--no-cache Do not use cache when building the image--pull Always attempt to pull a newer version ofthe image-q, --quiet Suppress the build output and print imageID on success--rm Remove intermediate containers after asuccessful build (default true)--security-opt strings Security options--shm-size bytes Size of /dev/shm-t, --tag list Name and optionally a tag in the'name:tag' format--target string Set the target build stage to build.--ulimit ulimit Ulimit options (default [])

    把鏡像保存到本地

    docker save

    如:把test:1.0這個鏡像保存到本地

    docker save -o test_1.0.tar test:1.0

    Usage: docker build [OPTIONS] PATH | URL | -Build an image from a DockerfileOptions:--add-host list Add a custom host-to-IP mapping (host: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--compress Compress the build context using gzip--cpu-period int Limit the CPU CFS (Completely FairScheduler) period--cpu-quota int Limit the CPU CFS (Completely FairScheduler) quota-c, --cpu-shares int CPU shares (relative weight)--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)--disable-content-trust Skip image verification (default true)-f, --file string Name of the Dockerfile (Default is'PATH/Dockerfile')--force-rm Always remove intermediate containers--iidfile string Write the image ID to the file--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--network string Set the networking mode for the RUNinstructions during build (default "default")--no-cache Do not use cache when building the image--pull Always attempt to pull a newer version ofthe image-q, --quiet Suppress the build output and print imageID on success--rm Remove intermediate containers after asuccessful build (default true)--security-opt strings Security options--shm-size bytes Size of /dev/shm-t, --tag list Name and optionally a tag in the'name:tag' format--target string Set the target build stage to build.--ulimit ulimit Ulimit options (default [])C:\Users\SunArmy\Desktop\demo>docker save --helpUsage: docker save [OPTIONS] IMAGE [IMAGE...]Save one or more images to a tar archive (streamed to STDOUT by default)Options:-o, --output string Write to a file, instead of STDOUT

    轉載于:https://www.cnblogs.com/SunArmy/p/11061003.html

    總結

    以上是生活随笔為你收集整理的Dockerfile镜像的制作的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。