Skip to content

Instantly share code, notes, and snippets.

@neckhair
Last active December 10, 2021 02:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neckhair/b9fdacec5d2e8b301ade47e37aac1755 to your computer and use it in GitHub Desktop.
Save neckhair/b9fdacec5d2e8b301ade47e37aac1755 to your computer and use it in GitHub Desktop.
Setup docker-gen on OS X
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>ch.nine.docker-gen</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/docker-gen</string>
<string>-only-published</string>
<string>-notify</string>
<string>/usr/local/bin/nginx -s reload</string>
<string>-watch</string>
<string>/usr/local/etc/docker-gen.tmpl</string>
<string>/usr/local/etc/nginx/servers/docker-gen.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/docker-gen.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/docker-gen.log</string>
</dict>
</plist>

How to setup docker-gen on the Mac:

This guide prepares your mac so you can access your docker containers by their docker-compose project name. So if you have a container with the label com.docker.compose.project=myapp running you will be able to access it on the url myapp.docker.

DNS

This guide assumes that you are already have a working dnsmasq setup on your mac. The follwing command resolves to 127.0.0.1:

dig @localhost myapp.dev

Create the file in /etc/resolver/docker with the following content:

nameserver 127.0.0.1

This lets you resolve the tld .docker to 127.0.0.1.

Now add the folloing line to dnsmasq.conf:

address=/docker/127.0.0.1

Automagic Nginx Vhosts

Nginx and Docker must be already installed. If not:

brew cask install docker
brew install nginx

Then install docker-gen:

brew install docker-gen

Docker-gen needs a template to automatically create vhost configurations. Copy this template to your machine:

curl https://gist.githubusercontent.com/neckhair/b9fdacec5d2e8b301ade47e37aac1755/raw/b6ca8c9e7b9e26aec104711513b205b321413a68/docker-gen.tmpl -o /usr/local/etc/docker-gen.tmpl

To run the service automatically in the background copy the service plist file to the following location:

curl https://gist.githubusercontent.com/neckhair/b9fdacec5d2e8b301ade47e37aac1755/raw/b0ffacf149a6ec1c7014ece213a10f3bd3637620/ch.nine.docker-gen.plist -o /Library/LaunchDaemons/ch.nine.docker-gen.plist
sudo launchctl load -w /Library/LaunchDaemons/ch.nine.docker-gen.plist

Ansible

I created an Ansible role to automate the process. You could use this if you want: https://github.com/neckhair/ansible-role-docker-gen/blob/master/tasks/main.yml.

{{ range $project, $containers := groupByLabel $ "com.docker.compose.project" }}
{{ $host := replace "_host_.docker" "_host_" $project 1 }}
upstream {{ $host }} {
{{ range $index, $value := $containers }}
{{ with $address := index $value.Addresses 0 }}
server localhost:{{ $address.HostPort }};
{{ end }}
{{ end }}
}
server {
listen 80;
root /Users/phil/src/{{ $project }}/public;
server_name {{ $host }} *.{{ $host }} {{ $host }}.*;
client_max_body_size 50M;
error_page 500 502 503 504 /50x.html;
gzip_types text/plain text/css application/json application/x-javascript
text/xml application/xml application/xml+rss text/javascript;
location = /50x.html {
root html;
}
try_files $uri/index.html $uri @{{ $host }};
location @{{ $host }} {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://{{ $host }};
}
}
{{ end }}
@calmyournerves
Copy link

Don't forget to add address=/docker/127.0.0.1 to dnsmasq.conf if you're not using .dev!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment