Important Components of a Container
image: linuxadvocates.com |
To save time let's just list out some important points:
- All of your containers and their configuration files are created under the /var/lib/lxc/container-name directory by default. You are able to modify this directory if you would like but Ill just be sticking with this because it can get messy as templates and other configuration files use this as default. One thing I will do though is mount that directory on its own BTRFS filesystem.
- Each container is assigned its own root filesystem (rootfs) and is maintained in a fstab file. This is one of the coolest things about LXC in my opinion. When you log into a container it basically feels like you are actually inside of a full Linux operating system with the normal filesystem structure. This makes the user feel right at home!
- Containers run-time configurations are maintained in its config file. This can be modified as needed. This file maintains networking, cgroups, hostname, filesystems, etc.
- The first linux container can take some time to create but each time a container is created from a template it is stored in /var/cache/lxc/. The next time you create from the same template, cache will allow it to create quickly.
- Containers are created from templates that are located /usr/share/lxc/templates/lxc-[name]. If you want to modify how the container is built you can modify the templates. These are just shell scripts. Templates contain things like root password, cache base, default path for container files, default container configs, etc. When you install LXC you are given default templates so check the directory to see what you can build.
- Each container has its own log file under /var/log/lxc/container-name.log which may come in handy for troubleshooting.
Common Commands
List containers on the host.
lxc-ls
lxc-ls --fancy ***shows state and IP address.
Create a new container.
lxc-create -t TEMPLATE -n CONTAINER_NAME
Start a container. The "-d" option starts container without attaching.
lxc-start -n CONTAINER_NAME
lxc-start -n CONTAINER_NAME -d
Start a process inside a container. This is like sending a remote command to the container. If no command is given however the current default shell of the user will be looked up inside the container and executed. This will make it appear that you are inside the container when in fact you will not be.
lxc-attach -n CONTAINER_NAME command
Launch a console for the container. To exit the container use the keystrokes ctrl+a and then hit q at any time.
lxc-console -n CONTAINER_NAME
See specific processes running inside a container.
lxc-ps -n CONTAINER_NAME
Stop a container.
lxc-stop -n CONTAINER_NAME
Delete a container.
lxc-destroy -n CONTAINER_NAME
Clone a container.
lxc-clone CONTAINER_NAME NEW_CONTAINER_NAME
Let's Create our First Container!
# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.15.6-200.fc20.
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled
--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled
--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /bin/lxc-checkconfig
2. Create the container. We will be creating from a default container template. I can show ways to create custom containers in an advanced LXC demo. Remember also to check out lxc-create man page.
# lxc-create -t TEMPLATE -n CONTAINER_NAME
Replace TEMPLATE with one container templates supplied in /usr/share/lxc/templates/lxc-[name] and also replace CONTAINER_NAME with your desired name. Example: replace TEMPLATE with fedora and CONTAINER_NAME with fedora-container to create a Fedora container named "fedora-container".
NOTE: This will most likely take some time to complete.
3. Once this completes you can verify its complete and then start up the container. Remember the "-d" flag with the lxc-start command to not attach to container while starting it.
# lxc-ls --fancy
# lxc-start -n CONTAINER_NAME -d
If you check the status of your containers once again you should be able to see that your container is now running.
4. Start playing around with your container. You should start getting familiar with your container. Try sending some commands to it with lxc-attach and get a console session going with lxc-console.
Examples:
# lxc-attach -n CONTAINER_NAME top
# lxc-console -n CONTAINER_NAME
When you console into your container try running some normal Linux commands and read/create some files like you would on a normal Linux machine. This will help you get familiar and help you see some of the differences between a container and a full blown OS.
That covers creating your first container! After I created my first couple containers and started playing with the different commands, I was able to become familiar and comfortable very quickly. Once I started playing with LXC the whole Linux container technology started to "click" and make sense and that's why I started this blog series with plain ole LXC. Please check back soon for some advanced container configurations in the next blog post and some Docker tutorials in the near future. Will also be following up with a video on this soon that covers what I have gone over in the past couple of posts. I will add to this page.
Blog Series on Linux Containers:
Previous Post: Control Groups
Next Post: Advanced Configuration