Linux Containers
Menu Close menu
Jump to main content
  • Home
  • LXC
    • Introduction
    • News
    • Getting started
    • Documentation
    • Manpages
      • lxc-attach.1
      • lxc-autostart.1
      • lxc-cgroup.1
      • lxc-checkconfig.1
      • lxc-checkpoint.1
      • lxc-config.1
      • lxc-console.1
      • lxc-copy.1
      • lxc-create.1
      • lxc-destroy.1
      • lxc-device.1
      • lxc-execute.1
      • lxc-freeze.1
      • lxc-info.1
      • lxc-ls.1
      • lxc-monitor.1
      • lxc-snapshot.1
      • lxc-start.1
      • lxc-stop.1
      • lxc-top.1
      • lxc-unfreeze.1
      • lxc-unshare.1
      • lxc-update-config.1
      • lxc-user-nic.1
      • lxc-usernet.5
      • lxc-usernsexec.1
      • lxc-wait.1
      • lxc.7
      • lxc.conf.5
      • lxc.container.conf.5
      • lxc.system.conf.5
    • Contribute
    • Security
    • Downloads
    • External resources
    • Articles
    • Forum
    • Mailing lists
    • IRC
    • Github
    • Jenkins CI
    • Travis CI
  • LXD
    • Introduction
    • News
    • Getting started - command line
    • Getting started - OpenStack
    • Documentation
    • REST API
    • Contribute
    • Downloads
    • Try it online
    • External resources
    • Product page
    • Articles & videos
    • Forum
    • Mailing lists
    • IRC
    • Github
    • Travis CI
  • LXCFS
    • Introduction
    • News
    • Getting started
    • Manpages
      • lxcfs.1
    • Contribute
    • Downloads
    • External resources
    • Forum
    • Mailing lists
    • IRC
    • Github
  • CGManager
    • Introduction
    • News
    • Getting started
    • D-Bus API
    • Manpages
      • cgm.1
      • cgmanager.8
      • cgproxy.8
    • Contribute
    • Downloads
    • External resources
    • Articles
    • Mailing lists
    • IRC
    • Github
    • Travis CI
  • Language
    • English
    • Français
    • Italiano
    • Português Brasileiro
    • 日本語
    • 한국어
    • Русский
  • LXD
  • Try it online

Server status You are connected over: ()
The demo server is currently running user sessions out of
The demo service is currently down for maintenance and should be back online in a few minutes. Your browser couldn't reach the demo server.
This is either (most likely) because of a firewall or proxy issue on your side or because of a network, power or other catastrophic server side failure.

Terms of service

Start

Starting the container...

Container information
Remote LXD lxc remote add tryit --password=
Remaining time minutes, seconds
Terminal
Step by step instructions
  • Introduction
  • Your first container
  • Limiting resources
  • Snapshots
  • Creating images
  • Accessing files from the container
  • Use a remote image server
  • Interact with remote LXD servers
  • Conclusion

Introduction

You are now root inside a LXD container with a nested LXD installed inside it.

Initial startup can take a few seconds due to having to generate SSL keys on a rather busy system.
Further commands should then be near instantaneous.

You're welcome to just poke around and discover LXD through its manpage and --help option.
Or you can follow this step by step tutorial instead which will get you through LXD's main features.

  • Previous
  • Next

Your first container

LXD is image based, however by default no images are loaded into the image store as can be seen with:

lxc image list

LXD knows about 3 default image servers:

  • ubuntu: (for Ubuntu stable images)
  • ubuntu-daily: (for Ubuntu daily images)
  • images: (for a bunch of other distributions)

The stable Ubuntu images can be listed with:

lxc image list ubuntu: | less

To launch a first container called "first" using the Ubuntu 16.04 image, use:

lxc launch ubuntu:16.04 first

Your new container will now be visible in:

lxc list

Running state details and configuration can be queried with:

lxc info first
lxc config show first

  • Previous
  • Next

Limiting resources

By default your container comes with no resource limitation and inherits from its parent environment. You can confirm it with:

free -m
lxc exec first -- free -m

To apply a memory limit to your container, do:

lxc config set first limits.memory 128MB

And confirm that it's been applied with:

lxc exec first -- free -m

  • Previous
  • Next

