怎么创建一个新的Docker镜像

发布时间:2021-08-30 22:17:08 作者:chen
来源:亿速云 阅读:124

本篇内容介绍了“怎么创建一个新的Docker镜像”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

我们在使用Dockerfile构建docker镜像时,一种方式是使用官方预先配置好的容器镜像。优点是我们不用从头开始构建,节省了很多工作量,但付出的代价是需要下载很大的镜像包。

比如我机器上docker images返回的这些基于nginx的镜像,每个都超过了100MB,而一个简单的Ubuntu的容器超过了200MB,如果安装了相关的软件,尺寸会更大。

怎么创建一个新的Docker镜像

如果我们的需求是在构建一个符合我们实际业务需求的Docker镜像的前提下,确保镜像尺寸尽可能的小,应该怎么做呢?

思路是使用空镜像scratch。

怎么创建一个新的Docker镜像

新建一个文件夹,用wget下载rootfs.tar.xz压缩包。

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

怎么创建一个新的Docker镜像

这个将近30MB的压缩包是个什么东东?

解压之后看内容就知道了,包含了操作系统大部分常用命令。

怎么创建一个新的Docker镜像

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.corp:8080
ENV https_proxy http://sap.corp:8080
ENV no_proxy .sap.corp
# 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-scratch2.0 .

怎么创建一个新的Docker镜像

产生的日志:

怎么创建一个新的Docker镜像

最后看到镜像成功构建的消息。

怎么创建一个新的Docker镜像

基于这个名为nginx-from-scratch的镜像启动一个新的nginx容器:

怎么创建一个新的Docker镜像

localhost:1083, 看到首页,说明这个新构建的镜像工作正常。

怎么创建一个新的Docker镜像

“怎么创建一个新的Docker镜像”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 怎么创建Docker镜像
  2. Docker基于现有镜像如何构建新镜像

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

docker

上一篇:怎么在docker里运行nginx服务器

下一篇:Docker镜像提交命令commit的工作原理以及使用方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》