<?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: Markus Binsteiner</title>
    <description>The latest articles on DEV Community by Markus Binsteiner (@makkus).</description>
    <link>https://dev.to/makkus</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%2F182425%2F833465b8-318e-48fb-9332-a7c3c269b965.jpeg</url>
      <title>DEV Community: Markus Binsteiner</title>
      <link>https://dev.to/makkus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/makkus"/>
    <language>en</language>
    <item>
      <title>'Dockerfiles' for LXD</title>
      <dc:creator>Markus Binsteiner</dc:creator>
      <pubDate>Sun, 11 Aug 2019 17:39:38 +0000</pubDate>
      <link>https://dev.to/makkus/dockerfiles-for-lxd-42nj</link>
      <guid>https://dev.to/makkus/dockerfiles-for-lxd-42nj</guid>
      <description>&lt;h2&gt;
  
  
  tldr;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://freckles.io"&gt;&lt;em&gt;freckles&lt;/em&gt;&lt;/a&gt; can be used as a generic, composable 'Dockerfile' replacement for LXD. To try it out, on a system that has &lt;em&gt;freckles&lt;/em&gt; and LXD installed and working, put the following content into a file &lt;code&gt;'example-lxd.frecklet&lt;/code&gt;':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;nginx-service&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;nginx-vhost-from-folder&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;document_root&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/var/www/html&lt;/span&gt;
    &lt;span class="na"&gt;default_server&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;use_https&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;file-with-content&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;www-data&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/var/www/html/index.html&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
      &lt;span class="s"&gt;&amp;lt;h1&amp;gt;&amp;lt;i&amp;gt;freckles&amp;lt;/i&amp;gt; says "Hello World", from {{:: from ::}}!&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will act as our 'Dockerfile' and describes how to install and configure nginx to serve a static webpage. To create an LXD image from it, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute lxd-image-from-frecklet-file &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--install-packer&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--source-image&lt;/span&gt; images:debian/10 &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--image-name&lt;/span&gt; freckles-image &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--frecklet-path&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;/example-lxd.frecklet &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--frecklet-vars&lt;/span&gt; &lt;span class="s1"&gt;'{"from": "my first image"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once that has finished, create a container from the new 'freckles-image' image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;lxc launch freckles-image freckles-test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To test whether the Nginx service that runs in the container works, issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;curl http://&amp;lt;container_ip&amp;gt;
&lt;span class="gp"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&amp;lt;i&amp;gt;freckles&amp;lt;/i&amp;gt; says &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;, from my first image!&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Long-ish preamble
&lt;/h2&gt;

&lt;p&gt;I think Dockerfiles played a fairly big part in Dockers success and adoption early on. They are simple to read/understand, flexible enough to be useful beyond basic tasks, and composable (if you squint your eyes a bit and look at 'inheriting from another Docker image' as composition).&lt;/p&gt;

&lt;p&gt;Of course, there are other factors for Dockers success, being at the right time at the right place (a.k.a. luck) among them, but Dockerfiles are certainly important. Docker containers are usually considered to be 'application containers', and part of a microservice-type architecture. In practices, that is (and probably never was) 100% true, there are a lot of people trying to force Docker into something else, for example coming up with 'init-systems' for Docker, trying to run webapps incl. Database and Memcache and whatever else in a single container. Think of that what you will, it's still a testament for Dockers success, and the flexibility of Dockerfiles, because they actually allow for all this to be built.&lt;/p&gt;

