Scaling DreamFactory With Docker
Wondering how to effectively scale your DreamFactory instance with Docker? Look no further!
Join the DZone community and get the full member experience.Join For Free
docker containers are great when it comes to deploying your application for production, testing, and scaling up for performance. dreamfactory instances can take advantage of docker containers as well. in fact, it’s even easier to horizontally scale dreamfactory instances (with or without docker containers) because dreamfactory uses json web tokens (jwt).
dreamfactory uses jwt to manage user sessions. jwt is completely stateless, which is important for scaling apps that rely on http requests under load. dreamfactory web servers do not maintain session state at all. the jwt itself (token) that is passed around in every request is sufficient to hold the minimum data required to maintain the session. therefore, when horizontally scaling across multiple web servers (or docker web containers) running dreamfactory, the load balancer doesn’t need to maintain session across the instances. the only requirement is to share the same app_key (located in the .env file of a dreamfactory instance) across all web instances of dreamfactory.
to make it easier to deploy dreamfactory using docker containers, we’ve created a dreamfactorysoftware/df-docker image on docker hub. this image uses the mysql and redis images for the dreamfactory system database and cache storage, respectively. simply follow the instructions on the dreamfactorysoftware/df-docker image to deploy a single instance of the dreamfactory docker container connecting to the mysql and redis containers.
here we'll show you how to deploy multiple dreamfactory web containers, all sharing a single mysql and redis container. then we’ll put the web containers behind a load balancer. before starting, make sure that you have docker installed. just follow the docker installation docs .
start by pulling the dreamfactorysoftware/df-docker image.
docker pull dreamfactorysoftware/df-docker
start the mysql database container under the name df-mysql.
docker run -d --name df-mysql -e "mysql_root_password=root" -e "mysql_database=dreamfactory" -e "mysql_user=df_admin" -e "mysql_password=df_admin" mysql
then start the redis container under the name df-redis.
docker run -d --name df-redis redis
now we’ll start three web containers: df-web1, df-web2, and df-web3 running a dreamfactory instance using the dreamfactorysoftware/df-docker image.
docker run -d --name df-web1 -e "app_key=useany32characterslongstringhere" --link df-mysql:db --link df-redis:rd dreamfactorysoftware/df-docker
docker run -d --name df-web2 -e "app_key=useany32characterslongstringhere" --link df-mysql:db --link df-redis:rd dreamfactorysoftware/df-docker
docker run -d --name df-web3 -e "app_key=useany32characterslongstringhere" --link df-mysql:db --link df-redis:rd dreamfactorysoftware/df-docker
as mentioned before, all instances of dreamfactory containers must use the same app_key. just replace useany32characterslongstringhere with any 32-character long alphanumeric string.
now start the load balancer under the name df-lb using the tutum/haproxy image from docker hub and link all three dreamfactory containers to it.
docker run -d -p 80:80 --name df-lb --link df-web1 --link df-web2 --link df-web3 tutum/haproxy
now using a web browser, point to ip 127.0.0.1 (on mac os x this should be the ip address of your docker machine). you should see the “hello and welcome…” greeting page followed by a form to create the first dreamfactory admin user.
congratulations! you have successfully deployed multiple instances of dreamfactory under a load balancer. all these instances share the same mysql database and redis cache.
to make sure the load balancer is working and using all three instances, go ahead and create the first admin user and log in to dreamfactory admin console. then click on ‘config’ tab. now notice the ‘host’ field under the ‘server’ section. it should show you the docker container id of the web container being used. refresh/reload the admin console on the web browser and notice that the ‘host’ has changed to a different container id. refresh again and the host should change again. every time you refresh the admin console the load balancer uses a different dreamfactory container.
i hope this blog post explains how easy it is to deploy and scale dreamfactory with docker. let us know what you think in the comments or head on over to the community forum if you have any questions or feedback.
Published at DZone with permission of Arif Islam, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.