Many of you already know that I moved all my web applications into containers on Fedora Atomic image based hosts. In the last few weeks, I moved a few of them from Docker to rkt on Fedora 25. I have previously written about trying out rkt in Fedora. Now I am going to talk about how can we build our own rkt based container images, and then use them in real life.
Installation of rkt
First I am going to install all the required dependencies, I added htop and tmux and vim on the list because I love to use them :)
$ sudo dnf install systemd-container firewalld vim htop tmux gpg wget $ sudo systemctl enable firewalld $ sudo systemctl start firewalld $ sudo firewall-cmd --add-source=172.16.28.0/24 --zone=trusted $ sudo setenforce Permissive
As you can see in the above-mentioned commands, rkt still does not work well with the SELinux on Fedora. We hope this problem will be solved soon.
Then install the rkt package as described in the upstream document.
$ sudo rkt run --interactive --dns=126.96.36.199 --insecure-options=image kushal.fedorapeople.org/rkt/fedora:25
The above-mentioned command downloads the Fedora 25 image I built and then executes the image. This is the base image for all of my other work images. You may not have to provide the DNS value, but I prefer to do so. The --interactive provides you an interactive prompt. If you forget to provide this flag on the command line, then your container will just exit. I was confused for some time and was trying to find out what was going on.
Building our znc container image
Now the next step is to build our own container images for particular applications. In this example first I am going to build one for znc. To build the images we will need acbuild tool. You can follow the instructions here to install it in the system.
I am assuming that you have your znc configuration handy. If you are installing for the first time, you can generate your configuration with the following command.
$ znc --makeconf
Now below is the znc.acb file for my znc container. We can use acbuild-script tool to create the container from this image.
#!/usr/bin/env acbuild-script # Start the build with an empty ACI begin # Name the ACI set-name kushal.fedorapeople.org/rkt/znc dep add kushal.fedorapeople.org/rkt/fedora:25 run -- dnf update -y run -- dnf install htop vim znc -y run -- dnf clean all mount add znchome /home/fedora/.znc port add znc tcp 6667 run -- groupadd -r fedora -g 1000 run -- useradd -u 1000 -d /home/fedora -r -g fedora fedora set-user fedora set-working-directory /home/fedora/ set-exec -- /usr/bin/znc --foreground # Write the result write --overwrite znc-latest-linux-amd64.aci
If you look closely to the both mount and port adding command, you will see that I have assigned some name to the mount point, and also to the port (along with the protocol). Remember that in the rkt world, all mount points or ports work based on these assigned names. So, for one image HTTP name can be assigned to the standard port 80, but in another image, the author can choose to use port 8080 with the same name. While running the image we choose to decide how to map the names to the host side or vice-versa. Execute the following command to build our first image.
$ sudo acbuild-script znc.acb
If everything goes well, you will find an image named znc-latest-linux-amd64.aci in the current directory.
Running the container
$ sudo rkt --insecure-options=image --debug run --dns=188.8.131.52 --set-env=HOME=/home/fedora --volume znchome,kind=host,source=/home/kushal/znc,readOnly=false --port znc:8010 znc-latest-linux-amd64.aci
Now let us dissect the above command. I am using --insecure-options=image option as I am not verifying the image, --debug flag helps to print some more output on the stdout. This helps to find any problem with a new image you are building. As I mentioned before I passed a DNS entry to the container using --dns=184.108.40.206. Next, I am overriding the $HOME environment value, I still have to dig more to find why it was pointing to /root/, but for now we will remember that --set-env can help us to set/override any environment inside the container.
Next, we mount /home/kushal/znc directory (which has all the znc configuration) in the mount name znchome and also specifying that it is not a readonly mount. In the same way we are doing a host port mapping of 8010 to the port named znc inside of the container. As the very last argument, I passed the image itself.
The following is the example where I am copying a binary (the ircbot application written in golang) into the image.
#!/usr/bin/env acbuild-script # Start the build with an empty ACI begin # Name the ACI set-name kushal.fedorapeople.org/rkt/ircbot dep add kushal.fedorapeople.org/rkt/fedora:25 copy ./ircbot /usr/bin/ircbot mount add mnt /mnt set-working-directory /mnt set-exec -- /usr/bin/ircbot # Write the result write --overwrite ircbot-latest-linux-amd64.aci
In future posts, I will explain how can you run the containers as systemd services. For starting, you can use a tmux session to keep them running. If you have any doubt, remember to go through the rkt documents. I found them very informative. You can also try to ask your doubts in the #rkt channel on Freenode.net.
Now it is an exercise for the reader to find out the steps to create an SELinux module from the audit log, and then use the same on the system. The last step should be putting the SELinux back on Enforcing mode.
$ sudo setenforce Enforcing