Keep Moving Forward | X-Team Magazine

Keep Your Local Machine Clean with Docker

Written by Jacek Ławniczak | Mar 29, 2017 4:00:00 AM

For some time now, I have been practicing a very strict way of keeping my local development machine clean and universal.

Working as Developer Unleasher for X-Team, I am in a unique position of having to deal with several different programming languages and technologies. Very often, I switch between projects or start new ones.

For all those reasons, I like to keep my computer free of any packages or services that are required to run specific projects.

If I had all required packages and services to run those projects on my local machine, they would simply run into conflicts with each other. For example, one PHP-project will be running on PHP7, but another would still run on a lower version. Some projects are configured to be served by the Apache Web Server and some by Nginx. Same goes for Node.js and projects running on all sorts of versions from 4 to 7. Node.js requires npm. Some tool requires globally installed node packages. Some projects use yarn. Then there are ports on which services are available. Do you get my point?

For some reason, I find myself changing computers very often. I remember when I used to spend all day to replicate all the required tools I needed to get back to development after getting a new machine. Those days have long since passed.

The Zen of Docker

I work on Mac machines; this is my preference. Moreover, when I get a new machine, I usually go through a few simple steps to setup my development environment.

  • I install Docker for Mac
  • I install GIT ( and X Code Developer Tools )
  • null

That is it. My number one rule has been, for some time now, I do not install anything on my machine to run projects. There is always a way to use Docker to get it going, so why would I bother?

Of course, this approach requires every project I work with to be “Dockerized”. That is actually awesome, not an issue.

However…

I found that, very often, it took me some time to find a simple solution to create a Docker wrapper for things I wanted to do. If I simply looked for “Wordpress Docker”, “Symfony Docker” or “Node.js Docker” I would not get proper results. In fact, there was always something wrong with the setup found online.

A good setup including Docker is the one that you will start with one simple command: docker-compose up or docker-compose run...

Solution

I started creating and sharing my own Docker “bootstraps” for several different technology stacks, which had the essentials to start playing with the given technology. These have been created only for quick setups like following some online coding courses or solving some problems.

Usually, all projects I work with are “Dockerized” and easy to setup on my local machine, without the need to install any additional components.

For Wordpress projects, I created this wrapper that helped me run a simple Wordpress installation, mainly to be able to help on StackOverflow with Wordpress-related issues.
It could also be a good starting point for any Wordpress project you have in mind. It supports automated SQL backups:

jacekelgda/docker-wordpress-mariadb

For Node.js projects, I created this wrapper, which already includes Express.js, with a simple “Hello world!” example. It also has an npm package installed in the image that monitors file changes and restarts the Express.js server to show the newest results:

jacekelgda/docker-node-express

For Symfony projects that I needed to create quickly to prepare a BDD workshop, I created this simple installation, which could also serve as a base for any project. It has been constrained to work only for the development environment:

jacekelgda/docker-symfony-nginx

All of the above are ready-to-run with services that keep them alive and which expose content to your web browser.

Just recently, I have been going through excellent Egghead.io online courses for React.js, and I found myself creating this simple docker wrapper to code along with the tutorial. This wrapper uses the Webpack module bundler to generate a JavaScript bundle, which is later included in the index.html. You can simply open it in your web browser, without the need of running a web server:

jacekelgda/docker-react-webpack

The Recipe

I hope this short post will inspire you to try running everything in Docker containers. The advantages are obvious: it is fast and easy.

If you would like to create your own development playgrounds to clean your local machine of project dependencies, let me give you some advice:

  • Try to keep your project universal up to the point where everyone will be able to use it for their own purpose.
  • Remember that your containers should be running a service, otherwise they will exit.
  • Use docker-compose as it keeps configuration and relations of your services and containers in simple form. It also lets you run commands using service names, which is very handy. By default, currently, syntax version: '2' creates a network between services, which is also very handy.
  • If you do not have services running inside your containers use docker-compose run or solutions like supervisor.
  • Publish your solutions to GitHub to reuse in future projects and share with others.
  • Try to use base images only, as other solutions add additional baggage, which you might not need.
  • If you have the option, try using the same base image versions, to use less disk space on your machine.
  • Always test your repositories by checking them out in a new directory and following the installation process you prepared in the README.md step by step, to see, if it still works.

Thanks!