Run Container Workloads via Docker Compose on Hetzner Cloud using Alpine Linux

If you want to use Docker Compose to host your containers in the cloud, then this post is the right one for you. If you want to use Podman Compose, please check out the previous post.

First we need to make sure what Alpine Linux distro we want to use. Hetzner already has a mountable Alpine Virtual image that you can mount after you created a server with any other OS. This a good choice for the cloud, especially optimized for virtual systems. To get started you can use this already provided image or take an extra step and download an image for Alpine Linux here for the type of architecture of the server you want to use. Make sure you are able to mount your image, e.g. by sending the support the ISO image for mounting in the Hetzner dashboard or by uploading it with SCP and mounting it with imageinstall. Next we will create an ssh key called hetzner_ed25519. You can follow this tutorial on GitHub on how to create one.

On MacOS you can setup your ssh config like this in the file at ~/.ssh/config

Host hetzner
  HostName <server-ip>
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/hetzner_ed25519
  User root
  Port 22

Once your image is uploaded to the Hetzner Dashboard, create any server e.g. alpine-2vcpu-4gb-arm64-ampere-nbg1-dc3 with the required resources and your preferred architecture. Log in with the root user by opening the console from the dashboard. Alternatively when you use imageinstall you already created the server. There isn't yet a password setup. Next execute the command:

setup-alpine
Choose your keyboard layout, a good option is us-mac and follow the setup process. Choose your timezone etc., add community repositiories and fill in your ssh-key, e.g. execute
cat ~/.ssh/hetzner_ed25519.pub | pbcopy
on your local machine and paste the result. Select sda as your partition and overwrite everything. If you forgot to enable community repositories, you can still add it by editing /etc/apk/repositories with vi:
vi /etc/apk/repositories # then enter 'i' key for insert
# uncomment the line for community repositories
# click ESC and write :wq (for write and quit)

Next we will install Docker and Docker Compose. We do this by running

apk add Docker
apk add docker-compose
Then we make sure everything is started.
service docker start # or service docker restart
service docker status

Make sure to install Docker on your local machine. Then add a new context for the remote. And use it for all docker commands. Make sure to replace <server-ip> with the IP of your server.

docker context create remote --docker "host=ssh://root@<server-ip>"
docker context use remote # all docker commands will use the remote
Now all your docker commands get executed on the remote. You can now deploy via docker run or via a docker-compose.yml.

To see how to do this with a Caddy reverse proxy that acquires TLS certificates for https refer to the previous post. The docker-compose.yml doesn't change. You can use it with:

docker compose up -d


← Back to Homepage