{"id":17336098,"url":"https://github.com/randombenj/lxd-cheat-sheet","last_synced_at":"2025-03-27T07:24:33.824Z","repository":{"id":74944742,"uuid":"353288255","full_name":"randombenj/lxd-cheat-sheet","owner":"randombenj","description":"Some useful commands when working with lxd containers","archived":false,"fork":false,"pushed_at":"2022-02-20T16:32:41.000Z","size":5,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T12:27:28.598Z","etag":null,"topics":["cheatsheet","docker","linux-containers","lxd","podman"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/randombenj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-31T08:48:20.000Z","updated_at":"2022-02-20T16:32:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"1d5c2a1d-0987-4bef-9a95-bfa1fd68a668","html_url":"https://github.com/randombenj/lxd-cheat-sheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randombenj%2Flxd-cheat-sheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randombenj%2Flxd-cheat-sheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randombenj%2Flxd-cheat-sheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/randombenj%2Flxd-cheat-sheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/randombenj","download_url":"https://codeload.github.com/randombenj/lxd-cheat-sheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245799734,"owners_count":20674161,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheatsheet","docker","linux-containers","lxd","podman"],"created_at":"2024-10-15T15:27:47.342Z","updated_at":"2025-03-27T07:24:33.796Z","avatar_url":"https://github.com/randombenj.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# LXD Lightning Talk\n\n\u003e\u003e\u003e\n:warning: Before you can run the config examples, you must run this setup:\n\n```sh\nexport _GID=$(id -u)\nexport _UID=$(id -g)\nexport SSH_KEY=$(cat ~/.ssh/id_ed25519.pub)\n\nenvsubst \u003c configs/cloud-init.tpl.yml \u003e configs/cloud-init.yml\nenvsubst \u003c configs/gui.tpl.yml \u003e configs/gui.yml\n```\n\u003e\u003e\u003e\n\nSome nice resources:\n\n- https://ubuntu.com/blog/feeling-at-home-in-a-lxd-container\n- https://lxd.readthedocs.io/en/latest/\n\n### Basic Image and Container handling\n\nOne can use `docker` images for application deployment (mostly one process).\nAnd `lxd` for opersting system deployment.\n\n```sh\npodman search debian\n\npodman run -dt debian:buster /bin/sh\n\npodman ps\n\npodman container top\n```\n\n```sh\nlxc image ls images:debian\n\nlxc lauch images:debian/buster c1\n\nlxc info c1\n\nlxc config show c1\n\nlxc exec c1 bash\n\npstree 0\n```\n\n### Configuring via Cloud Init\n\nTo configure containers one can use [cloud-init](https://cloudinit.readthedocs.io/en/latest/)\n\n```sh\nlxc launch ubuntu:20.04 c1 \u003c configs/cloud-init.yml\n\nlxc exec c2 -- su --login $USER\n\n# get container IP\nlxc exec c2 -- ip -f inet addr show eth0 | awk '/inet / {print $2}'\n\nssh CONTAINER_IP\n```\n\n### General Container Configuration\n\n- https://lxd.readthedocs.io/en/latest/instances/\n\nOne can do a lot of things inside an lxd container, here are a few examples:\n\n```sh\nlxc config device add c2 share disk path=/home/$USER/demo source=$PWD/demo\n\nlxc config device add c2 awesome-page proxy listen=tcp:0.0.0.0:8000 connect=tcp:0.0.0.0:8000\n\nlxc exec c2 -- su --login $USER\n\ncd demo \u0026\u0026 python3 -m http.server\n```\n\nHave you ever wondered wether a `t2.micro` instance is enough for your application?\nA full list of supported types can be found here: https://github.com/dustinkirkland/instance-type\n\n```sh\nlxc launch --type t2.micro ubuntu:20.04 micro\nlxc config show micro\n```\n\nThis can even be live updated!\n\n```sh\nlxc exec micro -- watch -n 0.5 nproc\n\n# in another terminal\nlxc config edit micro\n```\n\nThis resource limitation enables to simulate the whole internet:\n\n - https://github.com/nsec/the-internet\n - https://stgraber.org/2015/12/\n\n### Run Graphical Applications Inside an LXD Container\n\nOne can run graphical applications inside a container by mounting `/tmp.X11-unix/X0`\nwith the correct id mapping.\n\n```sh\nlxc launch ubuntu:20.04 \u003c configs/gui.yml c3\n\nlxc exec c3 -- bash\n\nsudo apt update \u0026\u0026 sudo apt install --yes x11-apps \u0026\u0026 xeyes\n```\n\n### Run nested Containers\n\nWith lxd it is very simple to run an lxd container inside another lxd container.\n\n```sh\nlxc launch ubuntu:20.04 nested -c security.nesting=true\n\nlxc exec nested -- bash\n\n# inside the container\nlxd init --auto\nlxc launch images:debian/buster c1\n\nlxc exec c1 -- bash\n```\n\nThere is also a guide on how to run docker containers inside an lxd container:\nhttps://lxd.readthedocs.io/en/latest/#how-can-i-run-docker-inside-a-lxd-container\n\n### Run k3s inside a Container\n\nTo run [k3s](https://k3s.io/) inside an lxd container, simply start a conatier with the \n[k3s config](configs/k3s.yml).\n\n```sh\nlxc launch ubuntu:20.04 k3s \u003c configs/k3s.yml\nlxc exec k3s -- curl -sfL https://get.k3s.io | sh -\n```\n\n### Creating Snapshots\n\n```sh\nlxc snapshot c1 snap0\n\n# view snapshots\nlxc info c1\n\nlxc exec c1 -- touch after-snap0\nlxc exec c1 -- ls\n\nlxc restore c1 snap0\nlxc exec c1 -- ls\n```\n\n### Run virtual Machines\n\nOne can very easily run VM's with the `--vm` flag\n\n```sh\nlxc launch ubuntu:20.04 v1 --vm\nlxc console v1\nlxc exec v1 -- bash\n```\n\n### Use Containers in the GitLab Pipeline\n\nIt is very simple to use lxd containers in a gitlab pipeline,\nyou need to setup a custom runner for this: https://docs.gitlab.com/runner/executors/custom_examples/lxd.html\n\n\n```yaml\nrun-lxd-container:\n  tags: [lxd]\n  image: ubuntu:20.04\n  script:\n    - echo \"Hello lxd container!\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frandombenj%2Flxd-cheat-sheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frandombenj%2Flxd-cheat-sheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frandombenj%2Flxd-cheat-sheet/lists"}