GitOps on Docker using Portainer

Navratan Lal Gupta
Linux Shots
Published in
5 min readOct 22, 2022

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.

GitOps with Portainer

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.

Portainer

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.
Portainer home page
  • Click on stacks → Add stack
Environment dashboard
Stack
  • In add stack form, Select Repository tab and Fill the information
Add stack form — 1

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.

Add stack form — 2

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.

Deployed stacks

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

Containers in stack

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.

Sonarr v3.0.9

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.

Commit the change in 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.

Updated Sonarr
Sonarr v4.0.0

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.

https://www.buymeacoffee.com/linuxshots

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

--

--

Navratan Lal Gupta
Linux Shots

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