<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joel Holmes</title>
    <description>The latest articles on DEV Community by Joel Holmes (@holmes89).</description>
    <link>https://dev.to/holmes89</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F744274%2F250293b4-0aef-4656-a108-02e7970a29fa.jpeg</url>
      <title>DEV Community: Joel Holmes</title>
      <link>https://dev.to/holmes89</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/holmes89"/>
    <language>en</language>
    <item>
      <title>Buildpacks with Go</title>
      <dc:creator>Joel Holmes</dc:creator>
      <pubDate>Wed, 03 Nov 2021 19:03:00 +0000</pubDate>
      <link>https://dev.to/holmes89/buildpacks-with-go-ao1</link>
      <guid>https://dev.to/holmes89/buildpacks-with-go-ao1</guid>
      <description>&lt;p&gt;&lt;em&gt;Take 35% off &lt;a href="https://www.manning.com/books/continuous-delivery-in-go?utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_holmes4_continuous_9_20_21&amp;amp;utm_content=author" rel="noopener noreferrer"&gt;Continuous Delivery in Go&lt;/a&gt;&lt;br&gt;
by entering &lt;strong&gt;fccholmes4&lt;/strong&gt; into the discount code at checkout at &lt;a href="https://www.manning.com/?utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_holmes4_continuous_9_20_21&amp;amp;utm_content=author" rel="noopener noreferrer"&gt;manning.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Today there are so many ways to deploy something. You can go down the Function as a Service (FaaS), Platform as a Service (PaaS), Container as&lt;br&gt;
a Service (CaaS), or Infrastructure as a Service (IaaS). All of these have one thing in common: they are virtualized. Each layer in our As a Service Parfait abstracts something different. A Function is an abstraction on a Platform which is an abstraction on a Container which and abstraction on a Virtualized Server. Under it all are pieces of specialized hardware to help manage these abstractions. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv2ab1sayv6u1wlvlxwe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv2ab1sayv6u1wlvlxwe.png" alt="as a service breakdown"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The cloud is just someone else's computer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you look at various cloud providers it all makes sense. Pay for hardware and charge for software. Functions as a Service and Platforms as a Service run in specialized containers for their virtualized container management. They have an understanding of their hardware and optimize their containers for it. This can all be done by using a special base images or allowing them to help build the images for you. These images are known as &lt;em&gt;Buildpacks&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Buildpacks are inextricably linked with how many PaaS work under the hood. In fact, this technology was first developed by Heroku in 2011 and has been used by various other companies such as Pivotal and Google to help run their PaaS. The concepts are simple: you provide the code and we'll build the image. Under the hood, PaaS are building their own custom images based on the libraries and dependencies that their platforms need to make the code run as efficiently as possible and deploy them as containers in their hosting environment. This gives you resilience and substantial up time while they can get the most out of their hardware by running isolated, secure, and maintainable applications.&lt;/p&gt;

&lt;p&gt;If you are building an application, Buildpacks will give you a lot of features that will make your application more robust, like advanced caching, multiprocessing, language detection, and much more. The big game changer for Buildpacks recently is this notion of &lt;em&gt;Cloud Native Buildpacks&lt;/em&gt; that allow you, as the developer, to take advantage of the PaaS-like ecosystem of building an application with the portability of using containers.&lt;/p&gt;

&lt;p&gt;So what goes on inside of a Buildpack? When triggering a Buildpack, it goes through two stages: detection and build. When triggering a build, the Buildpack will analyze your source code to first determine if it can recognize the source code and build the container, this is known as the detection stage. In our case it will look for Go files or a &lt;code&gt;.mod&lt;/code&gt; file. If we were building a JavaScript application it would look for a &lt;code&gt;package.json&lt;/code&gt; file, or a &lt;code&gt;pom.xml&lt;/code&gt; file for a Java application.&lt;/p&gt;

&lt;p&gt;When entering the building stage, the Buildpack will determine what the runtime should be, how the library should be built, installation of dependencies, and compilation and running of the application itself. It does this through the use of a &lt;em&gt;builder&lt;/em&gt; which is an image specifically for creating the application based on the detection done in the previous step. The build and running of an image is done through a &lt;em&gt;stack&lt;/em&gt; which combines the build and run environments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Docker is just one container runtime. There are many other container runtimes out there that aren't as popular.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All of this can allow different groups to create a process for identifying and building applications specific for their runtimes and environments. This means that Google, Amazon, Heroku, and Microsoft can build their own container runtimes that are optimized for their hardware and you can tap into that performance by using their Buildpack. Let's try it out with Google.&lt;/p&gt;
&lt;h2&gt;
  
  
  Quick Hello API
