Josheli
  • Home
  • Blog
    • Knob
    • Running
    • Soccer
    • Technology
  • Projects
    • Overview
    • No Instagram
    • Google Photos WordPress Plugin
    • Plex Channels
    • Sh***y Game
    • Soccer In Colorado
    • Statrat
    • The Dot Game
    • Vox cPanel Hacks
    • WW Points Calculator
  • About
Knob , Technology

Dockerizing PHP apps and deploying to AWS Fargate Part 1

by dv February 6, 2019 4 Comments

For the day job, I maintain four custom PHP apps in addition to several Drupal sites. Up until a couples months ago, each of the apps was hosted on-premise in LAMP virtual machines that our IT department maintains. With PHP 5.6 end-of-life at the end of 2018, the IT department decided it didn’t have the resources to maintain and upgrade hardware, meaning they couldn’t support PHP apps internally anymore, so they offered up AWS as an alternative. All I had to do was update the apps to work on PHP 7, get the apps containerized and running on Docker, migrate the filesystem access to S3 buckets, migrate the databases to RDS, migrate email code to use SendGrid, and migrate authentication to use SAML. Yep, that’s all. I won’t go through the whole process, but maybe highlight some wins and pain-points.

One of the major benefits of Docker is running services in isolation. So it’s possible to have one container running PHP 5.6 and another PHP 7.2 and another PHP 7.1. Likewise with MySql or whatever other service, you don’t have to fret about running multiple versions, or versions different from production. In the past I used VMs, usually with Vagrant, or just installed a development environment directly on the host machine, but both of those options basically involve building and maintaining servers. I’ve done that enough to not want to do it anymore. With docker I can create a Dockerfile that says “FROM php:7.2-apache” plus a couple of other commands to copy code into the image, and I’m golden. In the end, I settled on using docker-compose to create three containers: app, database, and tests. Something like this:

version: '3.5'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app-1
    env_file: .env.dev
    image: app-1:latest

  db:
    build:
      context: .
      dockerfile: Dockerfile-mysql
    container_name: app-1-mysql
    environment:
      MYSQL_ROOT_PASSWORD: test
    image: app-1-mysql:latest

  tests:
    build:
      context: .
      dockerfile: Dockerfile-tests
    container_name: app-1-tests
    env_file: .env.dev
    image: app-1-tests:latest

networks:
  default:
    name: app-1-network

With this, I can run docker-compose build && docker-compose up, and I have a full development environment. You can also specify multiple docker-compose files, so if you want to do something only when developing, you can do something like docker-compose -f docker-compose.yml -f docker-compose.dev.yml up, where the docker-compose.dev.yml looks like this:

version: '3.5'

services:
  web:
    volumes:
      - ./src:/var/www/src/
      - ./public:/var/www/html/
    ports:
      - "8008:80"
  db:
    ports:
      - "3309:3306"

So now I have my code mounted, a web server accessible on the host at port 8008, and a database accessible on the host at port 3309, all with just a few text files.

So now that the apps are containerized, we need to get them running on PHP 7, and get them working with third-party resources. Running apps on containers in AWS Fargate means we don’t have native access to persistent storage. You can use something like EBS, but it seemed easier to just modify the apps to read and write to S3 buckets. The containers also don’t have an email server, so we need to write some code to use a third-party email service.

Next, we’ll need to deploy the apps to Amazon. We’ll use a CI/CD pipeline to that. Of course we also need to setup and manage the infrastructure on Amazon, i.e. Fargate, RDS, S3, VPCs, etc.

I’ll write about each of those in upcoming installments.

It's only fair to share...Share on facebook
Facebook
Share on twitter
Twitter
Share on email
Email

Related Content:

  • Migrating a Laravel application to Symfony by Dv April 15, 2019 When I started at the day job about 6.5 years ago, I inherited a jumble of custom PHP/MySql web applications.…
  • A New, Old Hobby: Self-hosted Services by Dv February 21, 2021 I've recently returned to a hobby of mine ... self-hosting various software services and web applications on a server I…
  • Stupidly Simple, Static, Startpage for Self-hosted Services by Dv February 21, 2021 I mentioned the other day that I've been self-hosting some software services on a server in my basement. Well, I…
  • Continuous Deployment: Drupal 8, Composer, Github, Pantheon… by Dv April 17, 2018 UPDATE: Watch a tech talk I recently gave at work about this process. I've been doing some pretty cool work…
  • Laravel: Simple Method for Modules by Dv March 1, 2017 In my day job, I maintain a fairly large-ish Laravel application. It started out as a few separate vanilla PHP…
  • Previous Sometimes I Snowboard4 years ago
  • Next Loading Files to a Docker Container on Startup4 years ago

4 Replys to “Dockerizing PHP apps and deploying to AWS Fargate Part 1”

  1. Sebastian Nievas says:
    March 29, 2019 at 12:49 am

    Hey, this is awesome! It’s exactly what I’m looking to do more or less. Looking forward to your next installments.

  2. dv says:
    April 15, 2019 at 10:06 pm

    Hey Sebastian, glad it could be of some use. I should have another installment out soon. Sorry for the delay.

  3. Zeeshan Arif says:
    July 29, 2019 at 11:41 pm

    Is it possible to set up cPanel on AWS Fargate?

  4. ema says:
    December 12, 2019 at 7:41 am

    Part 2?

Leave a Reply

Your email address will not be published. Required fields are marked *

Popular Posts

  • Josheli, What Happened? (28,499)
  • Install Ubuntu on HP Laptop with UEFI and new SSD Hard Drive (14,582)
  • Running a Plex Media Server on an Old Laptop (13,450)
  • Simple Google Photos: A WordPress Plugin (11,755)
  • More Janky Snowboarding Video (11,053)

Random Read

PHP, JSON, Strings, Integers, Mysqlnd and Matching Environments
Here's another instance of being wary when your development environment does not exactly match your…

Read More

Google Photo
Google Photo
Google Photo
Google Photo

Social Things

  • Family Photos
  • Juiskie’s Instagram
  • Scooter’s Facebook
  • Scooter’s Instagram
  • YouTube Videos
  • DV’s Github
  • Tweet Tweet

RSS From Familyvance

  • Snowshoeing at Brainard Lake
  • Fishing and Hiking at Golden Gate Canyon State Park
  • Rainbow Trout Fishing Report at Waneka Lake
  • Weightless Texas-Rig Plastic Worms at Sawhill Ponds and Coot Lake
  • Sawhill Ponds Fishing Report
2023 Josheli. Donna Theme powered by WordPress