Post

Getting Started with Docker

Getting Started with Docker

A Primer for Docker

Docker is a great way to get into ‘containerization’, which can be thought of like spinning up a small virtual machine that only contains the critical software required to fulfill a specific purpose. You can pull down precompiled ‘images’ that allow you to spin up services very quickly with minimal effort, and even have a way to move those setups around easily with docker-compose (now just docker compose without the dash).

The benefit of using Docker as opposed to lots of VMs is how lightweight the containers are, they don’t need to be fed a certain number or CPU cores/RAM to be full allocated forever, they can scale on demand up to using the entire system.

If you are using Ubuntu, the easiest way to get started with Docker and Docker Compose is by following Docker’s guide here. I would also recommend following along the next part of the guide to run Docker commands with your regular user and not via root/sudo. Otherwise all docker commands must run as sudo.

Building Blocks for Learning Docker

One of the main things you need to decide early on is how you will backup all your Docker containers. This is important as after you spin up a new container and spend time configuring it, if you change the volumes/bind mounts around you can very easily blow away your data and need to start again.

This drives us to the main point of this post, which is relating to Docker volumes vs. local binding of folders.

Docker Volumes

If you let Docker make volumes for your containers and do not bind the important folders (containing database or config files, etc.) to your local disk, when you run the docker compose down command, the volume that hosted your container’s storage is now an orphan and could be deleted.

Bind Mounts

You can mount local files/folders and let your container directly access them, which can help with initial setup as well as being able to backup/move your container’s data if you want to migrate hosts for example.

For every container that I can (almost all except a few exceptions, e.g. Postgres databases) I locally bind critical folders within containers to NFS mounts that are automatically mounted at boot via my /etc/fstab in Ubuntu. Mounting the folders over the network makes it so that I do not need to migrate anything if my Docker host goes down, I set the new PC IP to the same as the old one, and then just run docker compose up -d on the new Docker host and I am completely back up and running.

I actually had this happen a few weeks ago and I got out a spare MiniPC and was back up and running within about an hour, which would have been faster except I moved to Ubuntu 24.04 which changed where the static IP settings hide versus where I expected them in /etc/dhcpcd.conf.

Post Install - Where Do I Go?

First step is to decide where you will put all your docker container files. Over the years I have gone through several stages:

  • Started with DockStarter which obfuscates where all the compose files are
  • Moved to just docker-compose.yaml files manually on a NFS share Now manage all my docker containers via Dockge once they are up and running. I love DockGe as it has a direct way to access container shells, which normally requires some longer docker commands that I never get right the first time

Dockge

To get started with Dockge, all you need to decide is:

  • The port you will access Dockge with (e.g. an open port on the machine you are on), defaults 5001
  • The base folder of where you will put all your docker container/stacks

Interactive docker-compose yaml generator from Dockge: https://dockge.kuma.pet/

Below is my current Dockge docker-compose.yaml for reference. Have my NFS at /mnt/nas/docker, with the dockge docker-compose.yml at /mnt/nas/docker/dockge/docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version: "3.8"
services:
  dockge:
    image: louislam/dockge:1
    container_name: dockge
    restart: unless-stopped
    ports:
      - 5001:5001
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./data:/app/data
      # Stacks Directory
      # ⚠️ READ IT CAREFULLY. If you did it wrong, your data could end up writing into a WRONG PATH.
      # ⚠️ 1. FULL path only. No relative path (MUST)
      # ⚠️ 2. Left Stacks Path === Right Stacks Path (MUST)
      - /mnt/nas/docker:/mnt/nas/docker
    environment:
      # Tell Dockge where to find the stacks
      - DOCKGE_STACKS_DIR=/mnt/nas/docker

To start Dockge, all you need to do is navigate your shell to /mnt/nas/docker/dockge/ and docker compose up -d (-d to disconnect and let the container run or you can omit to stay connected to the container and it will close when you Ctrl+C to exit the container)

Now to setup any more conatiners, go pull the docker compose yaml file for the project, setup all the config folders and get to setting up your new docker containers!

Trending Tags