Developing Sugar on Docker

TLDR 

Developing Sugar on Docker, get up to speed now!

The long version

What are Docker containers?

If containers for you are more than the two pictures below… then you are in the right place!

As you might be aware, there has been and there continues to be a lot of hype around containers. The hype really started with Docker, but containers have been around for a while (more or less), in a different shape or form. If you are not yet familiar with containers or are confused about the difference between a virtual machine and a container, this blog post from Docker might be useful to you.

Personally, I was deploying container-based virtualised single purpose hosting environments (now labelled micro-services?) for my customers, back when it was not so trendy. It was about 10 years ago when I was leveraging OpenVZ. OpenVZ is slightly different (eg: it has state/persistent storage for every container), but in the end you can run multiple lightweight containers that share the custom linux kernel of their host.

What Docker has managed to do differently is make infrastructure deployments repeatable and portable. An application can be shipped together with its Docker container directives, providing the same exact configuration across different host environments. The infrastructure looks more like code, which simplifies the process of automating and delivering software deployments. Teams using Docker found that the lines between development and operations blurred even further, and guess what? DevOps was born!

Docker has made it easy to get up and running with a technology for development purposes. A developer just needs Docker installed on his or her local machine (or on a linux VM) and a “Dockerfile”, to get an application up and running. If he or she needs to replace a single component of the infrastructure for testing purposes, they simply swap a few directives and re-build. The same application is executing on top of a slightly different platform. How awesome and easy is that?

With the always increasing pace of software development innovation required today, and with applications becoming more and more decoupled from each other (and at the same time integrated), development teams need to keep their deployment velocities up as well quickly deliver innovation across the board on a multitude of heterogeneous backends.

This need for a fast deployment velocity can be fulfilled with the aid of flexible containers and orchestration technologies when their delivery is seamlessly baked into the traditional software development pipeline. Obviously containers are not the silver bullet or the answer to every known question, but they are currently applicable to a vast set of different workloads and use cases.

Docker in production

At this stage, I do not have direct experience with Docker in production environments, but, as with any technology, there is always someone willing to share their horror stories…

Jokes aside; with the increase in popularity of orchestration, the extensive backing and support from various cloud providers and how lightweight it is, I am convinced Docker is the way to go for micro-services deployment and orchestration in production environments as well. As a matter of fact, container-technology adoption is constantly growing across the board and not just for micro-services.

Sugar on Docker

Enough said about the containers topic. Here it is, my attempt at creating a set of development stacks for Sugar with Docker.

I have to thank one of our long-standing partners Cédric Mourizard from Synolia for the initial work (read more in his previous blog post). The Sugar Docker setup has been polished here and there over the past 6 months, and it is now to a point where I only run Sugar locally with this solution.

I highly encourage you to give it a try. Have a look at how Docker can fit in your development process, and, if it does not yet fit into it, think about how it could help you streamline your overall software delivery pace and capabilities.

Flavours used

Let’s give a quick overview of the various components used. When using *nix, I’m a Debian kind of guy. So that’s that.

LAMP stack, Elasticsearch and Redis on top of Debian.

Stack components

It might evolve over time, but for now the main containers are:

  • Apache load balancer - Load balances requests between the cluster of Apache PHP web servers, round robin
  • Apache PHP web server - Web server(s)
  • MySQL database - Database
  • Elasticsearch - Sugar search engine
  • Redis - Two purposes: Sugar object caching service and PHP Session storage/sharing service
  • Cron - Sugar background scheduler processing. Note that this is enabled immediately and it will run cron.php as soon as the file is available, and it will attempt to do so every 60 seconds since its last run
  • Permission - Sets Sugar instance permissions correctly and then terminates

The application files are shared on the “cluster” (if you can call it that) through a directory based mounted volume to offer persistent storage. We also have various persistent storage directories within ./data/ for all the other software components that need them.

Based on the selected stack (within the ./stack/ directory) you will be able to pick and choose the various versions of the stack components available, and to decide if you want a load balancer and multiple web servers or just a single web server during initial development phases. Because the Docker containers are stateless, it is possible to run the same Sugar installation (stored on the persistent volume) either with PHP 7.1 or PHP 5.6 and swap between versions within seconds.

Current software versions

Currently, there are a few stack combinations available that run the supported platform for Sugar 7.9.

Containers are available with PHP 7.1/5.6, MySQL 5.7, Elasticsearch 1.7.5 and the latest Redis. When selecting a stack, it is possible to run it as a single web server or as two web servers behind a load balancer.

