因为docker能够进行隔离,基于docker会比chroot更加安全。这里研究了一下docker怎么使用rootfs。主要步骤如下:
制作rootfs的ext4的img镜像
通过docker import导入image
通过docker run启动这个image
进入系统后,修改并通过docker commit提交
这里制作rootfs和之前文章一样,大概步骤如下
dd if=/dev/zero of=rootfs.img bs=1k count=1024 mkfs.ext4 rootfs.img e2fsck -fy rootfs.img resize2fs rootfs.img 10G mount rootfs.img rootfs cp rootfs_file/* rootfs/ umount rootfs
mount rootfs.img rootfs cd rootfs tar -cv . | docker import - test_image 或从某个docker image里面导入 docker export -o rootfs.tar example_image docker import rootfs.tar test_image
三:启动这个镜像
docker run -it --name "`date +%y%m%d-``openssl rand -hex 2`" -p 2222:22 --hostname myOS --privileged --cap-add=sys_admin --env container=docker \ --entrypoint=/usr/lib/systemd/systemd \ --mount type=bind,source=/sys/fs/cgroup,target=/sys/fs/cgroup \ --mount type=bind,source=/sys/fs/fuse,target=/sys/fs/fuse \ --mount type=tmpfs,destination=/tmp \ --mount type=tmpfs,destination=/run \ --mount type=tmpfs,destination=/run/lock \ test_image:latest \ --unit=multi-user.target
这里参数解析如下:
--privileged 权限全开
--cap-add=sys_admin 允许执行系统管理任务
--entrypoint=/usr/lib/systemd/systemd 默认执行systemd
--env container=docker 要允许systemd(以及系列程序)能够感知到自己运行在一个容器中,systemd unit配置文件中的 ConditionVirtualization= 设置才能工作
--mount type=bind 在环境内进行mount
--unit=multi-user.target systemd运行这个target,可以更换自行的target
-p 2222:22 将容器的22端口映射到本机的2222端口,这样可以直接 ssh -p 2222 localhost登录容器
在容器中修改任何文件和内容之后,可以直接poweroff让容器处于退出状态,然后
docker commit -a "name@test.cn" -m "do something" $CONTAINER_ID test_image:v1
提交之后,通过docker images 可以看到两个images了。这样就可以用新的images进行开发了。
docker安装时,无法分配ip
解决方法:直接命令分配即可,然后手动运行dockerd测试验证
ip link add name docker0 type bridge ip addr add dev docker0 172.17.0.1/16
docker image save test_image:v1 -o rootfs.gz docker image lode -i rootfs.gz
https://github.com/docker/for-linux/issues/123#issuecomment-346546953
https://www.kernel.org/doc/html/latest/filesystems/fuse.html
https://docs.docker.com/engine/reference/commandline/import/
https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md
https://serverfault.com/questions/607769/running-systemd-inside-a-docker-container-arch-linux
https://cloud-atlas.readthedocs.io/zh_CN/latest/docker/init/docker_systemd.html