{"id":330,"date":"2019-03-11T09:05:09","date_gmt":"2019-03-11T09:05:09","guid":{"rendered":"http:\/\/www.hardwarehacker.co.uk\/blog\/?p=330"},"modified":"2021-04-07T08:29:56","modified_gmt":"2021-04-07T08:29:56","slug":"docker-part-2-our-use","status":"publish","type":"post","link":"https:\/\/www.hardwarehacker.co.uk\/blog\/docker-part-2-our-use\/","title":{"rendered":"Docker part 2 &#8211; our use"},"content":{"rendered":"<p>For us, using Docker means:<\/p>\n<ul>\n<li>we can build the code on our fast laptops and only deploy the built code to the robot&#8217;s Pi.<\/li>\n<li>The deployed container works just the same on my Pi and on Shaun&#8217;s Pi.<\/li>\n<li>We can package our build toolchain so that that too &#8220;just works&#8221; on my laptop and Shaun&#8217;s laptop.<\/li>\n<li>The robot code and build toolchain can be pushed to the cloud for easy sharing between us<\/li>\n<li>If we have to rebuild an SD card on the day, it should be easy.<\/li>\n<li>We don&#8217;t have to install OpenCV ourselves (someone else has already done the hard bit for us)!<\/li>\n<\/ul>\n<p>So how do we actually get these benefits?\u00a0 You define a docker container with a Dockerfile.\u00a0 This is a text file which has a few commands used to set up the contents of the container.\u00a0 Our build container (more on that in a moment) has this dockerfile:<\/p>\n<pre># Start with a container that's already set up with OpenCV\r\n# and do the builds in there.\r\n\r\nFROM sgtwilko\/rpi-raspbian-opencv:stretch-latest\r\n\r\nRUN apt update\r\nRUN apt install make gcc\r\nRUN apt install wget\r\nRUN wget https:\/\/dl.google.com\/go\/go1.10.linux-armv6l.tar.gz\r\nRUN tar -C \/usr\/local -xzf go*.tar.gz\r\n\r\nENV PATH=$PATH:\/usr\/local\/go\/bin\r\nENV GOROOT=\/usr\/local\/go\/\r\nENV GOPATH=\/go\/\r\nRUN apt install git\r\n\r\nRUN mkdir -p $GOPATH\/src\/gocv.io\/x\/ &amp;&amp; \\\r\n    cd $GOPATH\/src\/gocv.io\/x\/ &amp;&amp; \\\r\n    git clone https:\/\/github.com\/fasaxc\/gocv.git\r\n\r\n# Pre-build gocv to cache the package in this layer. That\r\n# stops expensive gocv builds when we're compiling the controller.\r\nRUN bash -c \"cd $GOPATH\/src\/gocv.io\/x\/gocv &amp;&amp; \\\r\n             source .\/env.sh &amp;&amp; \\\r\n             go build -v gocv.io\/x\/gocv\"\r\n\r\nRUN bash -c \"cd $GOPATH\/src\/gocv.io\/x\/gocv &amp;&amp; \\\r\n             source .\/env.sh &amp;&amp; \\\r\n             go build -v .\/cmd\/saveimage\/main.go\"\r\n\r\n# Add the propeller IDE tools so we can extract the propman tool.\r\nRUN wget https:\/\/github.com\/parallaxinc\/PropellerIDE\/releases\/download\/0.38.5\/propelleride-0.38.5-armhf.deb\r\nRUN sh -c \"dpkg -i propelleride-0.38.5-armhf.deb || true\" &amp;&amp; \\\r\n    apt-get install -y -f &amp;&amp; \\\r\n    apt-get clean -y\r\n\r\nRUN apt-get install libasound2-dev libasound2 libasound2-plugins\r\n\r\n# Pre-build the ToF libraries\r\n\r\nCOPY VL53L0X_1.0.2 $GOPATH\/src\/github.com\/tigerbot-team\/tigerbot\/VL53L0X_1.0.2\r\nCOPY VL53L0X_rasp $GOPATH\/src\/github.com\/tigerbot-team\/tigerbot\/VL53L0X_rasp\r\nWORKDIR $GOPATH\/src\/github.com\/tigerbot-team\/tigerbot\/VL53L0X_rasp\r\nRUN API_DIR=..\/VL53L0X_1.0.2 make all examples\r\n\r\nRUN mkdir -p $GOPATH\/src\/github.com\/tigerbot-team\/tigerbot\/go-controller\r\nWORKDIR $GOPATH\/src\/github.com\/tigerbot-team\/tigerbot\/go-controller<\/pre>\n<p>This breaks down as:<\/p>\n<ul>\n<li>start with the docker container by the <code>sgtwilko<\/code> organisation called <code>rpi-raspbian-opencv<\/code> with the version <code>stretch-latest<\/code> (this gets us the latest version of raspbian with opencv pre-installed).<\/li>\n<li>Run apt-get to install compilation tools.<\/li>\n<li>Set some environment variables<\/li>\n<li><code>git clone<\/code> our fork of the <code>gocv<\/code> repo<\/li>\n<li>Pre-build gocv<\/li>\n<li>Install the propeller IDE to get the <code>propman<\/code> tool (to flash the propeller with)<\/li>\n<li>Prebuild the VL53L0X libraries<\/li>\n<li>Create the directory for the go-controller code to be mounted into<\/li>\n<li>Set the working directory to be where the go-controller code is mounted in.<\/li>\n<\/ul>\n<p>A note about layers and caching: docker containers build in layers &#8211; docker caches container images at each command in the build.\u00a0 If you rebuild a container, it will start from the latest container image that hasn&#8217;t changed.\u00a0 So it pays to put the stuff that you won&#8217;t change early in the Dockerfile (like our build of OpenCV).<\/p>\n<p>We use 2 different containers in our robot &#8211; a build container (above) and a deploy container.\u00a0 The deploy container Dockerfile looks like this:<\/p>\n<pre># Start with a container that's already set up with OpenCV\r\n# and do the builds in there.\r\n\r\nFROM tigerbot\/go-controller-phase-1:latest as build\r\n\r\nCOPY go-controller\/controller \/go\/src\/github.com\/tigerbot-team\/tigerbot\/go-controller\/controller\r\nCOPY go-controller\/copy-libs \/go\/src\/github.com\/tigerbot-team\/tigerbot\/go-controller\/copy-libs\r\n\r\nWORKDIR $GOPATH\/src\/github.com\/tigerbot-team\/tigerbot\/go-controller\r\n\r\n# Copy the shared libraries that the controller uses to a designated\r\n# directory so that they're easy to find in the next phase.\r\nRUN bash -c \"source \/go\/src\/gocv.io\/x\/gocv\/env.sh &amp;&amp; \\\r\n.\/copy-libs\"\r\n\r\n# Now build the container image that we actually ship by copying\r\n# across only the relevant files. We start with alpins since it's\r\n# nice and small to start with but we'll be throwing in a lot\r\n# of glibc-linked binaries so the resulting system will be a bit\r\n# of a hybrid.\r\n\r\nFROM resin\/raspberry-pi-alpine:latest\r\n\r\nRUN apk --no-cache add util-linux strace\r\n\r\nRUN mkdir -p \/usr\/local\/lib\r\nCOPY --from=build \/usr\/bin\/propman \/usr\/bin\/propman\r\nCOPY --from=build \/lib\/ld-linux-armhf.so* \/lib\r\nCOPY --from=build \/controller-libs\/* \/usr\/local\/lib\/\r\nCOPY --from=build \/usr\/share\/alsa \/usr\/share\/alsa\r\nCOPY --from=build \/go\/src\/github.com\/tigerbot-team\/tigerbot\/VL53L0X_rasp\/bin\/* \/usr\/local\/bin\/\r\nCOPY go-controller\/sounds \/sounds\r\nCOPY --from=build \/go\/src\/github.com\/tigerbot-team\/tigerbot\/go-controller\/controller \/controller\r\nCOPY metabotspin\/mb3.binary \/mb3.binary\r\nENV LD_LIBRARY_PATH=\/usr\/local\/lib\r\n\r\nENTRYPOINT []\r\nCMD \/controller<\/pre>\n<p>Which breaks down like this:<\/p>\n<ul>\n<li>Grab the build container contents.<\/li>\n<li>Starting with the <code>raspberry-pi-alpine<\/code>\u00a0container with tag <code>latest\u00a0<\/code>from the <code>resin<\/code>\u00a0organisation (a very stripped down linux distribution &#8211; the whole OS is 18MB)<\/li>\n<li>Install <code>util-linux<\/code> and <code>strace<\/code> binaries<\/li>\n<li>Copy built artifacts from the build container into this container<\/li>\n<li>wipe the ENTRYPOINT (the command run when the container starts)<\/li>\n<li>set the command to run when the container starts to <code>\/controller<\/code><\/li>\n<\/ul>\n<p>Our build Makefile has these cryptic lines in:<\/p>\n<pre>ifeq ($(shell uname -m),x86_64)\r\n\tARCH_DEPS:=\/proc\/sys\/fs\/binfmt_misc\/arm\r\nendif\r\n\r\n\/proc\/sys\/fs\/binfmt_misc\/arm:\r\n\techo ':arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff\\xff:\/usr\/bin\/qemu-arm-static:' | sudo tee \/proc\/sys\/fs\/binfmt_misc\/register<\/pre>\n<p>This says &#8211; if we&#8217;re building on an x86_64 machine (i.e. our 64 bit intel laptops), then put that magic string into <code>\/proc\/sys\/fs\/binfmt_misc\/register<\/code>\u00a0which registers the <code>qemu-arm-static<\/code>\u00a0binary as an ARM interpreter in the kernel (using binfmt_misc kernel module).\u00a0 In other words, use the qemu emulator to make this machine pretend to be ARM architecture while building.<\/p>\n<p>We can now do all our development on intel linux laptops, build on the fast laptop, put the binaries into a deploy container and copy the container over to the Pi for execution.\u00a0 We can do the copy in a couple of ways.\u00a0 We can use <code>docker save<\/code>\u00a0to output a tar file which we copy over to the Pi and <code>docker load<\/code>\u00a0into docker there.\u00a0 Makefile has:<\/p>\n<pre>install-to-pi: controller-image.tar\r\n\trsync -zv --progress controller-image.tar pi@$(BOT_HOST):controller-image.tar\r\n\tssh pi@$(BOT_HOST) docker load -i controller-image.tar<\/pre>\n<p>The other way is to <code>docker push &lt;imagename&gt;<\/code>\u00a0the image to Dockerhub &#8211; this is cloud storage for Docker images.\u00a0 We can grab that from the cloud on the Pi with <code>docker pull &lt;imagename&gt;\u00a0<\/code>allowing us to grab and run the docker image on ANY Pi (connected to a network and running the Docker daemon) &#8211; so I can easily grab and try out code that Shaun has built and pushed to Dockerhub on my Pi at my home.<\/p>\n<p>This setup is a reasonably advanced use of Docker and pretty similar to what we have in our day jobs (building an open source software project for deployment on different architectures).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For us, using Docker means: we can build the code on our fast laptops and only deploy the built code to the robot&#8217;s Pi. The deployed container works just the same on my Pi and on Shaun&#8217;s Pi. We can package our build toolchain so that that too &#8220;just works&#8221; on my laptop and Shaun&#8217;s &hellip; <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/docker-part-2-our-use\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Docker part 2 &#8211; our use<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,5,10],"tags":[8,2],"_links":{"self":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/330"}],"collection":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":3,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/330\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}