Skip to main content
Self-hosting

Mastering Traefik with Docker Swarm - A Step-by-Step Guide to Setting up and Configuring Traefik using Docker Compose

Christian Schou

The ability to effectively manage incoming web traffic is essential in the world of contemporary web applications and microservices. Traefik enters the picture at this point. The popular load balancer and reverse proxy Traefik make it easier to direct incoming requests to the correct backend services.

This tutorial will look at the installation and configuration of Traefik using a supplied compose file. Whether you are managing a simple microservices architecture, a small business application, or a personal website, Traefik can help you improve the efficiency of your traffic management and the dependability of your services.

Traefik Labs: Say Goodbye to Connectivity Chaos
Reimagine your application connectivity and API management with Traefik’s unmatched approach to cloud native.

Introduction

We will use a pre-configured Docker Compose file that is available in my self-hosted GitHub repository to get things going. The components and configurations for Traefik are included in this compose file, which serves as the framework for our setup.

GitHub - Christian-Schou/self-hosted: Deploy 90+ open-source web apps with one Docker command
Deploy 90+ open-source web apps with one Docker command - GitHub - Christian-Schou/self-hosted: Deploy 90+ open-source web apps with one Docker command

It's important to have a fundamental understanding of Docker and Docker Compose before starting the setup process. It will be simpler to follow along and modify the configurations to meet your unique needs if you are familiar with these tools.

The setup of Traefik, understanding and modifying the compose file, and configuring Traefik to handle various scenarios will all be covered in detail in this article. We'll also look at integration with other services and offer troubleshooting advice to help you get past typical obstacles.

You will have a strong basis in installing and configuring Traefik by the end of this guide, giving you the ability to efficiently manage and route incoming web traffic in your own projects. Let's start with Traefik and dive in!

Prerequisites

There are a few prerequisites you should fulfill before setting up and configuring Traefik with the help of the provided compose file. Before beginning, let's look at what you'll need.

A. Required Software and Tools

  • Docker - Docker is required because Traefik is containerized using it, so make sure you have it set up on your system. To download and install Docker for your operating system, you can go read my tutorial about getting started with Docker or you can go to the official Docker website. Before continuing, make sure Docker is installed correctly and running.
  • Docker Compose - A tool for creating and running multi-container Docker applications is called Docker Compose. It enables us to easily manage the services and define our Traefik setup using a YAML file. Although Docker Compose is typically installed along with Docker, make sure it is set up and available in your terminal.

B. Basic Knowledge of Docker and Docker Compose

It helps to have a fundamental understanding of Docker and Docker Compose concepts in order to follow along with this tutorial. Learn the fundamentals of terms like volumes, networks, images, and containers along with the fundamentals of using Docker Compose, such as how to define services and manage multi-container setups.

If you are new to Docker, please read my tutorial below about Docker for beginners.

What is Docker? Docker beginner guide - Christian Schou
Get started with Docker and learn how to create your first container running Nginx and serve your own webpage written in HTML. Docker is a very powerful tool to run applications without having to install a lot of dependencies.

You are now ready to proceed with setting up and configuring Traefik using the provided compose file in my repository, as you have Docker and Docker Compose installed and you have a basic understanding of how to use them. Let's proceed to the following section!

Understanding the Compose File

Let's take a moment to look at the structure and components of the provided compose file before moving on to setting up Traefik. We can make any necessary adjustments and gain insight into the overall configuration by becoming familiar with the file.

The compose file we will be working with is available on the self-hosted GitHub repository. You can either clone the entire repository or simply download the traefik.yml file. I would recommend cloning the project into a directory on your computer or server. I/`ll show you how it´s done in the next section and why.

When you open the compose file, you'll notice several sections defined within the YAML structure. Let's discuss the important sections related to Traefik.

  • Services - The services that are active in our setup are defined in this section. The traefik service, which stands in for our Traefik instance, is defined in the compose file that is provided. It has settings for environment variables, ports, labels, and volumes.
  • Networks - The networks used by the services are described in this section. A traefik proxy network, which is used to link Traefik with other services, is included in the compose file.
  • Volumes - We can share files or store data on volumes so that containers and the host system can access them later. Volume definitions for Traefik's configurations, certificates, and the acme.json file (used by Let's Encrypt) can all be found in the compose file.
  • Labels - Docker's labels are a strong feature that let us add metadata to Docker objects. You'll see that the compose file makes extensive use of labels for the traefik service. These labels specify different Traefik configurations, including routing regulations, SSL certificates, and fundamental authentication.

We can alter the compose file to meet our unique requirements by comprehending its structure and component parts. For instance, we can modify the ports that are made available, add our own routing rules, or enable extra configurations like SSL certificates.

I'll go into detail about how to set up Traefik using the compose file in the upcoming section. I'll go through each section of the file and explain how it fits into the setup as a whole. Alright - let's see you how to create your own Docker Swarm, the Traefik overlay network, and how to deploy the whole traefik service with one command. Sounds awesome, right?! ✌️

Deploy the infrastructure

In order to run a Docker Swarm, we need some servers. I will be hosting three virtual private servers on Digital Ocean. Below is a step-by-step guide on how to deploy those.