&lt;p&gt;Now, if we just accept the premise there are cases where its advantageous to have multiple services on the same host without the overhead of a virtual network connecting all of them (and we could certainly argue when that makes and doesn't make sense), we'll sooner or later stumble upon LXD (I'll ignore the more low-level LXC, but I acknowledge that sometimes it'd be a better fit).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://linuxcontainers.org/lxd/introduction/"&gt;LXD&lt;/a&gt; is a so-called 'system container manager' (as opposed to 'application container'), which means it 'contains' whole operating systems, including a 'normal' init system. An LXD container behaves like a physical or virtual machine, except for some permission-related things, but without much overhead (like 'real' VMs, for example).&lt;/p&gt;

&lt;p&gt;LXD does not have an equivalent to a Dockerfile though (ignoring 'cloud-init' for now -- using &lt;em&gt;freckles&lt;/em&gt; with 'cloud-init- is a topic for a whole other blog post). One reason for this is that it's not &lt;em&gt;really&lt;/em&gt; necessary. Since an LXD container behaves like a normal physical or virtual machine, we can use the same tooling to install applications, and generally get a container into the state we want it to be. And for the case where sysadmin/devops-professionals do the state-making, that is fine, they know what to do, and how to do it in an automated way. Using tools like Ansible, Puppet. Or maybe Packer, Terraform, etc.&lt;/p&gt;

&lt;p&gt;The one advantage a 'Dockerfile for LXD' would have in that situation is to bring easy reproducability, re-usability, and just general 'orderlyness' to LXD for people who don't have to deal with those things usually. In the same way Dockerfiles brought those things along with Docker. All you need is a single text file, and you'll be able to replicate and costumize a Docker image. I think we can't overestimate the effect of a technology that allows people who know what they are doing (those who create Dockerfiles) to share what they are doing (Dockerfiles) with people who wouldn't have known how to do that particular thing (at least without spending a few hours on Stackoverflow) or can't be bothered, and who now can just re-use that particular thing.&lt;/p&gt;

&lt;p&gt;A thing which, by the way, now only has to be created once, and which then can be continuously improved upon by a community of people (who ideally know what they are doing). It's like using a specialized library in any programming language, it allows you to focus on your core problem as an application developer, without too much distraction on those tangential details you'd have to worry about otherwise. It's really not that deep of a problem, everything considered. But it's wide-spread, and prevalent to the point that we often don't even see and recognize it in our daily (working) life, which means we are not taking advantage of the already existing solutions for such a problem.&lt;/p&gt;

&lt;p&gt;This is, incidentally, exactly what &lt;a href="https://freckles.io"&gt;freckles&lt;/a&gt; tries to solve too, just in a more generic way than a Dockerfile does. Allow for re-usable recipes to bring computational environments into a certain state, technolgy- and stack-independent.&lt;/p&gt;

&lt;p&gt;Which, finally, brings me to the topic of this post: using &lt;a href="https://freckles.io/doc/frecklets"&gt;&lt;em&gt;frecklets&lt;/em&gt;&lt;/a&gt; as sort of re-usable 'Dockerfile-replacements'. In the context of LXD. Of course, since they are re-usable, you can use the same &lt;em&gt;frecklet&lt;/em&gt; you use in LXD for Docker too (with some caveats), or, to provision a remote server (as I'll show at the end of this post).&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;em&gt;freckles&lt;/em&gt; with LXD
&lt;/h2&gt;

&lt;p&gt;There are two separate areas where we can choose to use &lt;em&gt;freckles&lt;/em&gt; in combination with LXD:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;describing the content/state of the images&lt;/li&gt;
&lt;li&gt;building the actual LXD images&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition, we can use &lt;em&gt;freckles&lt;/em&gt; to install and configure LXD:&lt;/p&gt;

&lt;h3&gt;
  
  
  Optional: using &lt;em&gt;freckles&lt;/em&gt; to install LXD
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: the following does not setup internet connectivity for containers who are connected to the newly created bridge 'lxbr0'. This has to be done manually (for now), e.g. by doing &lt;code&gt;iptables -t nat -A POSTROUTING -s &amp;lt;bridge_subnet&amp;gt;.0/24 -o eth0 -j MASQUERADE&lt;/code&gt;. This will be done automatically sometime in the future, hopefully.&lt;/p&gt;

&lt;p&gt;For this, we can use the &lt;a href="https://freckles.io/frecklets/default/virtualization/lxd-service"&gt;&lt;code&gt;lxd-service&lt;/code&gt;&lt;/a&gt; frecklet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute lxd-service &lt;span class="nt"&gt;--user&lt;/span&gt; markus
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Under the hood, this uses the &lt;a href="https://github.com/juju4/ansible-lxd"&gt;juju4.lxd&lt;/a&gt; Ansible role to install LXD on a Ubuntu or RedHat-based Linux system. Other distributions might work, but are not tested. Also, note the &lt;code&gt;--user markus&lt;/code&gt; command-line argument. This lets you specify one or several users who'll have permissions to use the LXD service on this host. After running this, you'll have to either log-out of your current session and log-in again, or execute &lt;code&gt;newgrp lxd&lt;/code&gt; to take advantage of this permission.&lt;/p&gt;

&lt;p&gt;It is possible to adjust some parameters related to your LXD setup with this &lt;em&gt;frecklet&lt;/em&gt; (like for example network addresses or storage settings), but this is out of scope in this context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Describing the LXD-image contents
&lt;/h3&gt;

&lt;p&gt;We'll be writing a relatively simple &lt;a href="https://freckles.io/doc/frecklets"&gt;&lt;em&gt;frecklet&lt;/em&gt;&lt;/a&gt;, not different at all to anything we'd write for a different target type (e.g. a remote server). For more details on how to all this works, and how you can write your own, please check the &lt;a href="https://freckles.io/doc/getting_started"&gt;&lt;em&gt;freckles&lt;/em&gt; getting started guide&lt;/a&gt; and the &lt;a href="https://freckles.io/doc/frecklets"&gt;other frecklet-related documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For our example, lets install the Nginx webserver and configure it to host a single, static html site (which we also create dynamically).&lt;/p&gt;

&lt;p&gt;As this is a relatively 'normal' thing to do, there are already pre-made &lt;em&gt;frecklets&lt;/em&gt; that can help us implement the required steps, which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;install the Nginx packages and enable/start the service (&lt;a href="https://freckles.io/frecklets/default/service/nginx-service"&gt;&lt;code&gt;nginx-service&lt;/code&gt;&lt;/a&gt;) &lt;/li&gt;
&lt;li&gt;create a configuration for our vhost (&lt;a href="https://freckles.io/frecklets/default/service/nginx-vhost-from-folder"&gt;&lt;code&gt;nginx-vhost-from-folder&lt;/code&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;create our html file (&lt;a href="https://freckles.io/frecklets/default/filesystem/file-with-content"&gt;&lt;code&gt;file-with-content&lt;/code&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how that looks as a &lt;em&gt;frecklet&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;nginx-service&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;nginx-vhost-from-folder&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;document_root&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/var/www/html&lt;/span&gt;
    &lt;span class="na"&gt;default_server&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;use_https&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;file-with-content&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;www-data&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/var/www/html/index.html&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
      &lt;span class="s"&gt;&amp;lt;h1&amp;gt;&amp;lt;i&amp;gt;freckles&amp;lt;/i&amp;gt; says "Hello World", from {{:: from ::}}!&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note the &lt;code&gt;{{:: from ::}}&lt;/code&gt; template string in our &lt;em&gt;frecklet&lt;/em&gt; code: this lets us customize the generated html for every LXD image (if we chose to do so).&lt;/p&gt;

&lt;p&gt;To test, lets launch an LXC ocntainer and provision it with our new &lt;em&gt;frecklet&lt;/em&gt;. We could do this with a simple &lt;code&gt;lxc launch ...&lt;/code&gt; command, but while we're at it let's use &lt;em&gt;freckles&lt;/em&gt; for this too. In an interactive session that doesn't make much sense, since it's also just one command, and comes with a bit of overhead. But it's a good opportunity to show how one would do that if launching an LXD container was part of a provisioning pipeline. So, here goes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute lxd-container-running &lt;span class="nt"&gt;--image-name&lt;/span&gt; &lt;span class="s1"&gt;'debian/10'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--image-server&lt;/span&gt; https://images.linuxcontainers.org &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="s1"&gt;'freckles-test'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--register-addresses&lt;/span&gt; freckles_container_ip
&lt;span class="go"&gt;
╭╼ starting run
│  ├╼ running frecklet: lxd-container-running (on: localhost)
│  │  ├╼ starting Ansible run
│  │  │  ├╼ create/launch container 'freckles-test'
│  │  │  │  ╰╼ ok
│  │  │  ╰╼ ok
│  │  ╰╼ ok
│  ╰╼ ok
╰╼ ok

Result:

    freckles_container_ip:
      eth0:
      - 10.10.10.42
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example, we provided the optional &lt;code&gt;--register-addresses&lt;/code&gt; command-line argument. That prompts &lt;em&gt;freckles&lt;/em&gt; to register the (network) details of our newly created container into a variable, which makes it easier for us to connect to it later. Alternatively, you can get the ip address with a simple &lt;code&gt;lxc list&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that our container is running, we can provision it using our &lt;em&gt;frecklet&lt;/em&gt; from above. We'll save the content string as &lt;code&gt;example-lxd.frecklet&lt;/code&gt;, then issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute &lt;span class="nt"&gt;--target&lt;/span&gt; lxd::freckles-test example-lxd.frecklet &lt;span class="nt"&gt;--from&lt;/span&gt; &lt;span class="s1"&gt;'a test run'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The crucial thing is the &lt;code&gt;--target lxd::&amp;lt;container_name&amp;gt;&lt;/code&gt; part. It tells &lt;em&gt;freckles&lt;/em&gt; to not connect via &lt;em&gt;ssh&lt;/em&gt;, but directly via 'lxd'.&lt;/p&gt;

&lt;p&gt;This will take a minute or two, as there is some bootstrapping to be done in addition to installing Nginx. Once the process is finished, you should be able to point your browser to the IP address displayed in the run before this, and see the message "freckles says "Hello World", from a test run!"&lt;/p&gt;

&lt;p&gt;Sweet, now we have everything we need to make that into an LXD container image...&lt;/p&gt;

&lt;h3&gt;
  
  
  Building an LXD image
&lt;/h3&gt;

&lt;p&gt;While it's possible to create an LXD image from scratch using &lt;em&gt;freckles&lt;/em&gt;, for example by capturing the steps from &lt;a href="https://tutorials.ubuntu.com/tutorial/create-custom-lxd-images#0"&gt;this tutorial&lt;/a&gt; into a &lt;em&gt;frecklet&lt;/em&gt;, we won't do that here. We are going to be cheating a bit, and use &lt;a href="https://www.packer.io"&gt;Packer&lt;/a&gt; instead, since that offers the same functionality (and more), with less hassle.&lt;/p&gt;

&lt;p&gt;As we already have our &lt;code&gt;example-lxd.frecklet&lt;/code&gt; file, all we need to do is provide it's full path in the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute container-image-from-frecklet-file &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--install-packer&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--image-type&lt;/span&gt; lxd &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--source-image&lt;/span&gt; images:debian/10 &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--image-name&lt;/span&gt; freckles-image &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--frecklet-path&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;/example-lxd.frecklet &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;--frecklet-vars&lt;/span&gt; &lt;span class="s1"&gt;'{"from": "my first image"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's look at that command in detail:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://freckles.io/frecklets/default/virtualization/container-image-from-frecklet-file"&gt;&lt;code&gt;container-image-from-frecklet-file&lt;/code&gt;&lt;/a&gt;: the name of &lt;a href="https://freckles.io/frecklets/default/virtualization/container-image-from-frecklet-file"&gt;the
frecklet&lt;/a&gt;
we want to execute (we could have also used the wrapper &lt;em&gt;frecklet&lt;/em&gt; &lt;a href="https://freckles.io/frecklets/default/virtualization/lxd-image-from-frecklet-file"&gt;&lt;code&gt;lxd-image-from-frecklet-file&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--install-packer&lt;/code&gt;: we make sure that the
&lt;a href="https://www.packer.io"&gt;Packer&lt;/a&gt; executable is available on our
system (using the
&lt;a href="https://freckles.io/frecklets/default/devops/packer-installed"&gt;&lt;code&gt;packer-installed&lt;/code&gt;&lt;/a&gt;
&lt;em&gt;frecklet&lt;/em&gt; under the hood)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--image-type lxd&lt;/code&gt;: we want an LXD image (we could have also said:
&lt;code&gt;docker&lt;/code&gt;, but since there is some systemd/init stuff in our frecklet, indirectly, that won't work in this case)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--source-image images:debian/10&lt;/code&gt;: the source image to use (notice
the 'images:'-prefix, this is needed in this case to use the correct
LXD image index&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--image-name&lt;/code&gt;: the name of our new image&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--frecklet-path $(pwd)/example-lxd.frecklet&lt;/code&gt; (absolute!) path to the
&lt;em&gt;frecklet&lt;/em&gt; that contains our provisioning instructions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--frecklet-vars '{"..."}'&lt;/code&gt;: additional variables (as a JASON
string) which are needed by our provisioning &lt;em&gt;frecklet&lt;/em&gt;. The
alternative to using this is hard-coding everything in the
&lt;em&gt;frecklet&lt;/em&gt; file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, once this command finished, you should be able to see the new image via:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;lxc image list
&lt;span class="go"&gt;+----------------+--------------+--------+-----------------------------------------------+--------+-----------+-------------------------------+
|     ALIAS      | FINGERPRINT  | PUBLIC |                  DESCRIPTION                  |  ARCH  |   SIZE    |          UPLOAD DATE          |
+----------------+--------------+--------+-----------------------------------------------+--------+-----------+-------------------------------+
| freckles-image | f9600c979eba | no     | built with freckles.                          | x86_64 | 194.72MB  | Aug 10, 2019 at 10:37am (UTC) |
&lt;/span&gt;&lt;span class="c"&gt;...
...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And we can use it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute lxd-container-running &lt;span class="nt"&gt;--image-name&lt;/span&gt; &lt;span class="s1"&gt;'freckles-image'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
      &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="s1"&gt;'freckles-example'&lt;/span&gt; &lt;span class="nt"&gt;--register-addresses&lt;/span&gt; freckles_container_ip
&lt;span class="go"&gt;
╭╼ starting run
│  ├╼ running frecklet: lxd-container-running (on: localhost)
│  │  ├╼ starting Ansible run
│  │  │  ├╼ create/launch container 'freckles-example'
│  │  │  │  ╰╼ ok
│  │  │  ╰╼ ok
│  │  ╰╼ ok
│  ╰╼ ok
╰╼ ok

Result:

    freckles_container_ip:
      eth0:
      - 10.10.10.100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now point your browser to the container ip (10.10.10.100 in my case), and you should see a message like: 'freckles says "Hello World", from my first image!'.&lt;/p&gt;

&lt;h3&gt;
  
  
  Re-using our &lt;em&gt;frecklet&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;The main reason I wrote &lt;em&gt;freckles&lt;/em&gt; was to have a generic, flexible and re-usable way to describe desired states of computational environments. We already used our &lt;em&gt;frecklet&lt;/em&gt; to provision a running (empty) LXD container, and to create an LXD container image from it. If our &lt;em&gt;frecklet&lt;/em&gt; didn't contain init-system-specific instructions (setting up and restarting a systemd service unit), we could also use it to build a Docker image. This won't work here, but lets use it instead to setup a remote server (like an EC2 instance, or a Digital Ocean droplet):&lt;/p&gt;

&lt;p&gt;Requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(empty) Ubuntu/Debian remote machine with public IP&lt;/li&gt;
&lt;li&gt;root or sudo access to that machine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We don't need to make any changes to our &lt;em&gt;frecklet&lt;/em&gt;, all we need to do is change the &lt;code&gt;--target&lt;/code&gt; in our command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;frecklecute &lt;span class="nt"&gt;-t&lt;/span&gt; root@159.69.201.220 example-lxd.frecklet &lt;span class="nt"&gt;--from&lt;/span&gt; &lt;span class="s2"&gt;"a remote machine"&lt;/span&gt;
&lt;span class="go"&gt;
╭╼ starting run
│  ├╼ running frecklet: /home/markus/projects/frkl-dev/frkl.io/example-lxd.frecklet (on: 159.69.201.220)
│  │  ├╼ starting Ansible run
│  │  │  ├╼ updating apt cache
│  │  │  │  ╰╼ ok
│  │  │  ├╼ ensure rsync, ca-certificates and unzip packages are installed
│  │  │  │  ╰╼ ok
│  │  │  ├╼ creating freckles share folder
│  │  │  │  ╰╼ ok
│  │  │  ├╼ creating box basics marker file
│  │  │  │  ╰╼ ok
│  │  │  ├╼ recording python interpreter metadata
│  │  │  │  ╰╼ ok
│  │  │  ├╼ recording box metadata for later runs
│  │  │  │  ╰╼ ok
│  │  │  ├╼ Ensure nginx is installed.
│  │  │  │  ╰╼ ok
│  │  │  ├╼ Copy nginx configuration in place.
│  │  │  ├╼ reload nginx
│  │  │  │  ╰╼ ok
│  │  │  ├╼ write content to file: /etc/nginx/sites-enabled/localhost.http.conf
│  │  │  │  ╰╼ ok
│  │  │  ├╼ ensure user 'www-data' exists
│  │  │  │  ╰╼ ok
│  │  │  ├╼ write content to file: /var/www/html/index.html
│  │  │  │  ╰╼ ok
│  │  │  ├╼ geerlingguy.nginx : reload nginx
│  │  │  │  ╰╼ ok
│  │  │  ╰╼ ok
│  │  ╰╼ ok
│  ╰╼ ok
╰╼ ok

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;curl http://159.69.201.220
&lt;span class="gp"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&amp;lt;i&amp;gt;freckles&amp;lt;/i&amp;gt; says &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;, from a remote machine!&amp;lt;/h1&amp;gt;%
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>lxd</category>
      <category>freckles</category>
      <category>ansible</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
