build your own saas using docker

40
Build Your Own SaaS Build Your Own SaaS with Docker A proof of concept with a simple Memcached SaaS http://www.slideshare.net/julienbarbier42/building-a-saas- using-docker

Upload: julienbarbier42

Post on 08-Sep-2014

47.685 views

Category:

Technology


4 download

DESCRIPTION

Build Your Own SaaS using Docker. A proof of concept with a simple Memcached SaaS. See the Memcached as a service application in action at http://www.memcachedasaservice.com Find the source code on GitHub: https://github.com/jbarbier/SaaS_Memcached

TRANSCRIPT

Page 1: Build Your Own SaaS using Docker

Build Your Own SaaS

Build Your Own SaaSwith Docker

A proof of concept with a simple Memcached SaaS

04/14/2013 – by Julien Barbier

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 2: Build Your Own SaaS using Docker

Build Your Own SaaS

Table of content

INTRODUCTION 3

THIS IS CS50 3SOURCES 3PROOF OF CONCEPT 3MEMCACHED 3THANK YOU! 4

REQUIREMENTS 5

UBUNTU MACHINE 5DOCKER 5MEMCACHED 5

CREATING A DOCKER IMAGE WITH MEMCACHED 6

START DOCKER 6INSTALLING MEMCACHED ON DOCKER 6COMMITTING OUR MEMCACHED CONTAINER 8CHECKING OUR MEMCACHED CONTAINER IMAGE 8PLAYING WITH OUR MEMCACHED IMAGE 9SPAWNING A NEW CONTAINER BASED ON OUR MEMCACHED IMAGE 9RETRIEVING THE PUBLIC PORT OF OUR MEMCACHED CONTAINER 10TESTING OUR MEMCACHED 12

CREATING A MEMCACHED SAAS 15

BUILDING THE WEBSITE 15SPAWNING A MEMCACHED CONTAINER ON REGISTRATION 16DISPLAYING THE PUBLIC MEMCACHED IP AND PORT TO THE USER 18PORT 18IP ADDRESS 19USING THE MEMCACHED SERVER 21RUBY 21PHP 21PYTHON 22GO 22ADDING SECURITY 23USING IPTABLES TO FILTER BY IP 23CALLING IPTABLES FROM A WEB SERVER 24TESTING THE SECURITY FILTER 25

WHERE TO GO FROM HERE 29

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 3: Build Your Own SaaS using Docker

Build Your Own SaaS

Introduction

This is cs50

This document and source code are part of my final project for cs50x that I started on edx.org few months ago. For this final project I had several goals:

- learn a new language: I chose to learn Ruby and Rails- use a new piece of technology. I chose to use Docker- build a cool product. Building a SaaS a new way and with a new piece of

technology sounds fun!- make it open source, and learn how to use Git and GitHub

Along the way I wrote several documents that are available on SlideShare. Some of them have been used by Docker in their documentation.

Sources

You can find, clone, fork, or download the source code of the project on GitHub:https://github.com/jbarbier/SaaS_Memcached

Proof of concept

By downloading the source code and reading this document you will be able to run a minimalist SaaS. Your users will be able to get their own Memcached server. Of course this is only a proof of concept, but it runs quite well.

Memcached

Memcached is a free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

I chose Memcached because it is a widely used service. It is also easy to install and use, so that the tests are not too complicated to perform.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 4: Build Your Own SaaS using Docker

Build Your Own SaaS

Thank you!

I had only a few weeks to learn Ruby, Rails, Git, Github, iptables, sudoers, … and build this proof of concept. I would like to thank all the people who gave their time to help me and answer all my questions:

- Guillaume Charmes , alias Cortex, alias MPM, my Docker teacher- Guillaume Luccizano , Steeve Morin and Sylvain Kalache, my Rails and

Ruby teachers (sorry I was not able to use TDD until the end, I didn’t have enough time!)

- Daniel Mizyrycki, my Git and GitHub teacher- Jerome Petazzoni , my iptables teacher- and Thomas Meson for giving me an Ubuntu server to play with

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 5: Build Your Own SaaS using Docker

Build Your Own SaaS

Requirements

Ubuntu machine

In order to follow this tutorial you will need a server with the last version of Ubuntu (or any other OS, but using Vagrant and VirtualBox to run an Ubuntu image). We need Ubuntu because our minimalist SaaS will use Docker, which runs on Ubuntu servers only.

