Docker Base Image如何创建

发布时间:2022-05-26 16:18:47 作者:iii
来源:亿速云 阅读:235

本篇内容主要讲解“Docker Base Image如何创建”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Docker Base Image如何创建”吧!

一. 环境

宿主机操作系统: os x  ,需要安装virtualbox  ;
容器环境:centos7

二. 用virtualbox 安装系统,这里以centos 7为例(centos-7-x86_64-minimal-1503-01.iso)

创建虚拟机,并安装centos7,以下记录了详细安装过程。

Docker Base Image如何创建

Docker Base Image如何创建

* 安装过程简单,我这里网速太慢了,就不上图了,有需要的朋友留个邮箱,我发pdf。*

安装完后,重启后进入系统。

三. 修改网络配置

修改网络配置 /etc/sysconfig/network-script/ifcfg-enp0s3。
删除 uuid,hwaddr ;
修改onboot=no 为 onboot=yes , 然后保存 。

$ ifconfig 
cannot find a valid baseurl for repo: base/7/x86_6

重启网络接口:
[root@centos7 ~]#ifdown enps03
[root@centos7 ~]#ifup enps03

$ yum install ifconfig 
 提示:nothing to do
 通过” yum provides” 命令列出那个包提供ifconfig命令
$ yum provides ifconfig  
// 安装
$ yum -y install net-tools

四. 安装docker

安装 epel (参考:http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/ )
[root@centos7 ~]# rpm -uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

修改/etc/yum.repos.d/centos-base.repo 把enabled=0 改成enabled=1
vim /etc/yum.repos.d/centos-base.repo
[centosplus]
enabled=1

安装 docker
[root@centos7 yum.repos.d]# yum install docker-io


启动 docker 
[root@centos7 yum.repos.d]# service docker start

五. 创建基础镜像

替换变量
把 /etc/yum.repos.d/centos-base.repo 文件中
$releasever 替换成 7
$basearch 替换成 x86_64

快速替换方法:
vim /etc/yum.repos.d/centos-base.repo
:%s/$releasever/7/g
:%s/$basearch/x86_64/g

创建docker image 生成脚本
[root@centos7 yum.repos.d]# cd /root
[root@centos7 ~]# mkdir scripts
[root@centos7 ~]# cd scripts/
[root@centos7 scripts]# vim createimage.sh
然后把 https://github.com/docker/docker/blob/master/contrib/mkimage-yum.sh 文件中内容粘贴进去

[root@centos7 scripts]# chmod +x createimage.sh

创建image
[root@centos7 scripts]# ./createimage.sh centos7base

创建成功后查看,并把当前运行的容器删除掉
[root@centos7 tmp]# docker images
repository     tag         image id      created       virtual size
centos7base     7.1.1503      112ee00c2cbc    8 minutes ago    227.7 mb

[root@centos7 tmp]# docker ps -all
container id    image         command       created       status           ports        names
752b9d49a079    centos7base:7.1.1503  "echo success"   10 minutes ago   exited (0) 10 minutes ago            mad_saha

[root@centos7 tmp]# docker rm 752b9d49a079
752b9d49a079

[root@centos7 tmp]# docker ps -all
container id    image        command       created       status       ports        names

导出image
[root@centos7 tmp]# cd /tmp/
[root@centos7 tmp]# docker images
repository     tag         image id      created       virtual size
centos7base     7.1.1503      112ee00c2cbc    14 minutes ago   227.7 mb

[root@centos7 tmp]# docker save 112ee00c2cbc > /tmp/centos7base.tar

[root@centos7 tmp]# ls
centos7base.tar ks-script-l8tdo5 yum.log
[root@centos7 tmp]#

六. docker 常用命令

docker stop <container id> :用来停止运行中的容器,同时你还可以用
docker start <container id>:用来启动一个已经停止的容器。
docker restart <container id>:可以重启一个运行中的容器。这就相当于对一个容器先进行stop再start。
docker attach <container id> :关联到一个正在运行的容器

删除镜像: docker rmi <image id>
  docker rmi 2db1e85f26ba

删除容器:docker rm <container id>
  docker rm c3bfb652a491

查看正在运行的容器
  docker ps -all

停止容器:
  exit

重新进入窗口
  docker start <container id>
  docker attach <container id>

暂时退出容器
  ctrl + p 然后 ctrl + q
重新入进:
  docker attach <container id>

将容器保存成镜像:
  docker commit <container id> <name>:<tag>

七. 导到本地镜像库

回去宿主机,把虚拟机中导出的image拷到本地
hr:~ hr$ mkdir -p docker/images
hr:~ hr$ cd docker/images/
hr:images hr$ scp root@192.168.1.4:/tmp/centos7base.tar .
hr:images hr$ ls -lah
total 469392
drwxr-xr-x 3 hr staff  102b 12 5 21:08 .
drwxr-xr-x 3 hr staff  102b 12 5 21:05 ..
-rw-r--r-- 1 hr staff  229m 12 5 21:08 centos7base.tar

启动docker quick start terminal



加载image 包到docker image
hr:images hr$ docker load < /users/hr/docker/images/centos7base.tar

hr:images hr$ docker images
repository     tag         image id      created       virtual size
<none>       <none>       112ee00c2cbc    29 minutes ago   227.7 mb
hello-world     latest       975b84d108f1    7 weeks ago     960 b

tar 等于none的就是刚刚导入的,把tag改个名字:
hr:images hr$ docker tag 112ee00c2cbc centos7base:7.1
hr:images hr$ docker images
repository     tag         image id      created       virtual size
centos7base     7.1         112ee00c2cbc    33 minutes ago   227.7 mb
hello-world     latest       975b84d108f1    7 weeks ago     960 b


运行容器:
hr:images hr$ docker run -i -t 112ee00c2cbc /bin/bash
[root@e948acae7b42 /]# hostname
e948acae7b42

[root@e948acae7b42 /]# cat /etc/redhat-release 
centos linux release 7.1.1503 (core)

八. 发布镜像到docker hub

前提是先注册一个帐号:https://hub.docker.com/

 1. 登录

       docker login —username=<用户名> —email=<邮箱地址>

 2. 按docker repository 要求修改镜像tag

    docker tag <image id>  用户名/镜像名/tag
    docker tag 112ee00c2cbc honor/centos7base:7.1

 3. 上传

    docker push honor/centos7base

到此,相信大家对“Docker Base Image如何创建”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Docker系列3:Docker镜像详解
  2. Docker怎么搭建Nginx图片服务器的

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

docker

上一篇:docker快速入门实例分析

下一篇:Docker的volume如何使用

相关阅读

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

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