Walkthrough of Connecting Nginx, PHP and MySQL with Weave
Since other people have published quite sophisticated examples, I thought it was time for a really simple one. The scenario is: you want to run a web server in a container, out in the cloud, talking to a database in another container,...
How to Configure your Repos for Multi-Tenancy and GitOps: Zscaler’s Use Case
Automating Weave deployment on Docker hosts with Weave Discovery
Using Weave and Scope with Convox
The scenario is: you want to run a web server in a container, out in the cloud, talking to a database in another container, back in your home datacentre. Maybe you’re very precious about your data and don’t want to host it out on the cloud. You’re going to use weave to network up the two containers.
Here’s the architecture diagram – hexagons are application containers, and the red circles are weave routers.
I’m going to walk through setting this up from scratch.
Step 1: Create your VM:
I’m using Amazon EC2, but the principle is the same on any other cloud provider. We want a basic Linux install, nothing fancy.
For the purpose of this walkthrough, I picked the Ubuntu Server AMI, though weave will work just fine on Debian, CoreOS, etc.
Step 2: Open the weave port:
We need port 6783 open for tcp and udp. We only need to open this in one direction: in my case I’m opening it coming into EC2.
Step 3: Install Docker.
I followed these instructions for Ubuntu.
(this goes on for a while…)
Then we do the same on the other host (no screen grab – it looks just the same as the one above).
Step 4: Install and start weave.
Following the instructions. This is much faster than installing Docker because it’s only a 10K script and a 10MB container.
Do the same on the other host too – just start weave with a different IP address:
Step 5: Install the web server software:
I’m going to use a container from the Docker Registry that has Nginx and PHP built in:
$ sudo weave run 10.0.1.1/24 -p 8080:80 -v /var/realdata:/data maxexcloo/nginx-php
Breaking this command-line down, we:
- use ‘weave run’ instead of ‘docker run’, so weave can do its stuff.
- give this container the IP address 10.0.1.1 on the weave network.
- this IP address is in subnet 10.0.1 – i.e. the first 24 bits of the address.
- publish Nginx port 80 as port 8080 on this machine.
- map any files in /var/realdata from the host into the container.
<?php $mysqli = new mysqli("10.0.1.2", "root", "***", "testdb"); $result = $mysqli->query("SELECT version() as _version;"); $row = $result->fetch_assoc(); printf("Connected to MySQL on %s running version %s", $mysqli->host_info, $row['_version']); ?>
This is just to test out that we can connect to a mysql database on 10.0.1.2, which will be another host on the weave network. So let’s set that up…
Step 6: Install the database
On the host in our own datacentre. This is using another container from the registry:
$ sudo weave run 10.0.1.2/24 -e MYSQL_ROOT_PASSWORD="****" -e MYSQL_DATABASE=testdb mysql
Step 7: Test!
That’s our new web server, up on an Amazon EC2 host, communicating to a MySQL database back in our own datacentre, using the Weave network.