The community behind Docker is growing fast and is very active. And at the time I write this document, it is now possible to use Docker on different operating systems. For instance, Flavio Castelli has written a blog-post on how to use Docker on openSUSE. And I’ve seen people using it on CentOS during a Docker demo days.

Docker

Docker is a Linux container runtime. It has been released few weeks ago as an open-source project by dotCloud. Docker complements LXC with a high-level API which operates at the process level. It runs unix processes with strong guarantees of isolation and repeatability across servers.

Please visit Docker’s website for a tutorial on how to get Docker running on your Ubuntu machine or using Vagrant + VirtualBox on any other Operating system.

We will use only few Docker commands through this tutorial. To learn more about the Docker command line interface, you can take a look at their CLI documentation page.

Memcached

You don’t need to have Memcached installed on your server. Memcached will run inside our Docker containers.

I will explain in this document how you can build your Memcached container. If you are not interested in learning how to build your own image, you can skip the first chapter, jump directly to the next chapter “Creating a Memcached SaaS” and use the image called jbarbier/memcached. To get this image, use the docker pull command:

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 6: Build Your Own SaaS using Docker

Build Your Own SaaS

Creating a Docker image with MemcachedThe first step to build the minimalist Memcached SaaS is to have an image of a container with Memcached installed.

Start DockerLet’s check if Docker is already running.

If you do not see a line “docker –d”, then start Docker as a daemon:

Installing Memcached on DockerWe will install Memcached on a Docker container with the docker run command.

This command will return you the id of the new created container running your command will need to keep this id in order to use it later. In our example, the id is f1ab59fbc9d5.

We can check that the installation is complete by using the command docker logs

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

sudo docker –d &

ps aux | grep docker

docker run -d base apt-get -y install memcached

docker logs f1ab59fbc9d5 | less

docker pull jbarbier/memcached

Page 7: Build Your Own SaaS using Docker

Build Your Own SaaS

with the container id given by the previous command.

Remember to replace f1ab59fbc9d5 by your container id.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 8: Build Your Own SaaS using Docker

Build Your Own SaaS

Committing our Memcached containerWe can commit our new container with the docker commit command, using our container id. With the following command line we will name it jbarbier/memcached, but you should use your own name.

Remember to replace f1ab59fbc9d5 by your container id and jbarbier/memcached by your own name.

This command gives you back a new id, which is the image id of your committed container. In our example it is c3b6fcb48266.

Checking our Memcached container imageLet’s check that Memcached is installed on this image. To do so we can spawn a new container from this image and run bash inside.

Remember to replace jbarbier/memcached with the name of your image.

We are now inside a new container spawned from our image. Let’s see if Memcached is installed. Run

OK!

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

docker commit f1ab59fbc9d5 jbarbier/memcached

docker run -i -t jbarbier/memcached /bin/bash

memcached

Page 9: Build Your Own SaaS using Docker

Build Your Own SaaS

Note that you could have used the id of your image instead of the name of your repository.

Remember to replace c3b6fcb48266 by your image id.

Playing with our Memcached imageNow that we have an image with Memcached installed, let’s use it :)

Spawning a new container based on our Memcached image

Remember to replace jbarbier/memcached with the name of your image.

We need to launch Memcached with the –u option because you can not run it as root. With –u daemon, our Memcached will run as a daemon.

In the next chapter we will build a SaaS with this image. So we will need any user to be able to access their Memcached. In order to be able to use the Memcached server running in the container from outside our server, we can use the –p option. This option tells Docker to map the internal port of the container used by Memcached (11211), with a public port of the host.

As usual, Docker gives you back the id of the container you launched. In our case it is c360f228e22f.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

docker run -i -t c3b6fcb48266 /bin/bash

docker run -d -p 11211 jbarbier/memcached memcached -u daemon

Page 10: Build Your Own SaaS using Docker

Build Your Own SaaS

Retrieving the public port of our Memcached containerIn order to use Memcached from outside the localhost we need to know the host public port mapped by Docker. In order to know that we can use the docker inspect command.

Remember to replace c360f228e22f by your container id before running this command.

This will give you a JSON output with plenty of configuration details (see next page).

In the NetworkSettings/PortMapping you will find the public port you can use Memcached with from outside the server. In our case the public port is 49153.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

docker inspect c360f228e22f

Page 11: Build Your Own SaaS using Docker

Build Your Own SaaS

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 12: Build Your Own SaaS using Docker

Build Your Own SaaS

