Month: December 2017

Docker Explained for PHP Developers in 2018

Docker Explained for PHP Developers in 2018

This post assumes you have a basic understanding of Docker. But what is a basic knowledge? If you aren’t sure, I suggest you read the Get Started docs, and then you will have a basic understanding of Docker.

I’ve read a few posts on Docker for PHP developers. However, it was the same tutorial on different sites, so I’m not going to copy what they did. I’ll explain the concepts that I struggled with learning.

My objective with Docker is to develop locally and deploy to a Swarm. I hope to have a fast site with high-availability that can be updated and deployed quickly.

Developing Locally

I was recently working on a project that uses SOAP hosted on a slow connection. Instead of hitting that API each page load, I decided to store the results in Redis, and load them from there. This helped speed up the pages that needed to access the data from the remote API. Installing Redis on my local system wasn’t what I wanted to do, so I simply added a Redis service to my “docker-compose.yml” file, and it worked great. You can easily add new services to your stack while developing locally with Docker.

Being able to edit your code, with all the services running, and see the changes immediately is very valuable. I like to develop on Windows, OSX, and Linux, so my environment is drastically different on each machine. It would be very time consuming if I had to have Redis running on each of my dev environments. Having Docker running the same services locally on my dev machines as what is running in production is priceless.


The Docker Compose file

You will need two use two “docker-compose” files, one for local development, and one for deploying to your swarm. The main file, “docker-compose.yml,” will be used when deploying to your production environment. And “docker-compose.override.yml” will be used when developing locally. You want to copy your PHP code into the container when you are deploying to production. When you’re developing locally, you want your code outside of the container, so we mount it with volumes in the override file.

Docker Swarm explained in a drawing

You can use Volumes in production, but you will need to copy the files to the node where the container will live. That can be complicated and in my opinion, isn’t ideal. It does work well when developing locally and your computer is the node since the files are already on it.

Docker Services and etc

All of these terms can become confusing, at least they were to me, but I hope to explain them shortly. We used to refer to a “web server” as a server that ran everything a website needed. Even though it was a server running daemons like Apache’s httpd.

Now we have stacks, containers, node, swarms, services, and a lot more words to describe things. Communication is important when speaking with others about the approach you wish to use when solving problems. Understanding what you’re saying is even more important. I’m going to try and define these terms myself.

A service is a piece of software written that we will use to help us accomplish our goals. MySQL, nginx, Apache, PHP, node, and etc are all services we can use with Docker.

Your stack is essentially a list of the services being used for the app. For this example, I’m using haproxy, nginx, and PHP-FPM.

A container is a way to virtualize the service(s) on the computer. FreeBSD has had this ability for a long time with Jails, but Docker has made it much easier. Containers can’t access other resources running on the server.

A node is a single instance of an Operating System. Your computer could be a node, as well as a Virtual Private Server or a Virtual Machine or an entire bare metal server. Node’s run an Operating System that runs the containers.

Swarms are really awesome! They are a collection of nodes and can distribute the containers across them as needed. They can have their own private network and do load balancing.



This app is using haproxy, nginx, and PHP-FPM. I’m passing SSL through haproxy so nginx can handle SSL termination because I’m not sure how to do it with haproxy. The load balancer, haproxy, passes the request off to one of the web services. One of the web services, nginx, then connects to a PHP-FPM service to process the PHP.

Be sure to check out the levilol/php-docker repo on Github.

Docker is an excellent tool that I hope to learn more about this year. Other’s have predicted it will get much more attention in 2018 and I agree. I’m going to end this post with my simplified description of Docker.

Imagine if you could compile your app with all the required services and end up with one binary file that contains everything – that is Docker.

Please leave any feedback below in the comments section.

Discover 3 Tips Every Developer Should Know in 2018

Discover 3 Tips Every Developer Should Know in 2018

In this post, I will help you discover 3 tips every developer should know. For the people reading this post who are hoping to learn how to program, no one is perfect, and we all make mistakes, so have fun while you learn. Everyone else, leave a comment below if you can relate or have any other tips!


I think it is rare when code works as expected the first time you try it. Sometimes error messages seem very cryptic and are hard to understand, but if you read it carefully, it may give you some context as to what is wrong. Usually, you forgot a semicolon, and you’ll become very familiar with that error message, I am at least.

While you can Google your error messages all day long and find millions of results, they won’t always be helpful, so you need some tools to figure it out on your own. You are expecting your variables to contain specific values, so I usually check to see what is in those variables. Using console.log or var_dump can help you figure out where your code went wrong.

