How to use ZeroTier in Docker on Linux
ZeroTier is impressive software that enables users to create a “modern, secure, multi-point virtualized network”. It has a wide range of applications, but one of its standout uses is integrating it into Docker containers to facilitate easier connectivity. Here’s a guide on how to integrate ZeroTier into your Docker containers.
How to create your ZeroTier Dockerfile
If you want to integrate ZeroTier into Docker, you’ll have to create a custom Docker image. This process involves utilizing Dockerfiles, which act like recipes or playbooks (similar to Ansible) for building images.
To begin, open a terminal window. Then, use the mkdir
command to create a folder named “zerotier-container.” This directory will help keep your project organized.
mkdir -p zerotier-container
cd ~/zerotier-container
Once your directory is set up, use the touch
command to create an empty “Dockerfile.” We’ll fill in this file later in the guide, but it will remain blank for now.
touch Dockerfile
Next, open the newly created “Dockerfile” in the Nano text editor.
nano -w ~/zerotier-container/Dockerfile
In the Dockerfile, insert the following code. This code outlines a basic image setup: an Ubuntu system with ZeroTier installed. This is merely an example, and you should feel free to adjust it according to your specific requirements.
# Use an official Ubuntu as a parent image
FROM ubuntu:latest
# Avoid prompts from apt
ENV DEBIAN_FRONTEND=noninteractive
# Install ZeroTier
RUN apt-get update && \
apt-get install -y curl gnupg && \
curl -s https://install.zerotier.com | bash
# Ensure the ZeroTier service starts automatically
CMD service zerotier-one start && tail -f /dev/null
After entering the code in your Dockerfile, save your changes in the Nano text editor by pressing the Ctrl + O keys, followed by Ctrl + X to exit.
With Nano closed and your Dockerfile saved, you’ve successfully created a ZeroTier Dockerfile. If you need to modify this Dockerfile later, just reopen it in Nano and add your additional commands.
How to build your ZeroTier Docker Image
You’ve written your ZeroTier Dockerfile, and it is set up to perform the following tasks: update Ubuntu with the latest packages, install the “curl” package, and install ZeroTier. Now, it’s time to build the image based on the Dockerfile.
Building is a fairly quick process, especially on a modern Linux system. To begin, launch a terminal. Once your terminal window is open, use the command cd ~/zerotier-container
to enter the folder containing the Dockerfile.
cd ~/zerotier-container
From here, you can build the Docker image using the docker build
command. This build process will be visible in real time, allowing you to monitor its progress directly in the terminal. If the build fails, revert any modifications you’ve made, and re-run the command.
docker build -t zerotier-container .
How to run your ZeroTier Docker Container
Once the image is built, you’re ready to deploy your ZeroTier Docker container. Deployment can be done in two ways: using the docker run
command or by crafting a Docker Compose file. This guide will focus on Docker Compose, as it offers a more manageable approach than using lengthy commands.
First, ensure you are in the ~/zerotier-container/
directory. Then, create a new file named docker-compose.yml
by using the touch
command:
touch docker-compose.yml
After creating the blank Docker Compose file, open it with Nano:
nano docker-compose.yml
Insert the following configuration:
version: '3.8'
services:
my-zerotier-container:
image: zerotier-container
container_name: my-zerotier-container
devices:
- "/dev/net/tun:/dev/net/tun"
cap_add:
- NET_ADMIN
- SYS_ADMIN
restart: unless-stopped
Save the file by pressing Ctrl + O, then Enter, and exit Nano with Ctrl + X. Deploy the container using Docker Compose with the following command:
sudo docker compose up -d
The docker compose up -d
the command will create and start the container named “my-zero tier-container” in daemon mode, running it in the background.
To stop and remove the container and any networks created, use the following command while in the ~/zerotier-container/
directory:
sudo docker compose down
How to connect your ZeroTier Docker Container to the network
Now that your container is up and running with ZeroTier installed, the next step is to join it to your network. Begin by accessing the command console of your container with the following command:
docker exec -it my-zerotier-container bash
Executing the command above will log you into the terminal interface of the container. Next, open ZeroTier in a web browser to locate your Network ID. Once you have your Network ID, use it to join the container to your network by running:
zerotier-cli join YOUR_NETWORK_ID
Replace YOUR_NETWORK_ID
with the actual ID of your ZeroTier network.
After successfully joining the network, verify the connection and network details by executing:
zerotier-cli listnetworks
This command will display a list of networks the container is connected to, allowing you to confirm that the join operation was successful.