Testing our Memcached Let’s test and use our Memcached service, from an outside machine. In the following examples I will use 142.242.242.42 as the IP of the server where the container is running, and 49153 as the public port.

Before running any of these examples be sure to replace the IP with your server IP, and the port number with the one docker inspect gave you.

Ruby

Python

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Guillotine:test_memcached jbarbier$ cat test.rb# gem install dalli

require 'dalli'

ip = '142.242.242.42'port = 49153

dc = Dalli::Client.new("#{ip}:#{port}")dc.set('abc', "Always Be Closing") value = dc.get('abc')

puts value

Guillotine:test_memcached jbarbier$ cat test.py# pip install python-memcached

import memcache

ip = '142.242.242.42'port = 49153

mc = memcache.Client(["{0}:{1}".format(ip, port)], debug=0)

mc.set("best_dev", "Guillaume C.")value = mc.get("best_dev")

print value

Page 13: Build Your Own SaaS using Docker

Build Your Own SaaS

PHP

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Guillotine:test_memcached jbarbier$ cat test.php<?php

$ip = '142.242.242.42'; $port = 49153;

$memcache_obj = new Memcache; $memcache_obj->connect($ip, $port);

$memcache_obj->set('rule_1', 'You DO NOT talk about FIGHT CLUB'); $v = $memcache_obj->get('rule_1');

echo "$v\n"; ?>

Page 14: Build Your Own SaaS using Docker

Build Your Own SaaS

Go

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Guillotine:test_memcached jbarbier$ cat test.gopackage main

import ( "fmt" "github.com/kklis/gomemcache")

func main() {

ip := "142.242.242.42"port := 49153

memc, err := gomemcache.Connect(ip, port)if err != nil { panic(err)

}

err = memc.Set("foo", []byte("bar"), 0, 0)if err != nil { panic(err)}

val, _, _ := memc.Get("foo")fmt.Printf("%s\n", val)

}

Page 15: Build Your Own SaaS using Docker

Build Your Own SaaS

Creating a Memcached SaaS

Now that we have an image with Memcached installed, and that we know almost all the required commands, the plan is to use that to create our SaaS. Each user will have its own Memcached running inside his own container.

1. A user registers through our website2. We spawn a Memcached container using our image3. We give the user an IP and a port of his Memcached server4. We add a layer of security

This last step is required because otherwise everybody could use the user’s Memcached since there is no built-in security for Memcached servers.

Building the website

As I previously mentioned I chose to learn Ruby and Rails, so the website is using these technologies, but you could use any language.Since this article is not about building websites we won’t go into details on how to build a website. Feel free to use my code or to build your own website with your favorite language and database.

The example website does only handle sign-up, sign-in and a profile page which displays information about the user’s Memcached server. These are the only required pages in order to build this proof of concept. You can find the source code of the website on GitHub.

Sign-up page Sign-in page Profile page

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 16: Build Your Own SaaS using Docker

Build Your Own SaaS

Spawning a Memcached container on registration

When the user has signed-up, we need to run the commands to launch a new container with our Memcached image. The function create_memcached_instance is doing all the job. You can find this function in the file users_controller.rb

  def create_memcached_instance

    docker_path = '/home/julien/docker-master/'

    container_id = `#{docker_path}docker run -d -p 11211 jbarbier/memcached

memcached -u daemon`

    cmd = "#{docker_path}docker inspect #{container_id}"

    json_infos = `#{cmd}`

    i = JSON.parse(json_infos)

    @user.memcached = i["NetworkSettings"]["PortMapping"]["11211"]

    @user.container_id = container_id

    @user.docker_ip = i["NetworkSettings"]["IpAddress"]

  end

If you have cloned the repository and are running the website from that, remember to edit users_controller.rb and set the docker_path variable to your Docker’s path. Alternatively you can add Docker to your PATH.

Let’s go through all the lines:

1. docker_path = '/home/julien/docker-master/'

docker_path should contain the path of your Docker’s executable

2. container_id = `#{docker_path}docker run -d -p 11211 jbarbier/memcached memcached -u daemon`

As discussed in the previous chapter, docker run -d runs a command in a new container. We pass the option -d so that it leaves the container run in the background.

The option -p 11211 maps the internal port of the container used by Memcached with a public port of our server.

jbarbier/memcached is the name of our image with Memcached installed (see previous chapter to see how we built this image). If you have created your own image, you should replace jbarbier/memcached by the name of your image.