Sometimes you will have to dig a little deeper. So I encourage you to reduce the complexity of the error-prone code until the error goes away. You may want to make a copy of the files current state before you start removing lines of code.

Rick and Morty looking for something

Just remain calm, don’t stress, and use your brain. It may help if you step away from the computer for awhile. Maybe go for a walk or pet your dog.

Trial and Error

If at first, you do not succeed, don’t immediately ask for help, but do try it a different way. Your code may be getting too complicated, and it could be best for you to refactor it. Again, I suggest you step away and pet your dog if you get stuck.

I like to keep a log of what I’ve tried and when I tried it, why I thought it would work, and what happened. Doing this helps me keep track of where I was going with my thoughts. You don’t want to get a hunch and forget it; that’s a waste of your time.

You may need to go back to the planning phase and see if you still think this is the best way to approach the problem. I use pen and paper a lot when I’m working on larger projects. Our minds are incredible, but being organized helps me a lot.

Asking For Help

We all make mistakes. Sometimes, we think the error is not our fault, when in fact it is. Don’t be fearful of asking for help, but also, don’t let it be the first thing you do. Asking for help should be your last resort.

When you do ask for help, give enough information for people to understand the context of your situation, but don’t overload them with garbage. I often find myself typing up a question on StackOverflow when I realize what I did wrong. Even talking out the problem with a co-worker can help you understand what you did wrong. If you don’t have a co-worker, you can always tell your dog. They’re good listeners.


The title, Discover 3 Tips Every Developer Should Know, hopefully, encompasses the content of this post. Should you ever need any help, please feel free to leave a comment, or send me an email.

Advanced PHP is Actually Simple – Learn How Today

Advanced PHP is Actually Simple – Learn How Today

Advanced PHP methods, practices, processes, and everything else can be a bit daunting. If you find it hard to understand some of the documentation or tutorials out there, don’t worry, I can help! I struggled to learn some of the core concepts, but eventually figured it out, I hope I can help you learn with less of a struggle. Having a good development environment is a huge step in the right direction. For work, I use a Windows machine and a MacBook Pro. At home, I have a desktop running FreeBSD. Use whatever you are most familiar.


You don’t have to do everything yourself; this was a hard lesson for me to learn. There are groups of people who focus on a single PHP package. That package is going to be well written, tested, and hopefully documented. I suggest you use PHP packages when you can. You can browse the available packages on packagist. You will need to have composer installed on your dev machine. If you have an idea for a package, then read my post on creating a PHP package.


The autoloader isn’t magic, but it is very helpful when writing advanced PHP. It will conditionally include the needed PHP files based on the code being run. No longer will you need to have dozens of includes as the autoloader will handle most of them for you. Packages have a registered namespace that tells the autoloader where their files so it can load them. This allows you to easily instantiate a new object without having to include the needed files.

PHP Design Patterns

You’re only making it harder for yourself if you aren’t using design patterns. There are plenty of definitions of design patterns, and none of them make sense. When a definition is defined with other complicated words, I find it confusing. Below is a simple explanation of design patterns.

A design pattern is a way of structuring your code so it can easily be extended, enhanced, or fixed. Most advanced PHP applications will use a couple of design patterns. They’ll help keep your code organized and understandable.

I have written a few posts on different design patterns, such as the adapter pattern, dependency injection, and the factory design pattern. “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma is an amazing book if you want to learn even more.


There have been a lot of very public data breaches in the last couple months, so I will also cover some basic security tips. Whether your application is used by a couple of people or hundreds, you should always practice safe security. If your app uses a database, an API, or some other type of service, it could be vulnerable.

When you are writing information to your database, please use PDO. PDO is faster and more secure. PDO Tutorial does an excellent job of explaining everything clearly and concisely. Feel free to ask any questions in the comments below.

Another important aspect of application security is protecting against CSRF (Cross-Site Request Forgery). If I were to create an HTML form on my website and set the POST parameter to your website, how would your website know where the post came from? It’s a very difficult thing to do unless you use a CSRF token with your forms. When a user sees a form, you also generate a CSRF token. You add the CSRF token to a session variable and to a hidden input field that is part of the form. Then when the user submits the form, you can check the hidden input field data against the session variable. If they match, then you know they submitted the form from your site.

Visit the OWASP website if you would like to learn more about security.

Advanced PHP Conclusion

Programming is all about solving problems. We take a big problem, break it into smaller problems, solve the smaller ones first, and then we’ve solved the big problem. Keep trying to make your code smaller, simple, and elegant. Programming is art for logical people like yourself. Keep being curious. Let me know in the comments below if you have any questions. I would love to help.