{"id":8353,"date":"2019-01-19T10:53:08","date_gmt":"2019-01-19T10:53:08","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=8353"},"modified":"2019-03-09T01:34:40","modified_gmt":"2019-03-09T01:34:40","slug":"introduction-to-ubuntus-lxd-containers","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/19\/introduction-to-ubuntus-lxd-containers\/","title":{"rendered":"Introduction to Ubuntu&#8217;s LXD Containers"},"content":{"rendered":"<header class=\"entry-header\">\n<div class=\"entry-meta mb-2\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" style=\"font-size: 1rem;\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-featured.png\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-featured.png 800w, https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-featured-200x100.png 200w, https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-featured-400x200.png 400w\" alt=\"\" width=\"800\" height=\"400\" \/><\/div>\n<div><\/div>\n<\/header>\n<div class=\"entry-content\">\n<p>Containers are useful for many reasons. They isolate apps from the rest of the system. They are portable and easy to clone and\/or move to other operating systems. And, in the case of Linux, they work the same way under any distribution, with no adaptations necessary. If you need to move a container from RedHat to Ubuntu, it should be just a simple copy operation.<\/p>\n<p>Docker is a popular solution designed to contain a single application: for example, an MySQL database server. LXD is similar in some ways but designed to contain an entire operating system. This makes it useful for some scenarios. For example, you can spin up an LXD container, install a database server and an http server. You can then create a WordPress website inside. You can now switch from cloud to cloud by just moving this LXD container where it\u2019s needed when you\u2019re not happy with your previous provider. And since it\u2019s easy to clone a container, you can even upload your website to multiple cloud providers to create a redundant, high-availability setup.<\/p>\n<p>LXD doesn\u2019t virtualize hardware like QEMU or VirtualBox do, which means it\u2019s very fast, offering near-native speed of execution.<\/p>\n<h2>Install and Configure LXD<\/h2>\n<p>Open a terminal and install LXD, plus the ZFS utilities, which will help you speed up some operations and save disk space when working with containers.<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>sudo apt install zfsutils-linux lxd<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288011 aligncenter\" title=\"lxd-apt-install\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-apt-install.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-apt-install.png\" alt=\"lxd-apt-install\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-apt-install.png\" \/><\/p>\n<p>Start LXD configuration.<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>sudo lxd init<\/pre>\n<\/div>\n<\/div>\n<p>Press ENTER to select the default values.<\/p>\n<p>For\u00a0<code>Size in GB of the new loop device (1GB minimum) [default=15GB]<\/code>, you can pick another value, like \u201c50GB\u201d if you know you\u2019ll create a lot of containers.<\/p>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288012 aligncenter\" title=\"lxd-init-questions\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-init-questions.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-init-questions.png\" alt=\"lxd-init-questions\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-init-questions.png\" \/><\/p>\n<h2>Find and Launch an LXD Distribution Image<\/h2>\n<p>To list all Ubuntu images:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc image list ubuntu: arch=amd64<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288016 aligncenter\" title=\"lxd-ubuntu-images\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-ubuntu-images.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-ubuntu-images.png\" alt=\"lxd-ubuntu-images\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-ubuntu-images.png\" \/><\/p>\n<p>There will be a lot of results. You can ommit\u00a0<code>arch=amd64<\/code>\u00a0if you need images for other platforms, like ARM processors (Raspberry Pi devices use such architecture).<\/p>\n<p>In the previous picture the results have been limited (with\u00a0<code>|head<\/code>) to make it easier to read. The fingerprint of Ubuntu 18.04 (84a71299044b) has been highlighted. If you want to launch a container with that distribution, the command would be:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc launch ubuntu:84a71299044b<\/pre>\n<\/div>\n<\/div>\n<p>At the moment this would return a permission denied error. You need to be in the \u201clxd\u201d group. Your user has been added to this group already, but to make it active you have to log out and log back in. If you want to avoid that, use this command, replacing \u201cuser\u201d with your username:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>exec su - user<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288017 aligncenter\" title=\"lxd-user-active-groups\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-user-active-groups.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-user-active-groups.png\" alt=\"lxd-user-active-groups\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-user-active-groups.png\" \/><\/p>\n<p>Now, lxc commands work without requiring sudo.<\/p>\n<h3>Launch LXD Containers with non-Ubuntu Distributions<\/h3>\n<p>This command will show you what other distributions are available:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc image list images: arch=amd64<\/pre>\n<\/div>\n<\/div>\n<p>To launch an image, instead of the fingerprint, you can also use an alias name if you see one available in that list.<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc launch images:debian\/9<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288013 aligncenter\" title=\"lxd-launch-debian-9\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-launch-debian-9.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-launch-debian-9.png\" alt=\"lxd-launch-debian-9\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-launch-debian-9.png\" \/><\/p>\n<p>If you append a string at the end, you can choose a name for your container:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc launch images:debian\/9 wordpress-site<\/pre>\n<\/div>\n<\/div>\n<h2>Manage LXD Containers<\/h2>\n<p>To list all containers:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc list<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288014 aligncenter\" title=\"lxd-lxc-list\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-lxc-list.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-lxc-list.png\" alt=\"lxd-lxc-list\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-lxc-list.png\" \/><\/p>\n<p>The \u201cIPV4\u201d column especially is important if you have any running services on that instance. For example, if an Apache http server would be running on the instance, entering \u201c10.234.232.246\u201d in the browser would display the website hosted in the container.<\/p>\n<p>To stop a container:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc stop name_of_container<\/pre>\n<\/div>\n<\/div>\n<p>This can take a very long time (or fail) with non-Ubuntu distributions. It\u2019s better to get a shell to the container, and once inside, enter\u00a0<code>systemctl poweroff<\/code>\u00a0to stop it.<\/p>\n<p>If all else fails, you can force a stop with:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc stop name_of_container --force<\/pre>\n<\/div>\n<\/div>\n<p>To start it:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc start name_of_container<\/pre>\n<\/div>\n<\/div>\n<p>To move inside your container:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc shell name_of_container<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-288015 aligncenter\" title=\"lxd-shell-debian\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-shell-debian.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-shell-debian.png\" alt=\"lxd-shell-debian\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/lxd-shell-debian.png\" \/><\/p>\n<p>You can install programs with \u201csudo apt install\u201d and do anything else you would do on a normal Linux distribution, e.g., configure an Apache server. When you want to exit from the container, simply type:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>exit<\/pre>\n<\/div>\n<\/div>\n<h2>Transfer Files to\/from LXD Containers<\/h2>\n<p>To upload a file to your container:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc file push \/path\/to\/local\/file name_of_container\/path\/to\/uploaded\/file<\/pre>\n<\/div>\n<\/div>\n<p>Include the name of the file to be created, not only the directory where you want to upload it. Here is an example:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc file push lxd-apt-install.png accurate-colt\/var\/www\/website\/upload.png<\/pre>\n<\/div>\n<\/div>\n<p>To upload a directory instead of a file:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc file push \/path\/to\/local\/directory name_of_container\/path\/to\/remote\/directory --recursive --verbose<\/pre>\n<\/div>\n<\/div>\n<p>Example:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc file push \/bin accurate-colt\/tmp --recursive --verbose<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>To download a directory from your container to your main operating system:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc file pull name_of_container\/path\/to\/remote\/directory \/path\/to\/local\/directory --recursive --verbose<\/pre>\n<\/div>\n<\/div>\n<p>Example:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>lxc file pull accurate-colt\/tmp \/tmp --recursive --verbose<\/pre>\n<\/div>\n<\/div>\n<h2>Conclusion<\/h2>\n<p>This covers the basic usage of LXD containers. There are more advanced features such as snapshots and rollbacks, imposing limits on resources such as CPU and RAM, cloning containers, and so on.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=264963\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Containers are useful for many reasons. They isolate apps from the rest of the system. They are portable and easy to clone and\/or move to other operating systems. And, in the case of Linux, they work the same way under any distribution, with no adaptations necessary. If you need to move a container from RedHat &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/19\/introduction-to-ubuntus-lxd-containers\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Introduction to Ubuntu&#8217;s LXD Containers&#8221;<\/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":[1],"tags":[],"class_list":["post-8353","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=8353"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8353\/revisions"}],"predecessor-version":[{"id":10842,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8353\/revisions\/10842"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=8353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=8353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=8353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}