&lt;/h2&gt;

&lt;p&gt;To demonstrate the power of this we are going to create quick Go project that just returns "Hello World" when you call an HTTP endpoint. Run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;hello-api
go mod init hello-api
&lt;span class="nb"&gt;touch &lt;/span&gt;main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then open up the source file and add the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"encoding/json"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;":8080"&lt;/span&gt;

    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;enc&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
                &lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s"&gt;"application/json; charset=utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{}{&lt;/span&gt;
                &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"world"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;enc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"unable to encode response"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"listening on %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's build a container
&lt;/h2&gt;

&lt;p&gt;To start, we are going to use a Buildpack to build and run our container locally. Then, we will use the same process to deploy the container to production. Following that, we will work on building our own container using our own definition and deploy that as well. This way you will have the knowledge of how to build and maintain your own containerized deployment and local development. First, we must first install our container runtime; in this case Docker.&lt;/p&gt;

&lt;p&gt;Docker has three different installation types depending on your operating system so it will be best if you &lt;a href="https://docs.docker.com/get-docker/" rel="noopener noreferrer"&gt;follow the directions&lt;/a&gt; for the one that best suits you. This will give us our container runtime. Now, we need to create a container. To do this we  will use a Buildpack. Let's install &lt;a href="https://buildpacks.io/docs/tools/pack/" rel="noopener noreferrer"&gt;pack&lt;/a&gt; first, which is a tool built and maintained by Cloud Native buildpacks.&lt;/p&gt;

&lt;p&gt;Pack will help us choose and build our application into a container using a defined Buildpack. To demonstrate this, let's see what pack suggests we use to build our application. Type &lt;code&gt;pack builder suggest&lt;/code&gt; and see what options come up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Google:                gcr.io/buildpacks/builder:v1      Ubuntu 18 base image with buildpacks for .NET, Go, Java, Node.js, and Python                                                      
Heroku:                heroku/buildpacks:18              Base builder for Heroku-18 stack, based on ubuntu:18.04 base image                                                                
Heroku:                heroku/buildpacks:20              Base builder for Heroku-20 stack, based on ubuntu:20.04 base image                                                                
Paketo Buildpacks:     paketobuildpacks/builder:base     Ubuntu bionic base image with buildpacks for Java, .NET Core, NodeJS, Go, Python, Ruby, NGINX and Procfile                        
Paketo Buildpacks:     paketobuildpacks/builder:full     Ubuntu bionic base image with buildpacks for Java, .NET Core, NodeJS, Go, Python, PHP, Ruby, Apache HTTPD, NGINX and Procfile     
Paketo Buildpacks:     paketobuildpacks/builder:tiny     Tiny base image (bionic build image, distroless-like run image) with buildpacks for Java Native Image and Go 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that these packs are not focused on a specific language, rather they provide a broad foundation for multiple languages. You may also notice that these languages are the ones that are supported for FaaS and PaaS offerings on Google. This is because underneath the covers our FaaS and PaaS are actually running within a container using a Buildpack. Now let's build our application by typing the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pack build hello-api --builder gcr.io/buildpacks/builder:v1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a look at what happened. If you look closely you can see that&lt;br&gt;
the builder identifies that our application is a Go project from our&lt;br&gt;
module file and will look for a main package to run. There are&lt;br&gt;
configurations that can be done if you have more than one main function.&lt;br&gt;
Each Buildpack will have its own configuration. To see how our container&lt;br&gt;
runs just type in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run hello-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see your server run. Call your hello endpoint and see that your application is running in a nice, neat portable package. Now you can use it however you want! With this process you can explore and work with Buildpacks and build new and greater things.&lt;/p&gt;

&lt;p&gt;If you want to learn more about the book, check it out on Manning's&lt;br&gt;
liveBook platform &lt;a href="https://livebook.manning.com/book/continuous-delivery-in-go?origin=product-look-inside&amp;amp;utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_holmes4_continuous_9_20_21&amp;amp;utm_content=author" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to learn more about the motivations behind the book please read my other &lt;a href="https://joelholmes.dev/posts/cd/" rel="noopener noreferrer"&gt;post&lt;/a&gt; on my personal site.&lt;/p&gt;

</description>
      <category>buildpacks</category>
      <category>containers</category>
      <category>go</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
