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<i> </i>
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.
<strong><span style="font-weight: 400;">docker run -d --name df-redis redis</span></strong>
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.