memcached -u daemon is the command we run inside the new container. We use the option -u daemon to run Memcached with user daemon. The command docker run returns the id of the new container. We will need it so we save it to the variable container_id.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 17: Build Your Own SaaS using Docker

Build Your Own SaaS

3. cmd = "#{docker_path}docker inspect #{container_id}"

As discussed earlier, we need to get the public port to give it to the user. So we inspect our newly created container with the docker inspect command. We pass it the container_id variable to tell Docker which container to inspect. This command returns us lots of information about the container formatted in JSON. We save it and

4. i = JSON.parse(json_infos)

we parse it to access the information. We then store all the required information into the user variable.

5. @user.memcached = i["NetworkSettings"]["PortMapping"]["11211"]

i["NetworkSettings"]["PortMapping"]["11211"] contains the public port mapped with the port 11211, used by Memcached.

6. @user.container_id = container_id

saves the container id and

7. @user.docker_ip = i["NetworkSettings"]["IpAddress"]

saves the internal IP address of our container. We will need this IP when we will be adding a basic layer of security on top of the user’s Memcached service.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 18: Build Your Own SaaS using Docker

Build Your Own SaaS

Displaying the public Memcached IP and port to the user

We now have to give the user the IP and port with which he can access his Memcached. The code to show these information is in the file show.html.erb.

<% provide(:title, @user.email) %>

<h1>

  <%= gravatar_for @user %>

  <%= @user.email %>

</h1>

<div class="alert alert-info">

  Congratulations <%= @user.email %>. Your Memcached server is ready to use.

</div>

<h1>Your Memcached Server is ready!</h1>

<div class="block-info">

  <h3>IP: <%= my_public_ip %></h3>

  <h3>PORT: <%= @user.memcached %></h3>

</div>

<div class="alert alert-info">

  Use it with your favorite language.

</div>

<%= render 'code' %>

<%= render 'ip' %>

Port

We already have the port saved. We just need to display it

PORT: <%= @user.memcached %>

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 19: Build Your Own SaaS using Docker

Build Your Own SaaS

IP address

We just need to know our public IP address (the IP of our server). There are plenty of ways to know it. One of which is to ask an online service. I chose to use ifconfig.me but you could use any service of this type. The code to discover its own public IP address is in the file users_helper.rb.

  def my_public_ip

    @@ip ||=

Net::HTTP.get_response(URI.parse("http://ifconfig.me/ip")).body.chomp

  end

http://ifconfig.me/ip simply returns the IP. So we just have to store it and then show it to the user.

IP: <%= my_public_ip %>

Alternatively, if you just want to test, you can hard code your IP address in my_public_ip or even in show.html.erb.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 20: Build Your Own SaaS using Docker

Build Your Own SaaS

At this point you should have all you need to build your own Memcached SaaS.

If you cloned my GitHub repository, after the registration you should see the Profile’s page showing the IP and port whith which you can access your Memcached.

You can also check that a new container is running every time a new user registers.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

julien@cs50:~$ ps aux | grep dockerroot 23863 0.0 0.0 27540 1220 ? S Apr11 0:00 lxc-start -n 48610f83f354bd5a7675bf41daedbb87958e6acf618f8c24487526373ddde8b8 -f /var/lib/docker/containers/48610f83f354bd5a7675bf41daedbb87958e6acf618f8c24487526373ddde8b8/config.lxc -- /sbin/init -g 172.16.42.1 -- memcached -u daemon[…]

Page 21: Build Your Own SaaS using Docker

Build Your Own SaaS

Using the Memcached server

Once you register you can test your Memcached with the IP and port provided by the profile’s page, with your favorite language.

Ruby

# gem install dalli

require 'dalli'

ip = '137.116.225.4'

port = 49159

dc = Dalli::Client.new("#{ip}:#{port}")

dc.set('1762c2acf87','[email protected]')

value = dc.get('1762c2acf87')

puts "Welcome #{value}! Your Memcached server is ready to use :)"

PHP

$ip = '137.116.225.4'

$port = 49159;

$memcache_obj = new Memcache;

$memcache_obj->connect($ip, $port);

$memcache_obj->set('1762c2acf87', '[email protected]');

$v = $memcache_obj->get("1762c2acf87");

echo "Welcome $v! Your Memcached server is ready to use :)\n";

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 22: Build Your Own SaaS using Docker

Build Your Own SaaS

Python

# pip install python-memcached

import memcache

ip = '137.116.225.4

