Dockerizing a simple PHP web app

After learning how to download Docker images and run them in containers, I wanted to try my hand at Dockerizing an app myself.

myTinyTodo is a fairly simple PHP web app that uses SQLite for data storage. I have been using myTinyTodo for several years, hosted on a Digital Ocean VPS running on LAMP. My wife and I use it on a regular basis for grocery lists and other shared to-do lists. I knew it should be a good example for Dockerizing an app, and I would then be able to use it myself. It’s also been mentioned a number of times on the /r/selfhosted subreddit, so I know it has other users as well.

This post was helpful in getting started: https://semaphoreci.com/community/tutorials/dockerizing-a-php-application

Preparing the Dockerfile

I followed along the same lines as that tutorial to start by basing my image on the nimmis/apache-php7 image, which would have apache and php installed and configured properly. Ports 80 and 443 were also exposed, and apache was started in the foreground:

FROM nimmis/apache-php7       
MAINTAINER Scott Breakall <scott@breakall.org>
EXPOSE 80
EXPOSE 443
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Next up, the Dockerfile calls for SQLite to be installed:

 RUN apt update && \       
apt install -y sqlite3 libsqlite3-dev

Now we have apache, php, and sqlite all set up and ready to go.

Next, copy the mytinytodo folder into the apache folder

COPY mytinytodo /var/www/html/

myTinyTodo needs write access to the db folder in order to make use of the SQLite database. In addition, clean out the default apache index.html:

RUN chown -R www-data:www-data  /var/www/html/db/
RUN rm /var/www/html/index.html

GitHub project

Everything described so far is available in this GitHub repo, including myTinyTodo 1.4.3:

https://github.com/breakall/mytinytodo-docker

Building the image

Download the myTinyTodo installation package into the same folder as the Dockerfile, and unzip it to a folder called myTinyTodo.

Execute the following command:

docker build . --tag=mytinytodo 

This will build the image and name is mytinytodo.

user@pc:~/temp/mytinytodo-docker$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytinytodo latest a1c6238be95f 2 hours ago 550MB

Now that the image is built, run the image as a container:

docker run --name mytinytodo -p 80:80 -p 443:443 -d mytinytodo 

Run docker ps to see the container running:

user@pc:~/temp/mytinytodo-docker$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
ca9ce3ab2d0f mytinytodo "/usr/sbin/apache2ct…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp

Complete setup in the browser

Open the app in the browser to see it running:

I haven’t scripted the database setup since it’s incorporated into the first run of the app anyway, so click the setup.php link.

Click the SQLite radio button, then click Next.

Click Install.

Execute the following to remove the setup.php script:

docker exec mytinytodo rm /var/www/html/setup.php 

Reload the main URL:

Success!

Future improvements

SQLite setup

The database setup could be scripted as part of the build process so that the app runs 100% out of the box upon running.

MySQL setup

Since this build is focused on using SQLite, MySQL is not installed at all. If MySQL was desired, the ideal setup would be to have one container to run the PHP app in, and another container to run MySQL in. This could be accomplished by creating two images, and using Docker Compose to orchestrate the creation of two containers that would work together.

Update: the info above is already out of date since I got a couple of buddies to test it out for me, and they had some nice suggestions for improvement in the form of pull requests, which I have merged in (thanks Michael and Max!). But I think the post still holds up to show my process of Dockerizing this simple php app.

SNK809 Milspec Mod

From WWII on, the US government wrote specifications for wrist watches to be worn by pilots and soldiers. Great American companies like Hamilton, Elgin, Waltham, and Benrus made mechanical watches at scale for decades to be supplied to GIs.

The following image is from a military specification for a field watch from the early 60s:

https://i2.wp.com/wornandwound.com/library/uploads/2013/09/MilSpec-Dial.jpg?resize=388%2C378

Hamilton and others continue to make mechanical watches that follow this spec, but I don’t have the budget at the moment to buy one of those. So I decided to do a watch mod project aimed at producing a milspec watch.

The Seiko SKX is the watch with the most options in terms of available crystals, hands, bezels, etc. for modding, but at around $200 it’s pricey to get started on. Starting as low as $55, the SNK presents the cheapest way to get a decent mechanical with a fair number of mod options — so that’s where I decided to start.

Continue reading “SNK809 Milspec Mod”

Orient Flight

Since I got this watch a couple years ago, I’ve only grown to appreciate this watch more and more, and one aspect I love in particular is the legibility. This watch is so easy to read in bright light, low light, and anywhere in between. And in the dark, the lume is bright and lasts all night—it’s on the hands, minutes, and hour indices. At 42mm this watch is about as large as I want to wear on my wrist, and it’s very comfortable on a NATO.

Infrared Blaster

This project was about allowing me to turn the bedroom tv on and off via voice control. I already had an Amazon Echo Dot in the bedroom, so I had a way to capture intent. What I needed was a way to turn that intent into an infrared signal that the tv would receive as a power on/off command.

ESP8266/NodeMCU proves to be useful yet again as a cheap, low-power wifi module that can interface to analog devices, such as LED receiver and emitter.

I found this absolutely fantastic project on Github called ESP8266-HTTP-IR-Blaster which provides a ton of useful functionality, such as sending and receiving infrared signals, as well as a handy web interface for viewing codes and showing system info.

Since the software sketch was so full featured, this project would really be more of an exercise with getting the hardware working, and finished into a reasonably durable device.

The first step was to acquire the electronics, and get them working in a breadboard.

Continue reading “Infrared Blaster”