服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - Docker Base Image自己创建具体实现

Docker Base Image自己创建具体实现

2020-12-15 22:25Docker教程网 服务器知识

这篇文章主要介绍了Docker Base Image创建具体实现的相关资料,这里提供了详细的具体步骤,需要的朋友可以参考下

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。*

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

三. 修改网络配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
修改网络配置 /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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
安装 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

五. 创建基础镜像

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
替换变量
/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 常用命令

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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>

七. 导到本地镜像库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
回去宿主机,把虚拟机中导出的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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

延伸 · 阅读

精彩推荐