Installing Docker, and Portainer on Ubuntu Linux

I run my smart home on an Intel NUC computer, running Ubuntu Linux and Docker. This allows me to run many different smart home services as containers, including Home Assistant.

This blog post is a companion post to my YouTube video showing you how to install Docker, Docker Compose, Portainer and Home Assistant on Ubuntu Linux. This combination of applications forms an ideal foundation for any advanced smart home.

Before you get started you will need a few things:

  1. A dedicated computer, laptop or Intel NUC PC. I personally use an Intel NUC PC and it’s been great for running dozens of services.

  2. Ubuntu Linux installed. You can follow the instructions here to get it installed.

  3. A Static IP address assigned to the Ubuntu Linux computer.

  4. The ability to SSH into this Linux installation with a user that has root/sudo permissions.

Installing Docker

The first thing we’ll do is install Docker. I find it easiest to follow the steps on the official Docker installation page.

First SSH into the Ubuntu server.

SSH into your Ubuntu Server

Now we’ll install the Docker Engine for Ubuntu by running the following commands.

First we remove any older versions of Docker Engine, if they exist. You’ll probably have none if this is a fresh install of Ubuntu Server

sudo apt-get remove docker docker-engine docker.io containerd runc

Now we update theĀ aptĀ package index and allow it to use HTTPS with these two commands.

sudo apt-get update

sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add Dockers official GPG key to the KeyRing

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Set up the file repository for the Stable version of Docker (You can run a beta or experimental one if you want, but I prefer to use the stable versions)

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Finally, install the Docker engine using these two commands

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Congratulations! You now have Docker installed!

You can verify that everything is working by running the Hello World docker container. If everything is working properly you’ll see the container output a message to the screen and shut itself down.

sudo docker run hello-world

Manage Docker as a non-Root user

I find it easiest if I can manage Docker as a non-Root user – it means I don’t need to add sudo to the start of every command. This may reduce the security of my Docker environment, but because it’s running in my own network, inaccessible from the outside world I’m willing to accept that risk.

To get Docker to work as a non-root user we need to add our user to the Docker group. Let’s do that now.

First, create the Docker group if it doesn’t already exist.

sudo groupadd docker

Now add your user account to that Docker group.

sudo usermod -aG docker $USER

Now log out and back in again for the permission changes to take effect. If everything has worked properly, you should be able to re-download and run the Hello World Docker container without using sudo.

docker run hello-world

Installing Docker Compose

Docker Compose is a really handy utility that allows you to create and configure Docker containers using a YAML file. This makes it really easy to create and configure many containers at once, and also to backup your environment.

Installing Docker Compose is really easy, just copy and paste a few more commands into the SSH terminal. We start by downloading the current, stable release of Docker Compose.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Now we set the permissions on the downloaded files to allow us to run them.

sudo chmod +x /usr/local/bin/docker-compose

And we’re done! Docker Compose is now installed and we can test this by running it and having it output the version.

docker-compose --version

If everything has worked as expected, you should see the version information displayed on screen.

Installing Portainer

Portainer is a graphical user interface that allows you to view and manage your Docker containers. It makes it way easier to interact with Docker and I highly recommend that you install it. The best part is, it can actually be installed on Docker itself!

Portainer showing the Docker containers that I use in my smart home

To install Portainer, we need to first create a Docker Compose YAML file. This file contains all the information needed to create and start the container.

I like to keep all my Docker configuration files in my /opt directory on my Ubuntu server, so this is where we will create our Docker Compose file. First, let’s navigate to the /opt directory

cd /opt

Then we will use the Nano text editor to create a new file called docker-compose.yaml

sudo nano docker-compose.yaml

Now copy and paste this configuration information into the Nano text editor. If you want more information about what all these keys do, check out the YouTube video I linked at the top of this post. I go through all these items in more details.

You will see that this will map a directory from /opt/portainer on your Ubuntu machine to /data inside the Portainer container. This is great because it means you can recreate your container at any time and all of the configuration data will be safe and sound on your host computer. I make sure that all of my containers have their configuration data stored in its own folder in the /opt directory so I can easily update configurations and take backups.

version: '3.0'

services:
  portainer:
    container_name: portainer
    image: portainer/portainer-ce
    restart: always
    ports:
      - "9000:9000/tcp"
    environment:
      - TZ=Europe/London
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/portainer:/data

Now press CTRL + X to exit Nano and Y to save the file. If you do an ls -l on the /opt directory you should now see the file there.

We can now start Docker using the following command. Make sure you are in the /opt directory when you run this command, as it needs to be run from the same place that you keep your docker-compose.yaml file.

docker-compose up -d

This will bring up the Docker containers that you have specified in the compose file, and the -d will start them in Detached mode which means they will run in the background.

You should now be able to navigate to the IP address of your Ubuntu Server in a web browser on port 9000

http://<IPADDRESS>:9000/

The first time you load Portainer you will be asked to create a username and password. This will only need to be done the first time you load it up.

Now select the LOCAL option, so that you can start managing containers locally on your Ubuntu server, and click Connect.

And you should now see Portainer running! You can select Containers in the left hand navigation menu and see your Portainer container running!

Archives