A minimal example demonstrating how to create a dockerized Go app that can easily be deployed as a DigitalOcean droplet.
/src/...
/docker-compose.yml
/Dockerfile
The app source is placed under /src
- The dockerfile is based off of the official
golangbased image, which contains the desired go environment. - Different variants are example. For example, for a specific version of go, use
golang:1.6instead ofgolang:latest. - For more information see the golang docker image.
The docker-compose configuration sets up the app service and a simple nginx-proxy service for routing.
The nginx-proxy service automatically links up any other services that have a VIRTUAL_HOST environment variable, and forwards any requests that match the routes.
Both services use restart: always to automatically restart in case the machine restarts.
Log history is capped at 100m to prevent running out of disk space 😅 (I learned that the hard way once).
To run the services locally
docker-compose build && docker-compose up
To run the services locally (detached)
docker-compose build && docker-compose up -d
To see the status:
docker-compose ps
To test the app:
curl -H 'Host: foobar.example.com' localhost
Note that we have to supply an explicit host header since we're testing locally. We could also add another VIRTUAL_HOST to the service for localhost to receive requests from http://localhost/.
Deploying to DigitalOcean is pretty straightforward.
Create>DropletOne-click appsDocker 17.05.0-ce on 16.04(or newer)- Choose desired
size - Choose desired
region - Select
Monitoring - Choose desired
hostname
When creating the droplet, enable User Data and enter your script:
#!/bin/bash
git clone https://<token>@<repo> ~/app
cd ~/app && docker-compose build && docker-compose up -d
Note: Logs for User Data cloud init scripts can be found here: /var/log/cloud-init-output.log.
- SSH into the droplet
ssh root@<ip>
- Clone the source repo:
git clone <repo>
- Deploy
cd app && docker-compose build && docker-compose up -d
-
Optionally, create monitoring alerts in DigitalOcean
-
To re-deploy, repeat steps 2-4