Cloning the MAMP concept with Docker -- DAMP stack: Docker, Apache, MySQL, PHP, etc
The "DAMP Stack" is a tongue-in-cheek play on a product, MAMP, and WAMP, that simplifies running or developing Apache+MySQL+PHP applications on a desktop Windows or macOS computer. It's about using Docker on the desktop, achieving the same goal served by MAMP or WAMP, while having approximately a zillion more angles of freedom.
Twenty years ago MAMP was a godsend, letting us easily manage LAMP development on our laptop. While macOS came with Apache and PHP built-in (and Python and Java and more), the MAMP product gave us the combination of choosing precisely the version of PHP and Apache required for our deployment, and with the added benefit of bundled databases (MySQL, Postgres, etc). And for our Windows-using brethren, WAMP offered the same features.
Fast forward to 2020+ and Docker has become a very important part of the computing landscape. Deployments often use Docker containers rather than hosting directly on a server. The advantage is that the Docker container you tested on your laptop is the exact same Docker container that's deployed to production.
By contrast, MAMP and WAMP cannot offer the same guarantee. MAMP and WAMP helps Windows or MacOS users run Apache+MySQL+PHP/Perl/Python services. This can be either for developing LAMP stack code, or for running a local server in an office. But the regular servers used in typical deployment generally run Linux or another Unix-like system, which is different enough that ...
Stereotypically a developer pressed on why they deployed buggy code to the production server might say "It worked fine on my machine". But of course there are environmental differences between the laptop and the production server, making it quite possible for code to both work on the developers laptop, and to fail on a server.
Our focus in this area is a combination of application development, and local deployment of small scale services.
For me that means an Intel NUC, on my desk, running a Docker service running Gitea (Git hosting), Jenkins (build/CI), Plex (video streaming), Bookstack (keeping notes), Kanboard (project tracking), NextCloud (file storage), all of which are useful in the daily life of our household. Docker on my laptop is used to develop things that end up hosted on a virtual private server rented from a regular hosting company.
Disclaimer: This tongue-in-cheek "DAMP Stack" is completely unrelated to a product named DAMP from Acquia, almost 10 years ago. That DAMP product obviously had nothing to do with Docker, because the 'D' meant Drupal. Here, the 'D' is all about Docker.
Getting started with Docker on your local machines
Let's start this journey into Docker by learning how to install it on popular systems, namely macOS, Windows and Linux. Installation is very simple thanks to the hard work of the Docker team. You youngsters don't know how easy you have it now that Docker for Mac and Docker for Windows exist. Long gone are the days when we had to install VirtualBox along with a specialized virtual machine to use Docker.
Having familiarized ourselves with Docker, let's take it for a more serious spin. Namely, let's set up a basic LAMP Stack application, by installing MySQL, PHPMyAdmin, and Wordpress.
Set up MySQL using Docker on your laptop
Setting up MySQL on Docker is fairly simple, and the MySQL team has created a flexible Docker image that can be used in many ways. Databases, like MySQL, serve as the data persistence and retrieval layer of many applications we might deploy.
Set up PHPMyAdmin and Wordpress Docker containers using existing MySQL
There are several ways to launch a PHP service, backed by a MySQL database, on Docker. That combination is literally the LAMP Stack (Linux, Apache, MySQL, PHP/Python/etc), and we can use Docker to easily set up this combination on our laptop or deploy it to a production server. Elsewhere we went over setting up MySQL on Docker, so lets now demonstrate the LAMP Stack, on Docker, by deploying PHPMyAdmin and Wordpress on our laptop.
Using Docker Compose or Swarm
Deploying MySQL to get started using a Docker Compose file
While the docker run CLI command is powerful, Docker Compose files let us succinctly describe a whole system of Docker containers, virtual networks, and file systems, in one easy to read file. This is a powerful tool with which you can easily describe deployment of a full application stack. Compose files support not only deployment to the local Docker host, but to Docker Swarm clusters, and now to AWS ECS and Azure ACI cloud platforms.
Wordpress local development environment with Docker and Docker Compose on your laptop
We normally install Wordpress on a public Internet server, and installation on your local machine is normally limited to developing or testing a Wordpress feature or theme. The development loop is a lot shorter when you can directly edit Wordpress files on your machine, as opposed to working out a method to edit remote files. While it's convenient, it's not always clear how to create a website hosting environment on your laptop. In the past we would use MAMP (or the Windows equivalent), which is a macOS-native environment for running Apache/MySQL/PHP stacks. But today we have a different tool, Docker, that is easily able to run that stack, as well as any other stack. Docker easily runs on a laptop, so let's take a look at using it to run a local Wordpress development environment.
Using Docker to host long-running services in your home or office
It's one thing to use Docker on your laptop to experiment, eventually to develop a service that's deployed to a cloud hosting platform. But Docker can also be used locally, in your home or office. It's possible to do this simply, inexpensively, at low power consumption.
There are many 3rd party services - Dropbox, Github, etc - where you can just sign up, and it's free until your usage reaches a certain point and you're paying monthly service fees. These are convenient, but you're giving your data to others, trusting that they'll protect your data, and you're tying yourself to a monthly fee. There are many open source projects offering similar capability to commercial services, that you can install on your own hardware, usually using Docker. A simple computer like an Intel NUC can be a potent server, costing $300 or so to set up, and it pays for itself after 30 months of avoiding $10/month service fees.
Self-hosting services using Docker
Self-hosting is the opposite of using a 3rd party cloud service like Dropbox or Google Drive, and instead to host an equivalent service, like Nextcloud, on their own hardware. For every popular cloud service there is probably an independently developed open source equivalent one can host on their own hardware. Self-hosting gives one control, and peace of mind that their private information is not being sold to others, and it can save a lot of money from service fees that aren't paid. The personal security implications are huge, since it is known that governments around the world are pressuring internet services for data on user activity. While there is a legitimate need to know about criminal activities, do we really need to risk our data being snooped upon?
Advanced topics in using Docker for local services
Connect with SSL to MySQL in Docker container
MySQL might throw an error when an application connects without using SSL to encrypt the connection. Therefore the MySQL team clearly hints it is best to use SSL connections to MySQL servers. Clearly a database connection has critical data that you don't want to leak to 3rd parties, and encryption is a great counter-measure. The official MySQL Docker container automatically generates a set of SSL certificates to use for connections, so let's see how to put those certificates to use.
Understanding MySQL the Access Denied error in or outside a Docker container
I once wasted more than a full workday fighting the MySQL error message Access denied for user (using password: YES). After finding the solution I want to help others avoid this problem. Along the way to fixing the issue, I learned a lot about how MySQL authenticates user ID's. I've been using MySQL for years and had glossed over this topic, but it turns out to both not be terribly difficult, and very crucial to the solution.
Using multiple databases and PHPMyAdmin in a single MySQL Docker instance
Sometimes our applications need multiple database, but with a single database server instance. The typical recommendation is for each Docker container to serve a single application. For databases, that means one database server for each application. That's the result of following a theoretical best practice, but isn't there another best practice to avoid excess processes and overhead? MySQL, like most other database servers, can support multiple databases, multiple user identities, and has an extensive system for controlling access on IP address, user names, and passwords. The MySQL container for Docker is easy to use, but supports only a single database instance per server container. In this article let's learn how to go ahead and configure the MySQL container for Docker to support multiple databases each with their own user ID.
Scheduling background tasks using cron in a Docker container
The cron service has, for time immemorial in Unix-like systems, long helped us schedule the occasional background process that keeps our systems ticking. Typically it is used to gather up or summarize log files, or collect and process data from external systems. In the old days when e-mail exchange, and the all-too-important Usenet news exchange, happened using UUCP over modem lines, a cron job scheduled regular UUCP calls to neighboring servers. Having cron running in the background is part of normal Unix/Linux/etc system admin practices. Even though the crontab format is kind of hokey, we all learn it and set up automated background tasks to keep the world functioning. Let's see how to set this up in a Docker container.
Launching some useful local services
Easily manage Docker containers on both local and remote Docker hosts with Portainer
Docker is a wonderful advancement for software engineers and system administrators. It simplifies launching and maintaining background processes, while adding a layer of much-needed encapsulation and security. But the default command-line administrative tools are less than pleasant to use, and we instead want a good GUI with which to manage our Docker hosts. That's where Portainer comes in. It manages the Docker containers we have running on our local host, or on remote hosts.
Installing Gitea for self-hosted Git service, replacing Gogs
While Gogs is an excellent tool to have a self-hosted Git service (like Github), I recently found out the project is semi-abandoned. A group of Gogs users launched Gitea as a replacement, and in any case it looks like a better server. The goal here is to install Gitea, evaluate it, and see how to convert Gogs-based repositories over to Gitea. The result will be managed in the Docker self-hosting machine I have at home.