1. 准备工作
1.1 准备环境
root@nicktming:~/go/src/github.com/nicktming/mydocker# git clone https://github.com/nicktming/mydocker.git
root@nicktming:~/go/src/github.com/nicktming/mydocker# git checkout code-4.2
root@nicktming:~/go/src/github.com/nicktming/mydocker# git checkout -b dev-4.3
1.2 本文最终效果
-------------------------------terminal 01--------------------------------
root@nicktming:/nicktming# ls
busybox.tar volume
root@nicktming:/nicktming# tree volume/
volume/
`-- test01.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume/test01.txt
volume
root@nicktming:/nicktming#
// 启动容器
-------------------------------terminal 02--------------------------------
root@nicktming:~/go/src/github.com/nicktming/mydocker# pwd
/root/go/src/github.com/nicktming/mydocker
root@nicktming:~/go/src/github.com/nicktming/mydocker# git clone https://github.com/nicktming/mydocker.git
root@nicktming:~/go/src/github.com/nicktming/mydocker# git checkout code-4.3
root@nicktming:~/go/src/github.com/nicktming/mydocker# go build .
root@nicktming:~/go/src/github.com/nicktming/mydocker# ./mydocker run -it -v /nicktming/volume:/containerVolume -v /nicktming/volume01:/root/volume01 /bin/sh
2019/04/08 00:12:13 volume:[/nicktming/volume:/containerVolume /nicktming/volume01:/root/volume01]
2019/04/08 00:12:13 rootPath:
2019/04/08 00:12:13 rootPath is empaty, set cmd.Dir by default: /nicktming/mnt
2019/04/08 00:12:13 current path: /nicktming/mnt.
/ # ls -l
total 48
drwxr-xr-x 2 root root 12288 Feb 14 18:58 bin
drwxr-xr-x 4 root root 4096 Apr 7 16:12 containerVolume
drwxr-xr-x 4 root root 4096 Mar 17 16:05 dev
drwxr-xr-x 3 root root 4096 Mar 17 16:05 etc
drwxr-xr-x 2 nobody nogroup 4096 Feb 14 18:58 home
dr-xr-xr-x 102 root root 0 Apr 7 16:12 proc
drwx------ 3 root root 4096 Apr 7 16:12 root
drwxr-xr-x 2 root root 4096 Mar 17 16:05 sys
drwxrwxrwt 2 root root 4096 Feb 14 18:58 tmp
drwxr-xr-x 3 root root 4096 Feb 14 18:58 usr
drwxr-xr-x 4 root root 4096 Feb 14 18:58 var
/ # cat containerVolume/test01.txt
volume
/ # ls -l /root/volume01/
total 0
/ # echo "volume01" > /root/volume01/test02.txt
/ # echo "volume again" >> /containerVolume/test01.txt
/ # exit
root@nicktming:~/go/src/github.com/nicktming/mydocker#
// 查看宿主机内容
-------------------------------terminal 01--------------------------------
root@nicktming:/nicktming# ls
busybox busybox.tar volume volume01
root@nicktming:/nicktming# cat volume/
test01.txt .wh..wh.aufs .wh..wh.orph/ .wh..wh.plnk/
root@nicktming:/nicktming# cat volume/test01.txt
volume
volume again
root@nicktming:/nicktming# tree volume01/
volume01/
`-- test02.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume01/test02.txt
volume01
root@nicktming:/nicktming#
2. 存在的问题
[mydocker]---一步步实现使用AUFS包装busybox 中在容器内增删改文件都不会保存, 那如果用户需要保存起来怎么办. 在
docker
有-v
参数可以把宿主机的目录挂到容器内, 因此本文将会实现该功能.
3. 实现-v参数 (volume)
3.1 aufs 命令行实现
在上文中已经实现了将镜像层和容器层挂载到某一个目录(比如
mnt
)中, 接下来看看如何把用户指定目录挂载到容器中, 其实只需要在mnt
中挂载一个目录到用户指定的宿主机目录即可.
root@nicktming:/nicktming# pwd
/nicktming
root@nicktming:/nicktming# ls
busybox.tar
root@nicktming:/nicktming# mkdir -p busybox mnt volume writerLayer
root@nicktming:/nicktming# tar -xvf busybox.tar -C busybox/
root@nicktming:/nicktming# ls
busybox busybox.tar mnt volume writerLayer
root@nicktming:/nicktming# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 2.7G 45G 6% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 487M 12K 487M 1% /dev
tmpfs 100M 364K 100M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 24K 497M 1% /run/shm
none 100M 0 100M 0% /run/user
volume
: 是用户需要挂载到容器中的宿主机目录. 类似于-v /nicktming/volume:/containerVolume
.
mnt
: 是容器可以看到的目录, 也就是镜像层和容器层挂载的目录.
busybox
: 镜像层
writerLayer
: 容器层
执行两个mount操作,
root@nicktming:/nicktming# mount -t aufs -o dirs=/nicktming/writerLayer:/nicktming/busybox none /nicktming/mnt
// 宿主机挂载目录对应的容器目录
root@nicktming:/nicktming# mkdir -p mnt/containerVolume
root@nicktming:/nicktming# mount -t aufs -o dirs=/nicktming/volume none /nicktming/mnt/containerVolume
root@nicktming:/nicktming# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 2.7G 45G 6% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 487M 12K 487M 1% /dev
tmpfs 100M 364K 100M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 24K 497M 1% /run/shm
none 100M 0 100M 0% /run/user
none 50G 2.7G 45G 6% /nicktming/mnt
none 50G 2.7G 45G 6% /nicktming/mnt/containerVolume
操作文件进行测试, 往用户挂载的目录中写文件.
root@nicktming:/nicktming# echo "test01" > /nicktming/mnt/containerVolume/test01.txt
root@nicktming:/nicktming# tree volume/
volume/
`-- test01.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume/test01.txt
test01
// 执行umount操作看看操作的数据是否保存到/nicktming/volume中
root@nicktming:/nicktming# umount /nicktming/mnt/containerVolume
root@nicktming:/nicktming# umount /nicktming/mnt
root@nicktming:/nicktming#
root@nicktming:/nicktming# tree
busybox/ busybox.tar mnt/ volume/ writerLayer/
root@nicktming:/nicktming# tree mnt/
mnt/
0 directories, 0 files
root@nicktming:/nicktming# tree volume/
volume/
`-- test01.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume/test01.txt
test01
3.2 实现volume操作
根据 3.1 aufs 命令行实现所示, 其实就是利用代码来实现上述命令.
3.2.1 增加-v参数
在**
command/command.go
中修改RunCommand
如下:
var RunCommand = cli.Command{
Name: "run",
Flags: []cli.Flag {
...
cli.StringFlag{
Name: "v",
Usage: "enable volume",
},
},
Action: func(c *cli.Context) error {
...
volume := c.String("v")
...
Run(command, tty, &cg, rootPath, volume)
return nil
},
}
3.2.1 增加处理volume的添加和删除方法
// 增加volume 并且mount操作
func CreateVolume(rootPath, volume string) error {
if volume != "" {
containerMntPath := rootPath + "/mnt"
hostPath := strings.Split(volume, ":")[0]
exist, _ := PathExists(hostPath)
if !exist {
if err := os.Mkdir(hostPath, 0777); err != nil {
log.Printf("mkdir %s err:%v\n", hostPath, err)
return fmt.Errorf("mkdir %s err:%v\n", hostPath, err)
}
}
mountPath := strings.Split(volume, ":")[1]
containerPath := containerMntPath + mountPath
if err := os.Mkdir(containerPath, 0777); err != nil {
log.Printf("mkdir %s err:%v\n", containerPath, err)
return fmt.Errorf("mkdir %s err:%v\n", containerPath, err)
}
dirs := "dirs=" + hostPath
if _, err := exec.Command("mount", "-t", "aufs", "-o", dirs, "none", containerPath).CombinedOutput(); err != nil {
log.Printf("mount -t aufs -o %s none %s, err:%v\n", dirs, containerPath, err)
return fmt.Errorf("mount -t aufs -o %s none %s, err:%v\n", dirs, containerPath, err)
}
}
return nil
}
// 删除volume
func ClearVolume(rootPath, volume string) {
if volume != "" {
containerMntPath := rootPath + "/mnt"
mountPath := strings.Split(volume, ":")[1]
containerPath := containerMntPath + mountPath
if _, err := exec.Command("umount", "-f", containerPath).CombinedOutput(); err != nil {
log.Printf("mount -f %s, err:%v\n", containerPath, err)
}
if err := os.RemoveAll(containerPath); err != nil {
log.Printf("remove %s, err:%v\n", containerPath, err)
}
}
}
3.2.3 使用volume相关方法
// 使用CreateVolume
func NewWorkDir(rootPath, volume string) error {
if err := CreateContainerLayer(rootPath); err != nil {
return fmt.Errorf("CreateContainerLayer(%s) error: %v.\n", rootPath, err)
}
if err := CreateMntPoint(rootPath); err != nil {
return fmt.Errorf("CreateMntPoint(%s) error: %v.\n", rootPath, err)
}
if err := SetMountPoint(rootPath); err != nil {
return fmt.Errorf("SetMountPoint(%s) error: %v.\n", rootPath, err)
}
if err := CreateVolume(rootPath, volume); err != nil {
return fmt.Errorf("CreateVolume(%s, %s) error: %v.\n", rootPath, volume, err)
}
return nil
}
// 清理工作
func ClearWorkDir(rootPath, volume string) {
ClearVolume(rootPath, volume)
ClearMountPoint(rootPath)
ClearWriterLayer(rootPath)
}
3.2.4 测试
--------------------------------terminal 01-------------------------------
root@nicktming:/nicktming# pwd
/nicktming
root@nicktming:/nicktming# ls
busybox.tar
// 启动容器
--------------------------------terminal 02-------------------------------
root@nicktming:~/go/src/github.com/nicktming/mydocker# ./mydocker run -it -v /nicktming/volume:/containerVolume /bin/sh
2019/04/07 22:09:52 volume:/nicktming/volume:/containerVolume
2019/04/07 22:09:52 rootPath:
2019/04/07 22:09:52 rootPath is empaty, set cmd.Dir by default: /nicktming/mnt
2019/04/07 22:09:52 current path: /nicktming/mnt.
/ # ls
bin dev home root tmp var
containerVolume etc proc sys usr
/ # ls -l
total 48
drwxr-xr-x 2 root root 12288 Feb 14 18:58 bin
drwxr-xr-x 4 root root 4096 Apr 7 14:09 containerVolume
drwxr-xr-x 4 root root 4096 Mar 17 16:05 dev
drwxr-xr-x 3 root root 4096 Mar 17 16:05 etc
drwxr-xr-x 2 nobody nogroup 4096 Feb 14 18:58 home
dr-xr-xr-x 105 root root 0 Apr 7 14:09 proc
drwx------ 2 root root 4096 Apr 7 14:10 root
drwxr-xr-x 2 root root 4096 Mar 17 16:05 sys
drwxrwxrwt 2 root root 4096 Feb 14 18:58 tmp
drwxr-xr-x 3 root root 4096 Feb 14 18:58 usr
drwxr-xr-x 4 root root 4096 Feb 14 18:58 var
/ # echo "test01" > containerVolume/test01.txt
/ # cat containerVolume/test01.txt
test01
// 查看宿主机内容
--------------------------------terminal 01-------------------------------
root@nicktming:/nicktming# ls
busybox busybox.tar mnt volume writerLayer
root@nicktming:/nicktming# df -h
df: ‘/tmp/tmpMZmTH1’: No such file or directory
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 2.7G 45G 6% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 487M 12K 487M 1% /dev
tmpfs 100M 372K 100M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 24K 497M 1% /run/shm
none 100M 0 100M 0% /run/user
none 50G 2.7G 45G 6% /nicktming/mnt
none 50G 2.7G 45G 6% /nicktming/mnt/containerVolume
root@nicktming:/nicktming#
// 退出容器
--------------------------------terminal 02-------------------------------
/ # exit
root@nicktming:~/go/src/github.com/nicktming/mydocker#
// 查看宿主机内容
--------------------------------terminal 01-------------------------------
root@nicktming:/nicktming# ls
busybox busybox.tar volume
root@nicktming:/nicktming# tree volume/
volume/
`-- test01.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume/test01.txt
test01
root@nicktming:/nicktming#
可以看到宿主机的内容已经保存下来. 接下来利用该
volume
再次测试.
--------------------------------terminal 02-------------------------------
root@nicktming:~/go/src/github.com/nicktming/mydocker# ./mydocker run -it -v /nicktming/volume:/root/containerVolume /bin/sh
2019/04/07 22:20:30 volume:/nicktming/volume:/root/containerVolume
2019/04/07 22:20:30 rootPath:
2019/04/07 22:20:30 rootPath is empaty, set cmd.Dir by default: /nicktming/mnt
2019/04/07 22:20:30 current path: /nicktming/mnt.
/ # ls -l
total 44
drwxr-xr-x 2 root root 12288 Feb 14 18:58 bin
drwxr-xr-x 4 root root 4096 Mar 17 16:05 dev
drwxr-xr-x 3 root root 4096 Mar 17 16:05 etc
drwxr-xr-x 2 nobody nogroup 4096 Feb 14 18:58 home
dr-xr-xr-x 100 root root 0 Apr 7 14:20 proc
drwx------ 3 root root 4096 Apr 7 14:20 root
drwxr-xr-x 2 root root 4096 Mar 17 16:05 sys
drwxrwxrwt 2 root root 4096 Feb 14 18:58 tmp
drwxr-xr-x 3 root root 4096 Feb 14 18:58 usr
drwxr-xr-x 4 root root 4096 Feb 14 18:58 var
/ # ls -l /root
total 4
drwxr-xr-x 4 root root 4096 Apr 7 14:20 containerVolume
/ # ls -l /root/containerVolume/
total 4
-rw-r--r-- 1 root root 7 Apr 7 14:10 test01.txt
/ # echo "\ntest01 again\n" >> /root/containerVolume/test01.txt
/ # echo "test02" > /root/containerVolume/test02.txt
/ # exit
root@nicktming:~/go/src/github.com/nicktming/mydocker#
// 查看宿主机
--------------------------------terminal 01-------------------------------
root@nicktming:/nicktming# pwd
/nicktming
root@nicktming:/nicktming# ls
busybox busybox.tar volume
root@nicktming:/nicktming# tree volume/
volume/
|-- test01.txt
`-- test02.txt
0 directories, 2 files
root@nicktming:/nicktming# cat volume/test01.txt
test01
\ntest01 again\n
root@nicktming:/nicktming# cat volume/test02.txt
test02
root@nicktming:/nicktming#
4. 实现多个-v参数
第3部分已经实现了单个
-v
参数的操作, 此处将实现多个-v
操作.
4.1 修改RunCommand参数
var RunCommand = cli.Command{
Name: "run",
Flags: []cli.Flag {
...
cli.StringSliceFlag{
Name: "v",
Usage: "enable volume",
},
/*
cli.StringFlag{
Name: "v",
Usage: "enable volume",
},
*/
},
Action: func(c *cli.Context) error {
...
volumes := c.StringSlice("v")
...
Run(command, tty, &cg, rootPath, volumes)
return nil
},
}
4.2 修改NewWorkDir 和 ClearWorkDir
改变参数将
volume string
变为volumes []string
外层加一个循环即可.
func NewWorkDir(rootPath string, volumes []string) error {
if err := CreateContainerLayer(rootPath); err != nil {
return fmt.Errorf("CreateContainerLayer(%s) error: %v.\n", rootPath, err)
}
if err := CreateMntPoint(rootPath); err != nil {
return fmt.Errorf("CreateMntPoint(%s) error: %v.\n", rootPath, err)
}
if err := SetMountPoint(rootPath); err != nil {
return fmt.Errorf("SetMountPoint(%s) error: %v.\n", rootPath, err)
}
for _, volume := range volumes {
if err := CreateVolume(rootPath, volume); err != nil {
return fmt.Errorf("CreateVolume(%s, %s) error: %v.\n", rootPath, volume, err)
}
}
return nil
}
func ClearWorkDir(rootPath string, volumes []string) {
for _, volume := range volumes {
ClearVolume(rootPath, volume)
}
ClearMountPoint(rootPath)
ClearWriterLayer(rootPath)
}
4.2 修改Run方法
因为
NewWorkDir
和ClearWorkDir
已经修改, 所以调用此两个方法的command/run.go
中的Run
方法
func Run(command string, tty bool, cg *cgroups.CroupManger, rootPath string, volumes []string) {
...
log.Printf("volume:%s\n", volumes)
newRootPath := getRootPath(rootPath)
cmd.Dir = newRootPath + "/busybox"
if err := NewWorkDir(newRootPath, volumes); err == nil {
cmd.Dir = newRootPath + "/mnt"
}
defer ClearWorkDir(newRootPath, volumes)
...
}
4.3 测试
------------------------------terminal 01---------------------------------
root@nicktming:/nicktming# pwd
/nicktming
root@nicktming:/nicktming# ls
busybox busybox.tar volume
root@nicktming:/nicktming#
// 创建容器
------------------------------terminal 02---------------------------------
root@nicktming:~/go/src/github.com/nicktming/mydocker# ./mydocker run -it -v /nicktming/volume01:/containerVolume01 -v /nicktming/volume02:/containerVolume02 /bin/sh
2019/04/07 23:18:14 volume:[/nicktming/volume01:/containerVolume01 /nicktming/volume02:/containerVolume02]
2019/04/07 23:18:14 rootPath:
2019/04/07 23:18:14 rootPath is empaty, set cmd.Dir by default: /nicktming/mnt
2019/04/07 23:18:14 current path: /nicktming/mnt.
/ # ls -l
total 52
drwxr-xr-x 2 root root 12288 Feb 14 18:58 bin
drwxr-xr-x 4 root root 4096 Apr 7 15:18 containerVolume01
drwxr-xr-x 4 root root 4096 Apr 7 15:18 containerVolume02
drwxr-xr-x 4 root root 4096 Mar 17 16:05 dev
drwxr-xr-x 3 root root 4096 Mar 17 16:05 etc
drwxr-xr-x 2 nobody nogroup 4096 Feb 14 18:58 home
dr-xr-xr-x 98 root root 0 Apr 7 15:18 proc
drwx------ 2 root root 4096 Apr 7 15:18 root
drwxr-xr-x 2 root root 4096 Mar 17 16:05 sys
drwxrwxrwt 2 root root 4096 Feb 14 18:58 tmp
drwxr-xr-x 3 root root 4096 Feb 14 18:58 usr
drwxr-xr-x 4 root root 4096 Feb 14 18:58 var
/ # echo "containerVolume01" > containerVolume01/test001.txt
/ # echo "containerVolume02" > containerVolume02/test002.txt
/ # exit
root@nicktming:~/go/src/github.com/nicktming/mydocker#
// 查看宿主机的内容
------------------------------terminal 01---------------------------------
root@nicktming:/nicktming# pwd
/nicktming
root@nicktming:/nicktming# ls
busybox busybox.tar volume volume01 volume02
root@nicktming:/nicktming# tree volume01
volume01
`-- test001.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume01/test001.txt
containerVolume01
root@nicktming:/nicktming# tree volume02/
volume02/
`-- test002.txt
0 directories, 1 file
root@nicktming:/nicktming# cat volume02/test002.txt
containerVolume02
root@nicktming:/nicktming#
5. 时序图
6. 参考
1. 自己动手写docker.(基本参考此书,加入一些自己的理解,加深对
docker
的理解)
7. 全部内容
1. [mydocker]---环境说明
2. [mydocker]---urfave cli 理解
3. [mydocker]---Linux Namespace
4. [mydocker]---Linux Cgroup
5. [mydocker]---构造容器01-实现run命令
6. [mydocker]---构造容器02-实现资源限制01
7. [mydocker]---构造容器02-实现资源限制02
8. [mydocker]---构造容器03-实现增加管道
9. [mydocker]---通过例子理解存储驱动AUFS
10. [mydocker]---通过例子理解chroot 和 pivot_root
11. [mydocker]---一步步实现使用busybox创建容器
12. [mydocker]---一步步实现使用AUFS包装busybox
13. [mydocker]---一步步实现volume操作
14. [mydocker]---实现保存镜像
15. [mydocker]---实现容器的后台运行
16. [mydocker]---实现查看运行中容器
17. [mydocker]---实现查看容器日志
18. [mydocker]---实现进入容器Namespace
19. [mydocker]---实现停止容器
20. [mydocker]---实现删除容器
21. [mydocker]---实现容器层隔离
22. [mydocker]---实现通过容器制作镜像
23. [mydocker]---实现cp操作
24. [mydocker]---实现容器指定环境变量
25. [mydocker]---网际协议IP
26. [mydocker]---网络虚拟设备veth bridge iptables
27. [mydocker]---docker的四种网络模型与原理实现(1)
28. [mydocker]---docker的四种网络模型与原理实现(2)
29. [mydocker]---容器地址分配
30. [mydocker]---网络net/netlink api 使用解析
31. [mydocker]---网络实现
32. [mydocker]---网络实现测试