Helpful Linux Commands

Setting Static IPs

Good common practice when running services on VMs is to have everything running on static IP addresses. This can be done through your DHCP server or in your VM itself. The easiest way to do this on Ubuntu Server is through netplan. Use whatever addressing scheme you have setup in your network.

sudo nano /etc/netplan/01-netcfg.yaml
network:
 version: 2
 renderer: networkd
 ethernets:
  ens5:
   dhcp4: no
   addresses: [192.168.1.6/24]
   gateway4: 192.168.1.1
   nameservers:
    addresses: [192.168.1.1]
sudo netplan apply

Mounting SMB Shares

Often in a VM you will be utilizing external or shared storage on your network. In Ubuntu Server, we can utilize the fstab to mount these on boot.

sudo apt install cifs-utils
sudo nano /etc/fstab
//truenas.jellayy.com/Data /Data cifs credentials=/etc/share-credentials,uid=1000,gid=1000 0 0

Create credentials file:

sudo nano /etc/share-credentials
username=user
password=password

Secure credentials file:

sudo chown root: /etc/share-credentials
sudo chmod 600 /etc/share-credentials

Mount share:

sudo mount -a

Installing Docker and Portainer

A great way to deploy services, even on VMs, is through Docker. You can follow Docker’s official guide for installing on Ubuntu here. However, below will be some abridged commands for some easy copying and pasting:

sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo \
  "deb [arch=amd64 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

sudo apt update

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

sudo docker run hello-world

If hello world displayed its output that means everything is working great. For the future we’re going to make things easier by adding your user to the docker group. Make sure to log out and back in after doing this.

sudo usermod -aG docker <YOUR-USER>

Then, getting portainer running is as easy as:

docker volume create portainer_data

docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce

Have Docker wait for Filesystem Mounts

Odds are, you have multiple servers that depend on central storage that is mounted on boot. Sometimes this can take a bit and things like Docker can spin up your containers before their mounted storage is ready, causing them to lose access to their data and begin writing to the local disk.

Thankfully, if you mount via fstab, each mount is given a unit in systemd. This means you can have the Docker unit depend on your mount units via systemd.

First, search for the unit for your mount:

$ systemctl list-units -t mount | grep mymount
mymount.mount

Then edit the docker unit at /lib/systemd/system/docker.service and add the following to its configuration:

[Unit]
Requires=mymount.mount
After=mymount.mount

There will be other existing units in these blocks, you only need to add these to the existing lists

Leave a Reply

Your email address will not be published. Required fields are marked *