docker开发流程

By fanux | July 11, 2016

docker开发流程

注意容器构建时的信息:

e>

编译docker源码

clone moby

$ git clone https://github.com/moby/moby

创建一个分支:

$ git checkout dry-run-test

构建容器编译:

$ make BIND_DIR=. shell

运行容器:

docker run --rm -i --privileged \
-e BUILDFLAGS -e KEEPBUNDLE \
-e DOCKER_BUILD_GOGC \
-e DOCKER_BUILD_PKGS \
-e DOCKER_CLIENTONLY \
-e DOCKER_DEBUG \
-e DOCKER_EXPERIMENTAL \
-e DOCKER_GITCOMMIT \
-e DOCKER_GRAPHDRIVER=devicemapper \
-e DOCKER_INCREMENTAL_BINARY \
-e DOCKER_REMAP_ROOT -e DOCKER_STORAGE_OPTS \
-e DOCKER_USERLANDPROXY -e TESTDIRS \
-e TESTFLAGS -e TIMEOUT \
-v "home/ubuntu/repos/docker/bundles:/go/src/github.com/moby/moby/bundles" -t "docker-dev:dry-run-test" bash
root@f31fa223770f:/go/src/github.com/moby/moby#

我们启动容器时小作修改使可以很方便的在本机上改代码,在容器里构建:

docker run --rm -i --privileged \
    -e BUILDFLAGS -e KEEPBUNDLE \
    -e DOCKER_BUILD_GOGC \
    -e DOCKER_BUILD_PKGS \
    -e DOCKER_CLIENTONLY \
    -e DOCKER_DEBUG \
    -e DOCKER_EXPERIMENTAL \
    -e DOCKER_GITCOMMIT \
    -e DOCKER_GRAPHDRIVER=devicemapper \
    -e DOCKER_INCREMENTAL_BINARY \
    -e DOCKER_REMAP_ROOT -e DOCKER_STORAGE_OPTS \
    -e DOCKER_USERLANDPROXY -e TESTDIRS \
    -e TESTFLAGS -e TIMEOUT \
    -v /Users/fanux/work/src/github.com:/go/src/github.com \
    -t "docker-dev:dry-run-test" bash

容器内编译源码:

root@a8b2885ab900:/go/src/github.com/moby/moby# hack/make.sh binary
...output snipped...
bundles/1.12.0-dev already exists. Removing.

---> Making bundle: binary (in bundles/1.12.0-dev/binary)
Building: bundles/1.12.0-dev/binary/docker-1.12.0-dev
Created binary: bundles/1.12.0-dev/binary/docker-1.12.0-dev
Copying nested executables into bundles/1.12.0-dev/binary

拷贝bin文件:

root@a8b2885ab900:/go/src/github.com/moby/moby# cp bundles/1.12.0-dev/binary-client/docker* /usr/bin/
root@a8b2885ab900:/go/src/github.com/moby/moby# cp bundles/1.12.0-dev/binary-daemon/docker* /usr/bin/

启动containerd:

root@a8b2885ab900:/go/src/github.com/docker/docker# dockerd -D &
...output snipped...
DEBU[0001] Registering POST, /networks/{id:.*}/connect
DEBU[0001] Registering POST, /networks/{id:.*}/disconnect
DEBU[0001] Registering DELETE, /networks/{id:.*}
INFO[0001] API listen on /var/run/docker.sock
DEBU[0003] containerd connection state change: READY

这时你可以修改一些docker的代码了,然后重新编译即可,官方教程是修改了docker的代码,而我更感兴趣的是runc,下面就来改改runc试试。

修改runc代码

在容器内执行:

mkdir /mycontainer
cd /mycontainer
mkdir rootfs
docker export $(docker create busybox) | tar -C rootfs -xvf -
# 生成容器的配置文件config.json
docker-runc spec
docker-runc run mycontainerid

进到github.com/opencontainers/runc/run.go,修改代码:

spec, err := setupSpec(context)
fmt.Println("spec is: ", *spec)

再容器runc目录构建runc:

make && make install

这时再到mycontainer目录用我们构建的这个runc运行容器,我们打印的信息出来了。

root@7d8c68bba090:/go/src/github.com/opencontainers/mycontainer# runc run test
spec is:  {1.0.0-rc5 {linux amd64} {true {0 0} {0 0 [] } [sh] [PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin TERM=xterm] / 0xc42001a500 [{RLIMIT_NOFILE 1024 1024}] true  } {rootfs true} runc [{/proc proc proc []} {/dev tmpfs tmpfs [nosuid strictatime mode=755 size=65536k]} {/dev/pts devpts devpts [nosuid noexec newinstance ptmxmode=0666 mode=0620 gid=5]} {/dev/shm tmpfs shm [nosuid noexec nodev mode=1777 size=65536k]} {/dev/mqueue mqueue mqueue [nosuid noexec nodev]} {/sys sysfs sysfs [nosuid noexec nodev ro]} {/sys/fs/cgroup cgroup cgroup [nosuid noexec nodev relatime ro]}] <nil> map[] 0xc420084380 <nil> <nil>}
/ #
comments powered by Disqus