Docker Compose for Newbies

As with most new things, if they don’t come intuitively or naturally, they can appear difficult, possibly even scary.

There have been so many concepts that I have put off learning because I was intimidated and didn’t want to commit the brain power after getting home from my 9-5 job. I definitely understand. But funnily enough, most of these things didn’t require a fraction of the effort I was expecting to have to commit. They were simple afternoon projects that just needed a few youtube video walk-throughs or even a handy blog post. I hope to drive that sentiment home today by demystifying docker-compose.

Docker Compose is best described as an orchestration tool for building, managing and organizing your docker containers using a configuration file. It allows for the control of multiple containers through a unified interface allowing them to run smoothly together without any conflicts.

In simplified terms, docker compose allows you to spin up several apps with a single declaration. This declaration is usually in the form of a ‘Docker-compose’ file. See? Pretty user-friendly naming convention.

Docker Compose files are written in YAML, a human-readable data-serialization language that is often used in configuration files. The compose file spells out specifics about the containers that will be downloaded and spun up. It will likely list specific container, version, ports, file locations, relevant timezone, and much more.

Let’s take a look at an example of a Docker Compose file below.

This file (titled compose.yaml) will do the following:

  • Pulls down the latest Home Assistant Docker Image
  • Specifies the host network
  • Sets the PUID/PGID
  • Sets the Timezone to ETC/UTC
  • Specifies the path where the config files are stored
    • replace /path/to/data with the host directory.
  • Specifies the ports used to access the application
  • Devices specifies any USB access you want to expose to the container
    • For Home Assistant this comes in handy with zigbee controllers or other external devices. It can also be used to specify igpu access for Plex servers, but that’s not important right now.
---
services:
homeassistant:
image: lscr.io/linuxserver/homeassistant:latest
container_name: homeassistant
network_mode: host
environment:
- PUID=1000
- PGID=1000
- TZ=Etc/UTC
volumes:
- /path/to/data:/config
ports:
- 8123:8123
devices:
- /path/to/device:/path/to/device #optional
restart: unless-stopped

Note: This tutorial assumes that you have docker already installed and you are operating from the linux command line. Docker compose comes installed in the latest version of docker so if you followed my tutorial on installing docker, you should be able to follow along nicely. (If you are looking to install Docker on windows you can check out this article)

Step 1: Create a folder titled “home_assistant”

mkdir home_assistant

Optional: Create a Docker folder and add the subfolders for each docker container. Use each one as your ‘/config’ storage folders. It helps in the long run with docker organization.

Step 2: Using your favorite text editor, copy the compose file from above and save it into a text file called “compose.yaml” and save it into the recently made folder.

sudo nano compose.yaml

Step 3: From the directory with the new “compose.yaml” file in it, run the following command:

sudo docker compose up -d

This tells docker to utilize your compose file to spin up the docker container with all of the specified parameters, and to run it as a daemon in the background.

Docker will do its thing: show a bunch of spinning wheels and items loading. This is normal. After it is complete, you should be prompted back at your terminal. You can then navigate to the ports specified in the docker compose file and you should be up and running.

If we assume your IP address is 10.0.4.1, navigate to http://10.0.4.1:8123

Boom. You just used Docker Compose to spin up your own Smart Home Assistant. See? Not so bad. Now go forth and keep on learning.

Leave a comment