port = 49159

mc = memcache.Client(["{0}:{1}".format(ip, port)], debug=0)

mc.set("1762c2acf87", "[email protected]")

value = mc.get("1762c2acf87")

print "Welcome {0}! Your Memcached server is ready to use :)".format(value)

Go

package main

import (

"fmt"

"github.com/kklis/gomemcache"

)

func main() {

ip := "137.116.225.4"

port := 49159

memc, err := gomemcache.Connect(ip, port)

if err != nil {

panic(err)

}

err = memc.Set("1762c2acf87", []byte("[email protected]"), 0, 0)

if err != nil {

panic(err)

}

val, _, _ := memc.Get("1762c2acf87")

fmt.Printf("Welcome %s! Your Memcached server is ready to use :)\n", val)

}

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 23: Build Your Own SaaS using Docker

Build Your Own SaaS

Adding security

We have now a minimalist Memcached SaaS. But our users are not happy because anybody can access their Memcached server. So we need to give the option to our users to restrict somehow the access to their Memcached. There are plenty of ways to do so. In this tutorial we will give the user the option to restrict the access to the Memcached to one IP. And to do so we will use iptables.

Using iptables to filter by IP

Iptables is used to set up, maintain, and inspect the tables of IPv4 packet filter rules in the Linux kernel. The command lines to restrict the access to the service to one IP address is in the file add_ip

#!/bin/sh

/sbin/iptables -I FORWARD -d $1 -s $2 -j ACCEPT

/sbin/iptables -A FORWARD -d $1 -j DROP

add_ip is an executable shell script. That is why we have

1. #!/bin/sh

at the beginning of the file. The script will take two arguments in parameters.$1 is the internal IP of the container that we previously stored in @user.docker_ip upon user account creation.$2 is the IP provided by the user which will become the only authorized IP to access the user’s container, and the user’s Memcached server.

2. /sbin/iptables -I FORWARD -d $1 -s $2 -j ACCEPT

Tells iptables to add a rule to accept IP $2 to access internal IP $1.

3. /sbin/iptables -A FORWARD -d $1 -j DROP

Tells iptables to add a rule to deny all access to IP $1 from any IP.Since the first rule is “checked” first, only IP $2 will be able to access IP $1.

The file remove_ip does exactly the opposite using the –D option to delete the previous rules.

#!/bin/sh

/sbin/iptables -D FORWARD -d $1 -s $2 -j ACCEPT

/sbin/iptables -D FORWARD -d $1 -j DROP

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 24: Build Your Own SaaS using Docker

Build Your Own SaaS

Calling iptables from a web server

We call the previous scripts from the two following functions in users_controller.rb

def iptables_add_ip(i)

    cwd = Dir.pwd

    `sudo #{cwd}/iptables/add_ip #{@user.docker_ip} #{i}`

  end

  def iptables_remove_ip(i)

    cwd = Dir.pwd

    `sudo #{cwd}/iptables/remove_ip #{@user.docker_ip} #{i}`

  end

But in order to use iptables we need to have root privileges. And our web server is probably not running as root (and it should not). So we will need to use the sudo command and allow our webserver to run the two scripts.

To do so we will use /etc/sudoers. The /etc/sudoers   file controls who can run what commands as what users on what machines and can also control special things such as whether you need a password for particular commands.

A simple way to tackle our problem is to create a new file in /etc/sudoers.d. We can call it saas_memcached for instance.

There are two lines in the file. The first line creates an alias of all the executable files and the second allow the user www-data to run these executable files with root privilege without requiring typing any password.

You should replace /home/julien/final_proj/SaaS_Memcached by the root of your website. If your server does not run as www-data, simply replace www-data by the right user in the file.

Now the user should be able to specify the allowed IP from which he can access Memcached. Every other IP address will be blocked.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

julien@cs50:~$ cat /etc/sudoers.d/saas_memcachedCmnd_Alias ADD_REM_IPS_CMDS = /home/julien/final_proj/SaaS_Memcached/iptables/add_ip, /home/julien/final_proj/SaaS_Memcached/iptables/remove_ip

www-data ALL=(ALL) NOPASSWD: ADD_REM_IPS_CMDS

Page 25: Build Your Own SaaS using Docker

Build Your Own SaaS

Testing the security filter

In order to test our security filter, let’s create an account to launch a new Memcached server.

In the following examples we will run the ruby script from IP 69.42.42.42 and the PHP script from IP 69.33.33.33

