Lightweight containers in Fedora using systemd
It was the year of 2015 and people were still developing new applications in PHP... but for those who could no longer accept the idea of installing a system-wide LAMP stack, there was a new-old fassionable thing: Containers!
This is a quick howto for creating a throw-away container for messing around with PHP apps on Fedora. Most of it is taken from the examples at the bottom of 'man 1 systemd-nspawn'
Create the directory for the container filesystem:
[user@host]$ mkdir web-dev-container
Install a minimal Fedora image (including apache and php) into the new container root:
[user@host]$ sudo yum -y --releasever=21 --nogpg --installroot=$(pwd)/web-dev-container --disablerepo='*' --enablerepo=fedora install systemd passwd yum fedora-release less vim iproute httpd php
Update the SELinux context:
[user@host]$ sudo semanage fcontext -a -t svirt_sandbox_file_t "web-dev-container(/.*)?"
[user@host]$ sudo restorecon -R web-dev-container
Spawn the new container:
[user@host]$ sudo systemd-nspawn -D web-dev-container
In the new container, change the root password:
[root@container]# passwd
[root@container]# logout
Boot the new container:
[user@host]$ sudo systemd-nspawn -bD web-dev-container
Enable and start the web server and check that it's running:
[root@container]# systemctl enable httpd.service
[root@container]# systemctl start httpd.service
[root@container]# ss -lt
Check http://localhost in a web browser.
Add an index.html file:
[root@container]# echo "Hello World" >> /var/www/html/index.html
[root@container]# echo "<?php echo \"Hello PHP World\" ?>" > /var/www/html/index.php
Refresh http://localhost
Check http://localhost/index.php
You can now logout of the container (it will keep running).
Change the ownership and hack away!
[user@host]$ sudo chown -R user.user web-dev-container/var/www/html
[user@host]$ vim web-dev-container/var/www/html/index.php
Login to the container, if needed:
[user@host]$ sudo machinectl login web-dev-container
Exit the container by pressing ^] three times in one second.