{"componentChunkName":"component---src-templates-post-js","path":"/posts/2018/12/docker-in-ci-cd","result":{"pageContext":{"slug":"posts/2018/12/docker-in-ci-cd","date":"2018-12-29T00:00:00.000Z","title":"Docker for your CI/CD 101","description":"Docker images building could consume a pretty amount of time in your CI/CD. Here a nice trick.","content":"<p>With <a href=\"https://www.docker.com/\">Docker</a> and other technologies for\ncontainerization, you already understood the implication of a declarative way\nfor defining your environment so you are exploiting those benefits in both your\nruntime and CI/CD.</p>\n<blockquote>\n<p>If that’s not the case check out <code>Docker</code> <a href=\"https://docs.docker.com/get-started/\">here</a>.</p>\n</blockquote>\n<h2><code>Docker</code> image</h2>\n<p>Let’s take a look at an average <code>node</code> app <code>DockerFile</code>:</p>\n<pre class=\"language-docker\"><code class=\"language-docker\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> node</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">WORKDIR</span> /app</span>\n\n<span class=\"token comment\"># Copy all the stuff</span>\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> . /app/</span>\n\n<span class=\"token comment\"># Install deps</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> yarn install --ignore-optional</span>\n\n<span class=\"token comment\"># Build app</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> yarn build</span>\n\n<span class=\"token comment\"># Do something when `docker run your image...`</span>\n<span class=\"token instruction\"><span class=\"token keyword\">CMD</span> [<span class=\"token string\">\"yarn\"</span>, <span class=\"token string\">\"start\"</span>]</span>\n</code></pre>\n<p>That looks absolutely legit and will correctly generate a valid app image but\nwill fail to take advantage of the layer/cache capabilities of <code>Docker</code>: for any\nchange you make to your app, a complete rebuild will be required thus consuming\na precious amount of computing time (and I/O) in your CI/CD environment.</p>\n<p>Maybe this can be sustainable for your team. But microservices are a rising\npattern, so your CI/CD env can experience a peak in traffic when <em>“devs are on\nfire”</em>.</p>\n<blockquote>\n<p>And when you are trying to fix bugs, every minute can feel like an eternity.</p>\n</blockquote>\n<h2>Layer &amp; cache</h2>\n<p>A <code>Docker</code> layer can be compared to a filesystem snapshot. Every line of the\n<code>Dockerfile</code> will trigger a “snapshot”.</p>\n<p><code>Docker</code> will try to reuse every <s>snapshot</s> layer, if possible. So, the later\nwe change this immutable snapshot, the better: filesystem will be reused.</p>\n<h2>I’m in a hurry…</h2>\n<p>If we want go faster we must optimize our <code>Dockerfile</code> by splitting the portion\nof the filesystem that are often changing (src) from the ones that change rarely\n(deps and project metadata):</p>\n<pre class=\"language-docker\"><code class=\"language-docker\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> node</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">WORKDIR</span> /app</span>\n\n<span class=\"token comment\"># Add project and deps meta</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ADD</span> package.json /app/package.json</span>\n\n<span class=\"token comment\"># Install deps</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> yarn install --ignore-optional # &lt;-- !! We want this layer to be cached !!</span>\n\n<span class=\"token comment\"># Copy source code</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ADD</span> src /app/src</span>\n<span class=\"token comment\"># Build app</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> yarn build</span>\n\n<span class=\"token comment\"># Do something when `docker run ...`</span>\n<span class=\"token instruction\"><span class=\"token keyword\">CMD</span> [<span class=\"token string\">\"yarn\"</span>, <span class=\"token string\">\"start\"</span>]</span>\n</code></pre>\n<p>That way - because you don’t usually change your dependencies at every commit -\nwe can use the cached filesystem for every subsequent <em>step</em> and when we copy\nour source code, a layer with all the dependencies already in place will be\nprovided by <code>Docker</code> (thus avoiding intense I/O @every build).</p>\n<p>That will also be possible with <code>Java</code>, <code>Maven</code> and multistage images.</p>\n<pre class=\"language-docker\"><code class=\"language-docker\"><span class=\"token instruction\"><span class=\"token keyword\">FROM</span> maven:3.5-jdk-8-alpine <span class=\"token keyword\">as</span> builder</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">WORKDIR</span> /app</span>\n\n<span class=\"token comment\"># Prepare by downloading dependencies</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ADD</span> pom.xml /app/pom.xml</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> mvn dependency:go-offline # &lt;-- !! We want this layer to be cached !!</span>\n\n<span class=\"token comment\"># Adding source, compile and package into a fat jar</span>\n<span class=\"token instruction\"><span class=\"token keyword\">ADD</span> src /app/src</span>\n<span class=\"token instruction\"><span class=\"token keyword\">RUN</span> mvn package</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">FROM</span> openjdk:8-jre</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">COPY</span> <span class=\"token options\"><span class=\"token property\">--from</span><span class=\"token punctuation\">=</span><span class=\"token string\">builder</span></span> /app/target/fat.jar /usr/share/app/</span>\n\n<span class=\"token instruction\"><span class=\"token keyword\">EXPOSE</span> 8080</span>\n<span class=\"token instruction\"><span class=\"token keyword\">CMD</span> java -jar /usr/share/app/fat.jar</span>\n</code></pre>\n<p>This will provide <code>Maven</code> all the deps that are needed when executing\n<code>RUN mvn package</code> - and so…speeding our build.</p>\n<p>I hope you’ll find this tweak useful!</p>\n"}},"staticQueryHashes":[]}