Skip to content

Instantly share code, notes, and snippets.

@tygern
Last active May 6, 2020 14:10
Show Gist options
  • Save tygern/514b5d5ee57f976fa890e01fcb9caf06 to your computer and use it in GitHub Desktop.
Save tygern/514b5d5ee57f976fa890e01fcb9caf06 to your computer and use it in GitHub Desktop.
Docker machine on Digital Ocean
# Install docker-machine
curl -L https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-$(uname -s)-$(uname -m) > ~/bin/docker-machine
chmod +x ~/bin/docker-machine
# Install doctl
curl -sL https://github.com/digitalocean/doctl/releases/download/v1.41.0/doctl-1.41.0-darwin-amd64.tar.gz | tar -xzv
chmod +x doctl
mv doctl ~/bin
# Create docker machine
docker-machine create --driver digitalocean --digitalocean-access-token $DO_PERSONAL_TOKEN --digitalocean-size s-4vcpu-8gb docker-sandbox
# Create firewall
export DROPLET_ID=$(docker-machine inspect docker-sandbox | jq ".Driver.DropletID")
doctl -t $DO_PERSONAL_TOKEN compute firewall create --droplet-ids=$DROPLET_ID \
--inbound-rules "protocol:tcp,ports:22,address:0.0.0.0/0,address:::/0 protocol:tcp,ports:2376,address:0.0.0.0/0,address:::/0" \
--outbound-rules "protocol:icmp,address:0.0.0.0/0,address:::/0 protocol:tcp,ports:all,address:0.0.0.0/0,address:::/0 protocol:udp,ports:all,address:0.0.0.0/0,address:::/0" \
--name=sandbox-firewall
# Start containers
eval $(docker-machine env docker-sandbox)
docker-machine start docker-sandbox
docker-compose up redis sql-server-db rabbitmq
# Start ssh tunnel (example)
docker-machine ssh docker-sandbox -N -L 1433:localhost:1433 -L 6379:localhost:6379 -L 5672:localhost:5672
# Stop droplet and tear down firewall
docker-machine stop docker-sandbox
export FIREWALL_ID=$(doctl -t $DO_PERSONAL_TOKEN compute firewall list-by-droplet $DROPLET_ID --format ID --no-header)
doctl -t $DO_PERSONAL_TOKEN compute firewall delete $FIREWALL_ID -f
# Remove droplet
docker-machine rm -f docker-sandbox
@tygern
Copy link
Author

tygern commented Apr 19, 2020

@tygern
Copy link
Author

tygern commented Apr 20, 2020

Make sure to create a firewall https://www.digitalocean.com/docs/networking/firewalls/how-to/create/ for the droplet. Open ports 22 (for SSH) and 2376 (for Docker).

For later https://www.digitalocean.com/docs/apis-clis/doctl/compute/firewall/create/

@tygern
Copy link
Author

tygern commented Apr 20, 2020

SSH tunnel:

docker-machine ssh docker-sandbox -N -L 1433:localhost:1433 -L 6379:localhost:6379 -L 5672:localhost:5672 -L 15672:localhost:15672

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