Dockerfile鏡像的製做

Dockerfile鏡像的製做


若是學習Docker,那麼製做鏡像這一步確定不能少的,別人給你的是環境,而你本身作的纔是你最終須要的東西,接下來就記錄一下如何製做一個知足本身的鏡像,咱們使用docker通常就是部署咱們的應用,那麼我就製做一個鏡像來發布咱們的應用,製做方式咱們就選擇Dockerfile的方式java

Dockerfile:python

docker其實就像是一個腳本文件,或者你能夠直接把他當作一個腳本或者就是當作一條條命令的集合,在Dockerfile文件中咱們通常分爲四個部分:基礎鏡像、建立者信息(可省略)、製做鏡像過程(操做鏡像指令)、啓動容器的命令spring

接下來咱們先了解一下這四部分分別是什麼?docker

準備:獲取基礎鏡像app

咱們能夠從鏡像倉庫中獲取適合咱們的基礎鏡像使用docker search <鏡像名> 查詢鏡像,使用docker pull <鏡像名> 獲取,如:我要獲取一個jdk8的鏡像那麼咱們 ide

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

以下圖:這個鏡像我已經下載過了因此顯示的已經存在學習

所有下載完成以後咱們能夠查一下咱們的本地會有一個名字是corvis/java8的鏡像ui

#查詢本地鏡像
docker images

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

製做的步驟orm

  1. 指定基礎鏡像

    #指定基礎鏡像(咱們全部的操做都是在這個鏡像的基礎上作的)
    FROM corvis/java8
  2. 寫入建立者信息

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

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

    #使用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"]
  1. 咱們已經寫好了Dockerfile文件那麼咱們直接在當前目錄下執行命令來生成鏡像

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

至此,咱們的鏡像已經作好了

Dockerfile經常使用指令,語法

一、FROM <鏡像名image>:<版本號tag>
   通常是Dockerfile的第一行,指定基礎鏡像
   
二、MAINTAINET <建立者信息>
   指明該鏡像的建立者信息
   
三、RUN <命令>
   容器中須要執行的命令,如:在容器建立以後須要在根目錄建立一個logs目錄 
   RUN mkdir /logs
   
四、COPY <本地文件> <容器路徑>
   複製本地文件到鏡像中,本地路徑是以Dockkerfile所在目錄爲根目錄 如:把test.jar複製到/usr/local目錄
   COPY test.jar /usr/local
   
五、ADD <本地文件> <容器路徑>
   複製本地文件到鏡像中,本地路徑是以Dockkerfile所在目錄爲根目錄,區別與COPY的能夠從URL複製,能夠直接解壓.tar.gz並把解壓以後的文件複製到鏡像 
   如:把test.jar複製到/usr/local目錄
   ADD test.jar /usr/local
   從URL複製到/usr/local
   ADD URL /usr/local
   解壓test.tar.gz複製到鏡像/usr/local
   ADD test.tar.gz /usr/local
   
六、ENV <key> <value>
   設置環境變量,能夠直接用的環境變量有以下
   $HOME 用戶主目錄
   $HOMENAME 容器主機名
   $PATH 容器環境變量(這個會常常用到)
   如:咱們要把/usr/bin添加到環境變量中
   ENV PATH $PATH:/usr/bin
   
七、EXPOSE [<port>...]
   Docker服務端暴露端口,如:咱們鏡像中有8080和8081兩個端口須要暴露出去
   EXPOSE 8080 8081
   
八、CMD ["",""]
   括號中的是須要執行的命令
   指定啓動容器時執行的命令,每一個Dockerfile只能有一條CMD指令,若是指定了多條指令,則最後一條執行(若是啓動的時候指定了命令會被啓動時指定的命令覆蓋)
  
九、ENTRYPOINT ["",""]
   和CMD指令同樣,惟一不一樣的是即便啓動的時候指定了命令,該命令也不會被覆蓋
   
十、VOLUME ["/data"]
   做用是建立在本地主機或其餘容器能夠掛載的數據卷,用來存放數據。

十一、USER username
   指定容器運行時的用戶名或UID,後續的RUN也會使用指定的用戶。要臨時使用管理員權限可使用sudo。在USER命令    以前可使用RUN命令建立須要的用戶。
   
十二、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 Dockerfile

Options:
      --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 Fair
                                Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair
                                Scheduler) 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 RUN
                                instructions during build (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
      --rm                      Remove intermediate containers after a
                                successful 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 Dockerfile

Options:
      --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 Fair
                                Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair
                                Scheduler) 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 RUN
                                instructions during build (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
      --rm                      Remove intermediate containers after a
                                successful 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 --help

Usage:  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
相關文章
相關標籤/搜索