LXC
LXC Overview¶
LXC Containers are environments which we use to fully contain one project.
They can be though of as similar to virtual machines, but without the overhead as they share the Linux kernel with the host machine. Once inside the container, everything appears as though it were a standalone machine.
Desktop LXC¶
On the desktop, we use the same LXC functionality as used by the Cluster machines to run containers.
Storage¶
Containers are stored in the /var/lib/lxc
folder
Through a trick of file permissions, files owned by the first user on the host machine are automatically owned by the first user on the container. In our case this means that the host's main user automatically shares ownership with the container's ec user.
This means that we can edit site files from the host using PHPStorm, view repos using gitk etc.
Managing local containers¶
Running the following command will allow all lxc- prefixed commands to be run without requiring a sudo password
echo "`whoami` ALL=(ALL) NOPASSWD:/usr/bin/lxc-*" | sudo tee /etc/sudoers.d/lxc-commands
Your basic desktop lxc commands are:
lxc-attach {container_name}¶
Simply enough, attach to a local container as the ec user
lxc-xdebug {container_name}¶
Start a reverse tunnel so you can Xdebug PHP inside a local container.
-Install Xdebug in the container.
1 2 3 |
|
lxc-stop {container_name}¶
Stop a container
lxc-kill {container_name}¶
Loop through running containers and have the option to stop them
lxc-ls¶
See a list of all local containers with status
lxc-top¶
See a list of running containers with resource usage info
cluster{X}_clone {container_name}¶
Clone a container from a cluster machine to local
Destroying Local Containers¶
Important
Please read this bit
To do this cleanly you must do the following. Do not just delete the /var/lib/lxc/containername folder.
lxc-stop [container-name]
sudo lxc-destroy -n [container-name]
Local Disposable Containers¶
For things like developing container assets and testing out unknown software, disposable local containers are ideal.
You should regard these containers as disposable as they will only reside on your local machine and are therefore not backed up at all.
You have two main ways of working with local disposable containers
Clone the Scratch Container¶
If you want a more fully featured container, then you should probably clone the scratch container rather than building a new local one. This is a standard web server ready for you to play with.
To get a scratch container just run
cluster1_clone edmondscommerce-scratch
Note
Cloning the scratch container is probably the right thing to do most of the time.
Create Local Testing Container¶
There is a script located in
/opt/Projects/snippets-edmondscommerce/Cluster/shellscripts/desktop/lxc/create.bash
This predates the scratch container, though has been kept for reasons.
For normal usage, you should call the script without any of the optional arguments and you will get an empty centos7 machine ready for you to mess about with. If you do want another distro though you are free to do that.
Commands to Run:¶
Here is how to build a basic centos based container. This is only really useful for developing container assets or other highly disposable work.
Please ensure coding is being tracked in some form of git repo and regularly pushed.
Step One¶
cd /opt/Projects/snippets-edmondscommerce/Cluster/shellscripts/desktop/lxc
# Create the raw container
./create.bash [container name] (distroRelease defaults to 7) (distro defaults to centos)
# Attach as root (the slash means we use the raw lxc command rather than our alias
\lxc-attach -n [container name]
# Run the setup script that has already been copied in for you
bash -x /containerSetup.bash
# LEAVE THIS TERMINAL OPEN
Step Two¶
Create a new terminal but leave the other one open
You are now going to push container assets into your local container.
At a minimum you will want the general container but of course you can push whichever ones you want.
Note
This is the perfect way to push new container assets for testing. You can push as many times as you want. Each time you push it will delete existing files and push again which is great for development.
cd /opt/Projects/snippets-edmondscommerce/Cluster/shellscripts/desktop/lxc
sudo ./pushContainerAssetsToContainer.bash general test-test
Step Three¶
# Normal lxc command to connect as ec
lxc-attach [container name]
# and then run your container assets
```