Linux Shots
Published in

Linux Shots

GitOps on Docker using Portainer

GitOps is a practice of operations which uses central git repository (Such as Github or GitLab or any other git repos) as ‘Single Source of Truth’ for a deployment. Deployment configurations are managed and updated directly from git repo.

This helps centralizing our application deployment configurations at one place and any further changes are only done through git. All changes to code are tracked, making updates easy while also providing version control if in case rollback is needed. We can also integrate it with CI pipeline to perform continuous delivery (CD) of applications.

When we check about GitOps, we mostly find it for Kubernetes clusters either using Argo CD or Flux CD. But using Portainer, we can enable GitOps on our docker standalone as well as docker swarm clusters.

I have already covered installation part of Portainer on docker in another article here.

Contents

  1. What is Portainer ?
  2. Why it works on my machine ? (Pre-requisites)
  3. Setup a GitOps
  4. Updating a deployment (By making changes to Git repo)

What is Portainer ?

Portainer is a Web User Interface (WUI/GUI) dashboard tool, which can be used to monitor and centrally manage a docker and Kubernetes platform.

If you wish to know more about Portainer, I have already wrote an article on it. Please read it here. This is highly recommended if you don’t already know about it. You may also choose to install Enterprise (Business) edition. As of today, Portainer is providing Enterprise Edition free of cost for use up to 5 nodes. Find this link to claim it: https://www.portainer.io/take-5

Why it works on my machine ?

I am running docker on my local machine with below configurations/specifications.

  1. Ubuntu 22.04 server
  2. Docker version 20.10.18
  3. Docker compose version 2.10.2
  4. Portainer CE 2.15.1

Setup a GitOps using Portainer

We will deploy and setup GitOps for media-stack. To know more about my self-hosted media stack (Jellyfin + Radarr + Sonarr + Prowlarr/Jackett + qBitTorrent/Transmission + VPN), read this article.

  1. Create a new stack using Portainer
  • Go to home page and select the docker environment. Here, I am selecting local environment.
  • Click on stacks → Add stack
  • In add stack form, Select Repository tab and Fill the information

Name — Give the name of stack. Here, I have used ‘media-stack’.

Repository URL — Add repository URL where your docker compose file resides. I am using my GitHub repository URL.

Repository reference — In this field, We need to add branch name or tag name which we want to deploy. It should be in format refs/heads/branch_name or refs/tags/tag_name. I have a demo branch for this demo.

Compose Path — Give the file name of compose file.

Authentication — You can enable the authentication, if your repository is private and add Github/repository username and password. In my case, Its public repository so I don’t require authentication.

Automatic Updates — Enable the automatic updates. This will make sure your GitOps is setup. This checks your Git Repository at regular interval (default 5 minutes) and compare it with current state. If current state doesn't equal to desired state (state configured in git repo), It will re-deploy the stack with latest update in repo. You may change the fetch interval as per your requirement.

Pull latest image — If enabled, It will always pull the image from registry even if image is already present in node. This is EE feature.

Force redeployment — If enabled, it will forcefully redeploy the stack at every interval. Its required if you have aggressive requirement of ‘sync with Git’. This is EE feature.

Environment Variables — You may add environment variables which will be set on node before deployment. It will also replace occurrence of all your variables ${ENV_VARIABLE} in docker compose file with its value before deploying it. Here, I have used COMPOSE_PROFILES environment variable to set -profile=stack-1 flag in docker compose.

That’s it. Click on deploy the stack. Grab a cup of coffee and wait until all images are pulled and stack is deployed.

After sometime, You will see your deployed stack in dashboard. You can check containers and access applications.

Here, Lets see if my Sonarr is working or not. We can see Sonarr is working fine and its current version is v3.0.9. We will try to update it in next section.

Updating an application

Now, After few months, A new version of our application is released. And we want to upgrade our deployment. I see v4 of Sonarr is already released. Lets upgrade our application.

To upgrade our application, We only need to make changes in compose file in git repo.

After a commit is made in git repo. Portainer checks the git repo within 5 minutes, And if it finds changes, It will update the application.

Lets grab another cup of coffee and wait for our Portainer to detect the new commit and make changes in our deployment.

After, 5 Minutes (or sooner), You will see new version of Sonarr is deployed.

That’s it for today. I hope you learned something new.

If it helped, You may choose to share a cup of coffee on https://www.buymeacoffee.com/linuxshots or by scanning below QR code.

Don’t forget to get license of Portainer EE for up to 5 nodes, free of cost. Check this link — https://www.portainer.io/take-5

Thanks

Navratan Lal Gupta

Linux Shots

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Navratan Lal Gupta

Lets talk about Linux, DevOps, Kubernetes, Docker, opensource and Cloud technology. Don't forget to follow me and my publication linuxshots.