docker base image 創(chuàng)建
本著學(xué)習(xí)的態(tài)度,想了解創(chuàng)建 docker base image的方法,在官網(wǎng),在百度,在相關(guān)論壇,查找相關(guān)資料,這里記錄下實(shí)現(xiàn)的步驟,
一. 環(huán)境
宿主機(jī)操作系統(tǒng): os x ,需要安裝virtualbox ;
容器環(huán)境:centos7
二. 用virtualbox 安裝系統(tǒng),這里以centos 7為例(centos-7-x86_64-minimal-1503-01.iso)
創(chuàng)建虛擬機(jī),并安裝centos7,以下記錄了詳細(xì)安裝過程。
* 安裝過程簡(jiǎn)單,我這里網(wǎng)速太慢了,就不上圖了,有需要的朋友留個(gè)郵箱,我發(fā)pdf。*
安裝完后,重啟后進(jìn)入系統(tǒng)。
三. 修改網(wǎng)絡(luò)配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
修改網(wǎng)絡(luò)配置 /etc/sysconfig/network-script/ifcfg-enp0s3 。 刪除 uuid,hwaddr ; 修改onboot=no 為 onboot= yes , 然后保存 。 $ ifconfig cannot find a valid baseurl for repo: base /7/x86_6 重啟網(wǎng)絡(luò)接口: [root@centos7 ~] #ifdown enps03 [root@centos7 ~] #ifup enps03 $ yum install ifconfig 提示:nothing to do 通過” yum provides” 命令列出那個(gè)包提供 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 啟動(dòng) docker [root@centos7 yum.repos.d] # service docker start |
五. 創(chuàng)建基礎(chǔ)鏡像
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 創(chuàng)建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 文件中內(nèi)容粘貼進(jìn)去 [root@centos7 scripts] # chmod +x createimage.sh 創(chuàng)建image [root@centos7 scripts] # ./createimage.sh centos7base 創(chuàng)建成功后查看,并把當(dāng)前運(yùn)行的容器刪除掉 [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 導(dǎo)出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 > :用來停止運(yùn)行中的容器,同時(shí)你還可以用 docker start <container id >:用來啟動(dòng)一個(gè)已經(jīng)停止的容器。 docker restart <container id >:可以重啟一個(gè)運(yùn)行中的容器。這就相當(dāng)于對(duì)一個(gè)容器先進(jìn)行stop再start。 docker attach <container id > :關(guān)聯(lián)到一個(gè)正在運(yùn)行的容器 刪除鏡像: docker rmi <image id > docker rmi 2db1e85f26ba 刪除容器:docker rm <container id > docker rm c3bfb652a491 查看正在運(yùn)行的容器 docker ps -all 停止容器: exit 重新進(jìn)入窗口 docker start <container id > docker attach <container id > 暫時(shí)退出容器 ctrl + p 然后 ctrl + q 重新入進(jìn): docker attach <container id > 將容器保存成鏡像: docker commit <container id > <name>:<tag> |
七. 導(dǎo)到本地鏡像庫(kù)
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
|
回去宿主機(jī),把虛擬機(jī)中導(dǎo)出的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 啟動(dòng)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的就是剛剛導(dǎo)入的,把tag改個(gè)名字: 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 運(yùn)行容器: 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) |
八. 發(fā)布鏡像到docker hub
前提是先注冊(cè)一個(gè)帳號(hào):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
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!