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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

最简单的Docker镜像教程:从头基于空镜像scratch创建一个新的Docker镜像

發布時間:2023/12/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最简单的Docker镜像教程:从头基于空镜像scratch创建一个新的Docker镜像 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們在使用Dockerfile構建docker鏡像時,一種方式是使用官方預先配置好的容器鏡像。優點是我們不用從頭開始構建,節省了很多工作量,但付出的代價是需要下載很大的鏡像包。

比如我機器上docker images返回的這些基于nginx的鏡像,每個都超過了100MB,而一個簡單的Ubuntu的容器超過了200MB,如果安裝了相關的軟件,尺寸會更大。

如果我們的需求是在構建一個符合我們實際業務需求的Docker鏡像的前提下,確保鏡像尺寸盡可能的小,應該怎么做呢?

思路是使用空鏡像scratch。

新建一個文件夾,用wget下載rootfs.tar.xz壓縮包。

wget -O rootfs.tar.xz https://github.com/debuerreotype/docker-debian-artifacts/raw/b024a792c752a5c6ccc422152ab0fd7197ae8860/jessie/rootfs.tar.xz

這個將近30MB的壓縮包是個什么東東?

解壓之后看內容就知道了,包含了操作系統大部分常用命令。

wget -O nginx.conf https://github.diablo.corp/raw/slvi/docker-k8s-training/master/docker/res/nginx.conf

新建一個dockerfile文件,將下列內容粘貼進去:

FROM scratch# set the environment to honour SAP's proxy servers ENV http_proxy http://sap:8080 ENV https_proxy http://sap:8080 ENV no_proxy .sap# give yourself some credit LABEL maintainer="Jerry Wang"# add and unpack an archive that contains a Debian root filesystem ADD rootfs.tar.xz /# use the apt-get package manager to install nginx and wget RUN apt-get update && \ apt-get -y install nginx wget# use wget to download a custom website into the image RUN wget --no-check-certificate -O /usr/share/nginx/html/cheers.jpg https://github.diablo.corp/raw/slvi/docker-k8s-training/master/docker/res/cheers.jpg && \ wget --no-check-certificate -O /usr/share/nginx/html/index.html https://github.diablo.corp/raw/slvi/docker-k8s-training/master/docker/res/cheers.html# copy the custom nginx configuration into the image COPY nginx.conf /etc/nginx/nginx.conf# link nginx log files to Docker log collection facility RUN ln -sf /dev/stdout /var/log/nginx/access.log && \ ln -sf /dev/stderr /var/log/nginx/error.log# expose port 80 - the standard port for webservers EXPOSE 80# and make sure that nginx runs when a container is created CMD ["nginx", "-g", "daemon off;"]

執行命令進行鏡像的構建:

docker build -t nginx-from-scratch1.0 .

產生的日志:

最后看到鏡像成功構建的消息。

基于這個名為nginx-from-scratch的鏡像啟動一個新的nginx容器:

localhost:1083, 看到首頁,說明這個新構建的鏡像工作正常。

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

總結

以上是生活随笔為你收集整理的最简单的Docker镜像教程:从头基于空镜像scratch创建一个新的Docker镜像的全部內容,希望文章能夠幫你解決所遇到的問題。

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