If we run we run these scripts from our two IPs it will work.

Let’s scroll to the bottom of the profile page and add the IP 69.42.42.42 so our Memcached access can be restricted to this IP.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Guillotine:test_memcached jbarbier$ ruby ip_ok.rbWelcome [email protected]! Your Memcached server is ready to use :)

julien@revolution:/tmp$ php ip_nok.php Welcome [email protected]! Your Memcached server is ready to use :)

Page 26: Build Your Own SaaS using Docker

Build Your Own SaaS

After saving the IP, we should check that the access is really restricted to the IP 69.42.42.42.

Running the Ruby script from 69.42.42.42

Running the PHP script from 69.33.33.33

As we can see only the script ran from IP 69.42.42.42 is able to connect to Memcached.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Guillotine:test_memcached jbarbier$ ruby ip_ok.rbWelcome [email protected]! Your Memcached server is ready to use :)

julien@revolution:/tmp$ php ip_nok.php PHP Notice: Memcache::connect(): Server 137.116.225.4 (tcp 49164, udp 0) failed with: Connection timed out (110) in /tmp/ip_nok.php on line 6PHP Warning: Memcache::connect(): Can't connect to 137.116.225.4:49164, Connection timed out (110) in /tmp/ip_nok.php on line 6[…]

Page 27: Build Your Own SaaS using Docker

Build Your Own SaaS

Let’s check the iptables by running iptables –L

The two lines

ACCEPT all -- c-69-42-42-42.hsd1.ca.comcast.net 172.16.42.14DROP all -- anywhere 172.16.42.14

Have been added by our add_ip script to restrict the access of [email protected]’s container running Memcached.

We can verify that the the IP 172.16.42.14 is the right internal IP of the container by using the docker inspect command on the container id. We saved this id into @user.container_id during registration (see users_controller.rb). Let’s retrieve this id from the database.

Be sure to replace www.sqlite3 by your database.

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

julien@cs50:~$ sudo iptables -L[sudo] password for julien:Chain INPUT (policy ACCEPT)target prot opt source destination

Chain FORWARD (policy ACCEPT)target prot opt source destinationACCEPT all -- c-69-42-42-42.hsd1.ca.comcast.net 172.16.42.14DROP all -- anywhere 172.16.42.14

Chain OUTPUT (policy ACCEPT)target prot opt source destination

julien@cs50:~/final_proj/SaaS_Memcached$ sqlite3 db/www.sqlite3SQLite version 3.7.13 2012-06-11 02:05:22Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> select container_id from users where email = '[email protected]';190c7d70fbc1

Page 28: Build Your Own SaaS using Docker

Build Your Own SaaS

On our server, the id of [email protected]’s container is 190c7d70fbc1. To check the IP of the container we can use the docker inspect command.

Be sure to replace 190c7d70fbc1 by the right container id that the SQL request gave you during the last step.

As expected the IP address is 172.16.42.14. If you do the same on your server be sure that this IP matches the one shown in the iptables listing.

Congratulations! You are now running a Memcached SaaS, with a simple security layer. Congratulations!

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

julien@cs50:~/docker-master$ ./docker inspect 190c7d70fbc1 | grep IpAddress "IpAddress": "172.16.42.14",

Page 29: Build Your Own SaaS using Docker

Build Your Own SaaS

Where to go from here

We’ve seen how to run Memcahed as a service with Docker. But you could create your own container image, running another service. John Costa did write an article on how to install Redis on Docker for instance. But you could create an image running any service (MySQL, MongoDB, PHP, …), and then build a SaaS using this container image.

Why should we offer only one type of service on our SaaS? We could offer multiple services. We could simply add a new table “services” to the database so that our users could be able to have multiple services. And we could add an admin page in order to list, activate and deactivate the available services.

The website shown in this example is very basic. We could easily improve it. We could for instance:

- add an admin page to list users and delete/suspend them- let users change recover and change their password- let users delete their account- let user restart their Memcached server- let the user specify the memory limit he needs (using run docker –m) - let the user know how much memory he uses- add a payment gateway to make our customer pay for the service- …

You can also add more security, scalability, etc… but this will be another story :)

I hope you had fun playing with this article. Feel free to contact me if you have any question.

Happy SaaSing!

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker

Page 30: Build Your Own SaaS using Docker

Build Your Own SaaS

To be continued…

http://www.slideshare.net/julienbarbier42/building-a-saas-using-docker