Snapshots

LXD supports snapshoting and restoring container snapshots.
Before making a snapshot, lets do some changes to the container, for example, updating it:

lxc exec first -- apt-get update
lxc exec first -- apt-get dist-upgrade -y
lxc exec first -- apt-get autoremove --purge -y

Now that the container is all updated and cleaned, let's make a snapshot called "clean":

lxc snapshot first clean

Let's break our container:

lxc exec first -- rm -Rf /etc /usr

Confirm the breakage with (then exit):

lxc exec first -- bash

And restore everything to the snapshotted state:

lxc restore first clean

And confirm everything's back to normal (then exit):

lxc exec first -- bash

  • Previous
  • Next

Creating images

As your probably noticed earlier, LXD is image based, that is, all containers must be created from either a copy of an existing container or from an image.

You can create new images from an existing container or a container snapshot.

To publish our "clean" snapshot from earlier as a new image with a user friendly alias of "clean-ubuntu", run:

lxc publish first/clean --alias clean-ubuntu

At which point we won't need our "first" container, so just delete it with:

lxc stop first
lxc delete first

And lastly we can start a new container from our image with:

lxc launch clean-ubuntu second

  • Previous
  • Next

Accessing files from the container

To pull a file from the container you can use the "lxc file pull" command:

lxc file pull second/etc/hosts .

Let's add an entry to it:

echo "1.2.3.4 my-example" >> hosts

And push it back where it came from:

lxc file push hosts second/etc/hosts

You can also use this mechanism to access log files:

lxc file pull second/var/log/syslog - | less

We won't be needing that container anymore, so stop and delete it with:

lxc delete --force second

  • Previous
  • Next

Use a remote image server

The lxc client tool supports multiple "remotes", those remotes can be read-only image servers or other LXD hosts.

LXC upstream runs one such server at https://images.linuxcontainers.org which serves a set of automatically generated images for various Linux distributions.

It comes pre-added with default LXD but you can remove it or change it if you don't want it.

You can list the available images with:

lxc image list images: | less

And spawn a new Centos 7 container with:

lxc launch images:centos/7 third

Confirm it's indeed Centos 7 with:

lxc exec third -- cat /etc/redhat-release

And delete it:

lxc delete -f third

The list of all configured remotes can be obtained with:

lxc remote list

  • Previous
  • Next

Interact with remote LXD servers

For this step, you'll need a second demo session, so open a new one here

Copy/paste the "lxc remote add" command from the top of the page of that new session into the shell of your old session.
Then confirm the server fingerprint for the remote server.

Note that it may take a few seconds for the new LXD daemon to listen to the network, just retry the command until it answers.

At this point you can list the remote containers with:

lxc list tryit:

And its images with:

lxc image list tryit:

Now, let's start a new container on the remote LXD using the local image we created earlier.

lxc launch clean-ubuntu tryit:fourth

You now have a container called "fourth" running on the remote host "tryit". You can spawn a shell inside it with (then exit):

lxc exec tryit:fourth bash

Now let's copy that container into a new one called "fifth":

lxc copy tryit:fourth tryit:fifth

And just for fun, move it back to our local lxd while renaming it to "sixth":

lxc move tryit:fifth sixth

And confirm it's all still working (then exit):

lxc start sixth
lxc exec sixth -- bash

Then clean everything up:

lxc delete -f sixth
lxc delete -f tryit:fourth
lxc image delete clean-ubuntu

  • Previous
  • Next

Conclusion

We hope this gave you a good introduction to LXD, its capabilities and how easy it is to use.

You're welcome to use the demo service as long as you want to try LXD and play with the latest features.

Enjoy!

  • Previous
  • Next
Feedback
Fill this to allow us to contact you about your feedback.
Unable to create a new container Unable to access the container

The server is currently full, please try again in a few minutes.

You have reached the maximum number of concurrent sessions, please wait for some to expire before starting more of them.

You have been banned from this service due to a failure to respect the terms of service.

An unknown error occured. Please try again in a few minutes.

The container you're trying to connect to doesn't exist anymore.

Project sponsored by Canonical Ltd.

  • Improve this website
  • Back to top
  • Content under Creative Commons CC BY NC SA