Sugar 7.10 is not available for on-site hosting but it can be downloaded for local development, so, I have prepared a single stack leveraging PHP 7.1, compatible with the required matching platform. The main difference in stack requirements between Sugar 7.10 and earlier versions is the Elasticsearch version 5.4 requirement. Elasticsearch 5.4 seems to be more memory hungry, so be prepared for that.

Finally, there is an additional stack for 7.9 upgrades, running both Elasticsearch 1.7.5 and 5.4, to allow future testing of upgrades to the Sugar Spring '18 release.

Getting up and running

Most of the details you need to get up and running are within the README.md of the git repository. In terms of where to run this setup, you have the option of leveraging docker-machine or to run your own VM, as currently the native Docker for Mac does not perform well with the mounted volumes for persistent storage.

I personally use a Debian VirtualBox VM on a Macbook. If you want to run the same VM as I do, I have prepared a VirtualBox appliance export. You can find the appliance on this repository, as well as more details on how to deploy it on your Mac. It runs a NFS share as well, so that you can mount the directory locally and code with your preferred editor.

During my process, I’ve also tried Vagrant to help with automatic deployment of the VM instead of building an appliance, but I did not obtain the same performance footprint (not even close). So I abandoned that route long ago.

Now you are up to speed with Sugar on Docker, and you might have realised how containers can help your business gain velocity.

Feel free to let us know your thoughts, by leaving some comments below!

  • Kenneth Brill

    I would like to hear Enricos comments, but for me it has been the ability to switch platforms and stack parameters. If I have a client that is on 6, I can easily spin up a platform with PHP 5 and older mySQL. If I am working on 8, then it's cake walk to switch to that.

    If I am working on an enterprise project, I can cluster a couple of application servers. Enrico did a great job and I use the stacks daily. I used to use VMWare Workstation 12, but I find docker faster. Just my opinion.

    I guess one other benefit is that all the working directories for mySQL and ES for the container are on the host machine. I recently upgraded a computer and it was nice copying the /srv/SugarCRMDockerized directory and every platform and database got migrated and spun up flawlessly on the new computer.

    Other things like xdebug and other tools wire in nicely as well. I know you must know these things but I am posting with the hopes that others might understand the benefits.

    Thanks!

  • OK, after 6 months of experimenting with docker for SugarCRM development I am left with one question.

    Assumptions:

    1. Docker doesn't seem to play well with MacOS (I am running a current version but maybe they fixed this and I am behind the times, maybe?)
    2. All developers on my system use MacOS
    3. To get any speed out of the system you have to run docker in a Linux VM.

    Question

    What use is docker then?  It seems to me that running a containerized system on a virtualized system kinda doesn't add up.  Why wouldn't I just run LAMP on the Linux system.  What extra benefits do I get that would mitigate the extra hoops I have to jump though?  I could see a benefit if I ran docker directly on the MacOS system or if we just moved everyone to Linux boxes but is docker worth that kind of change (or speed degradation in the MacOS scenario)?

  • Diego, 

    At first glance it appears that your .htaccess might not be happy. I myself am looking for a way to host multiple clients off the same web root.

    However, you may want to not that the cron container is most likely fixed to data/app/sugar and keep that in mind.

    Enrico Simonetti please correct me if I am wrong.

  • Hi Diego Celdrán,

    SugarDockerized has been built specifically to work with one only Sugar instance at the time. Modifying it, might require more work than you think, as there are many components that rely specifically on the system living within the "sugar" directory and with one system only live at the time (eg: permissions fixing, opcache settings, cron etc). Hence my best guess on why you see the errors.

    I have never personally encountered this problem before, by using only one instance.

    There are tools that have been built and made available with the utilities directory of the project, to take backups and restoring other sugar installations, or even the full data directory, to facilitate and speed up switching between sugar installation, but still it only supports one active instance at the time.

    Let me know how you go with this approach

    Hope it helps

  • Hi Enrico Simonetti,

    When i work with docker and develop in sugarcrm, every 10-15 min i get 404 errors and 500 errors and the same into the javascript console.

    When i work with vagrant i don't have these problems.

    For example:

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    I edit your docker files and with one development environment i can develop more than one instance, is for that the url is not /sugar.

    But you can see are random errors, normally appears when i have developer mode enabled and i reload Sugar without cache and i enter some record in any module.

    I just do f5 and i don't have the problem. Is a little frustating because every javascript development, the normal is reload the page without cache a lot to see the changes and every time i have to do f5 again.

    When you work with docker, do you have the problem?

    Thanks!