How to set up the Hass.io Docker image for productivity

We need to add a few package to the default Docker image that hosts our Hass.io configuration files.

Docker only installs the packages specified in the Dockerfile that defines the image. This helps manage dependencies and keeps the image as small as possible.

However, we need some additional tools installed so we can easily modify the configuration files and version control them.

Specifically, we need vim & git. Sorry emacs fans, you are wrong here :). Vim is derived from the oldest text editors on the UNIX system and has been available on every Linux system since the beginning. Learning vim is a vital skill for any developer. Vim’s ability to chain together commands is incredibly powerful and makes editing very efficient for a power user.

Nowadays, everyone is using IDEs like VS Code, Visual Studio, IntelliJ, or even good old Notepad++. I will admit, I am mostly using VS Code both at work and at home. However, the first plugin I install is the VIM key bindings. The vim way of doing things is just too powerful and productive to give up (even if the GUI of VS Code is very helpful, it is not as powerful).

Since we will be SSH-ing in to the Raspberry Pi and it has a relatively low power processor, using vim is the most efficient and fastest way to get our work done. No shame if you want to RDP into your Raspberry Pi and install an IDE (or use SAMBA), but I prefer simple and fast.

  • Launch PuTTY and connect to your Raspberry Pi (if you need a refresher, please see my previous blog post).
  • Notice we are in the home directory of the “pi” user
pwd
/home/pi        <-- this is not a command, this is the expected output
  • We need to “login” to the Docker container that hosts Hass.io.
    • This command will sudo (elevate our privileges), docker (run a Docker command), exec (run a command in a Docker container), -it (start an interactive session), homeassistant (create the session on the homeassistant container, /bin/bash (open a Bash shell).
sudo docker exec -it homeassistant /bin/bash

Bash is the most popular Linux shell (command interpreter) and is another vital part of your professional software development toolkit.

After starting the Bash shell, we can see that we are in the “config” directory under “root” inside the container.

pwd
/config

If you “list” the directory, you can see the various configuration files Hass.io uses.

ls -aw 1
.
..
.august.conf
august.yaml
automations.yaml
binary_sensor.yaml
camera.yaml
climate.yaml
.cloud
configuration.yaml
customize.yaml
deps
device_tracker.yaml
.git
.gitignore
glances
groups.yaml
.HA_VERSION
home-assistant.log
home-assistant_v2.db
home-panel-config.json
home-panel.db
http.yaml
.ios.conf
ios.yaml
known_devices.yaml
light.yaml
logger.yaml
neato.yaml
notify.yaml
options.xml
OZW_Log.txt
panel_iframe.yaml
pyozw.sqlite
.ring_cache.pickle
ring.yaml
scene.yaml
scripts.yaml
secrets.yaml
sensor.yaml
.storage
switch.yaml
tts
ui-lovelace.yaml
.uuid
zwave.yaml
zwcfg_0xd53a21b8.xml
zwscene.xml

These are the YAML files that control your Hass.io installation. As you can see, there are lots of files (mine will have more than yours if you are just starting out).

However, if you tried to run git or vim now, you will get an error.

git
bash: git: command not found

vim
bash: vim: command not found

Again, this is because there is nothing installed in the Docker container that was not in the Dockerfile originally. So I don’t have to modify the default Dockerfile, let’s add the needed packages manually each time I login.

apk add git
apk add vim

Apk is a package management system used by our running container. We need to use the add command to install the two packages we need (technically, we don’t need vim since vi is installed by default, but that would just be barbaric :)).

Running these 3 commands each time we login to our Raspberry Pi to edit the configuration will get tedious, so let’s cause it to happen whenever we login. Exit the bash shell you are currently in (make sure you are in the home directory for the pi user).

exit
exit       <-- this is not a command, this is the expected output

pwd
/home/pi   <-- this is not a command, this is the expected output
  • Use vim to create a new file in the home directory of the pi user.
    • A .bash_profile is a “hidden” file that is executed whenever a new bash shell is instantiated (like when you login to the Raspberry Pi).
vim .bash_profile

Vim has 2 modes of operation (command & insert). This is the hardest concept to learn since this is the complete opposite of most of the WYSIWYG editors you are used to using in Windows (or emacs). Vim command mode is for moving around the editor window and manipulating text. Vim insert mode is for actually typing in text.

  • Make sure you are in insert mode (press i if you are not sure, it will say — INSERT — at the bottom of the screen). Enter the following text.
sudo docker exec -it homeassistant /bin/bash
  • Press Esc, then :wq. Congratulations! You have successfully gotten over the most asked question on StackOverflow; you have successfully exited vim.

Now close PuTTY and reopen. You will see that you are no longer logged into the default pi user anymore. You have been logged into the Hass.io Docker container.

Now we need to ensure vim and git are installed whenever you login (in a future article, I will figure out how to actually modify the Dockerfile and install these packages by default, but I’m not sure how to make sure I am always up to date with the Hass.io image, so it will have to wait).

  • Create a .bashrc file in the root home directory (it will get run whenever you login to the container and install the needed packages).
vi ~/.bashrc           <-- open vi (not vim, not installed yet)

apk add git            <-- install git
apk add vim            <-- install vim

:wq                    <-- save and quit vi

Now close PuTTY and reopen. You will see that you are no longer logged into the default pi user anymore. You have been logged into the Hass.io Docker container and the 2 most important tools have been installed for you (which is the command to see where an executable is located on the filesystem).

pwd
/config

which git
/usr/bin/git

which vim
/usr/bin/vim