docker安装linux镜像制作,制作Docker镜像的两种方式

此文已由作者朱笑天授权网易云社区发布。

欢迎访问网易云社区,了解更多网易技术产品运营经验。

一、使用docker commit命令制作docker镜像

1. pull一个centos6.6的基础镜像,并运行一个docker container,然后在其中进行定制化(安装、配置服务等);[root@localhost ~]# docker pull centos:6.6

Pulling repository centos

8b44529354f3: Download complete

f1b10cd84249: Download complete

Status: Downloaded newer image for centos:6.6

[root@localhost ~]# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

centos 6.6 8b44529354f3 4 days ago 202.6 MB

centos centos6.6 8b44529354f3 4 days ago 202.6 MB

[root@localhost ~]# docker run -i -t centos:6.6 bash

[root@b42c1ba929a9 /]# ls

bin dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var

[root@b42c1ba929a9 /]# mkdir /tmp/test.txt

[root@b42c1ba929a9 /]# exit

exit

接下来,查看一下container的改动:[root@localhost ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

b42c1ba929a9 centos:6.6 "bash" About a minute ago Exited (0) 22 seconds ago berserk_mcclintock

804a56ce8008 f1b10cd84249 "/bin/echo hello" 22 minutes ago test

[root@localhost ~]# docker diff b42c1ba929a9

C /tmp

A /tmp/test.txt

C /root

A /root/.bash_history

2. 执行docker commit命令创建一个镜像:[root@localhost ~]# docker commit -m "new container" b42c1ba929a9 yuanhuan/newcontainer1

07b146e0be9e98c253122784c3837dd1604f7692e794f3601dad747d41901cd4

[root@localhost ~]# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

yuanhuan/newcontainer1 latest 07b146e0be9e 43 seconds ago 202.6 MB

centos 6.6 8b44529354f3 4 days ago 202.6 MB

centos centos6.6 8b44529354f3 4 days ago 202.6 MB

3. 登录docker,并将该镜像push到docker仓库:[root@localhost ~]# docker login

Username: yuanhuan

Password:

Email: yuanhuan_2005@126.com

Login Succeeded

[root@localhost ~]# docker push yuanhuan/newcontainer1

The push refers to a repository [yuanhuan/newcontainer1] (len: 1)

Sending image list

Pushing repository yuanhuan/newcontainer1 (1 tags)

f1b10cd84249: Image already pushed, skipping

8b44529354f3: Image already pushed, skipping

07b146e0be9e: Image successfully pushed

Pushing tag for rev [07b146e0be9e] on {https://cdn-registry-1.docker.io/v1/repositories/yuanhuan/newcontainer1/tags/latest}

成功之后,就可以登录docker页面,查看到刚刚制作的镜像了。

1540954273124073.jpg

二、使用dockerfile制作镜像

创建dockerfile文件:dockerfile.txt,内容如下:FROM centos

MAINTAINER YH, http://yuanhuan.blog.51cto.com

RUN yum install passwd openssl openssh-server -y

RUN echo '123456' | passwd --stdin root

RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''

RUN ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''

RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd

RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh

EXPOSE 22

CMD ip addr ls eth0 | awk '{print $2}' | egrep -o '([0-9]+\.){3}[0-9]+';/usr/sbin/sshd -D

简要说明:该dockerfile的主要目的是开启sshd服务,以便可以ssh登录,并且设置了root用户的密码。

然后执行下面的命令制作一个镜像:

docker build -t centos:autosshd - < dockerfile.txt

成功之后就可以用docker images查看镜像列表了。

两种方式比较:

使用commit命令比较简单,相当于docker根据container内部执行的命令自动生成了dockerfile,并进行了build,比较适合对dockerfile不熟悉的用户;

而dockerfile的方式显得稍有点复杂,但是比较适合批量处理的场景。dockerfile一次写入,多次运行。

网易云容器服务为用户提供了无服务器容器,让企业能够快速部署业务,轻松运维服务。容器服务支持弹性伸缩、垂直扩容、灰度升级、服务发现、服务编排、错误恢复及性能监测等功能。点击可免费试用

更多网易技术、产品、运营经验分享请点击。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值