Deploy a Golang web application to the cloud with Docker Swarm

In a previous article, the author showed how to build and deploy a Golang Web application (or API backend for a frontend framework like React or Vue) to a cloud provider, using Docker as a process manager. In this post, which is part of two of this series, he will show you a second method of deploying a Golang Web app or backend to any cloud, using Docker Swarm. The most significant difference here is that, instead of using Docker as a process manager this example uses Docker Swarm, a container orchestrator.

Golang Docker Deployment Method 2

Docker Swarm is a container orchestration engine from Docker.

It is integrated into Docker, and makes the setup easier than alternatives but lacks some basic functions you would require to host a simple web application.

Examples on this post are similar to the previous post and can be found here on Github (note the branch name). As a result some parts of the post are similar to the last one, including the script to build the required images with a unique tag. Please see the previous post for the build steps.

Diagram of Docker Swarm Mode
Docker Swarm Mode with Golang

When using Docker Swarm with Golang, our app is hosted as a container on Docker and gets a random port assigned to it by Docker. Instead of us running a reverse proxy like HAProxy in front of our services, Docker Swarm runs an “Ingress Load Balancer” in front of our services. This Ingress Load Balancer does 2 main things:

  1. It exposes the application on a Published Port (like 80 in our configuration below) to the outside world and sends the inbound traffic to our app.
  2. It ensures that the traffic reaches a running instance of the application, regardless of the server (node) that receives it.