How to deploy a PHP app using Docker

How to deploy a PHP app using Docker

Docker is a popular open-source technology for running applications in a containerized environment. This is useful because it allows you to easily scaffold and deploy applications to any platform that supports Docker. In this tutorial, we will learn how to build a PHP application that can be run in a Docker container. We will do this by building a Docker image and run it as a container. The web server we will be using for our PHP app is Apache. You can also optionally use Nginx for this if you'd like.

What is PHP?

Before we begin, we need to understand what PHP is. PHP is a programming language that is used to create dynamic web applications. PHP is a popular choice for web developers because it is easy to learn and is very flexible. PHP is a server-side language, meaning that it is written on the server and executed on the server. Running PHP in Docker is good idea because it allows you to easily deploy and run your PHP application on any platform that supports Docker.

Prerequisites

  1. Basic ability to use a command line interface

Installing Docker

You will also need to install Docker.

  1. Visit the official Docker website to get the installer.
  2. After it downloads, run the installer until the end.
  3. Restart your computer to ensure the changes can take effect.

The Docker installer.

Directory Structure

By the end of this tutorial, our application will be in the following directory structure:

BASH
app ├── src | └── index.php ├── .env ├── docker-compose.yml └── Dockerfile

As you can see, it's a simple application with a few files for the purposes of this tutorial.

PHP app

This tutorial assumes that you already have a PHP app that you want to deploy on a Docker container, however we will build a very minimal one from scratch.

Create a file named index.php in the src directory.

PHP
<?php phpinfo(); ?>

This file is just a simple PHP file that will print out the PHP version and some information about the server. This is how it will look once it is running.

The PHP app up and running.

Docker Overview

Before we continue, let's run through a quick overview of how Docker works. As said before, Docker is a containerization technology that makes it easy to create, deploy, and run applications inside containers.

A Docker container is a lightweight, portable, and isolated unit of software. It is a single process that runs on a single computer. It is designed to run a specific application, such as a web server, or a database.

A Docker image is a blueprint for a Docker container. It is a collection of files and commands that can be used to create a Docker container, and it is stored inside of a file called Dockerfile. The instructions in this file are used to create an image.

Docker overview

Docker Compose

Now that we have an app to deploy and the basic terms for Docker defined, let's deploy it on a Docker container. There are several ways to do this, but the easiest way is to use the Docker Compose tool. It comes with Docker, so you should already have access to it.

Create a file named docker-compose.yml in the root directory of your folder.

YML
version: "3.9" services: php: container_name: php image: php restart: always build: context: . dockerfile: Dockerfile target: base ports: - "${PORT}:80"

Let's breakdown what's going on here. This file tells Docker to create a service called php that runs the image php with container name php and exposes the port that we will later set to 8080. The actual instructions for building the image are in the Dockerfile file.

Dockerfile

Create a file named Dockerfile in the root directory of your folder.

DOCKERFILE
FROM php:8.0-apache as base COPY ./src /var/www/html

With only two lines here, it's straightforward.

DOCKERFILE
FROM php:8.0-apache as base

This tells Docker to get the latest version of the php:8.0-apache Docker image, which comes pre-installed with both PHP 8 and Apache, and name this image base.

DOCKERFILE
COPY ./src /var/www/html

This tells Docker to copy the contents inside src to the /var/www/html/ directory inside the container. This is important because this folder is the root where Apache will read from.

Env file

Before we can deploy our app on a Docker container, we need to set up the environment variables that we will need to run the app. Create a file named .env in the root directory of your folder.

Since we defined the port as a variable in the docker-compose.yml file, we need to set the port in the .env file.

BASH
PORT=8080

You can set whatever port you want, just keep in mind that this the port that you will use to access your app.

Running the app

Finally we are ready to deploy the app on a Docker container. To do so, use Docker Compose.

BASH
docker compose up --build

On the first build, you don't need the build flag, but you will in subsequent runs. If successful, you should see something like this:

The Docker Compose output.

The app is now built and accessible at http://localhost:8080/ just like before, however now the app is running in Docker.

The PHP app running on port 8080.

You can confirm the container is running in Docker, along with all of the other containers that are running by using the docker ps command.

BASH
docker ps
BASH
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ba804e8bff5f php "docker-entrypoint.s…" 5 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp php

You can stop the running container by then running docker compose down.

Keep in mind, as mentioned before, even though we are using Apache as our web server, you can still alternatively use Nginx instead.

Deploying to Production

Now that your PHP app has been containerized successfully, it is ready to be deployed to whatever environment you want. This tutorial showed how to deploy it on your own machine, however, this Docker container can be deployed on a staging environment, remain local, or be deployed to a production environment.

To deploy it to production, you will need some kind of server or hosting platform to host the app. There are several different options for hosting, each with their pros and cons. In addition, since your app is a web app, it will likely need a domain as well. Either way, containerizing your app using Docker is becoming the industry standard and it will certainly help you get your app accessible to your users.

Conclusion

Docker is a powerful tool for a developer to learn and use. As you saw, with just a few lines in your Dockerfile, you can set up a full-blown PHP app. This makes deploying the app anywhere you want a breeze. I hope this tutorial has helped you get a better understanding of Docker and Docker Compose. Happy deploying!

Docker's hot for a reason though!

Resources

  1. Docker
  2. Docker Compose docs
  3. PHP
  4. Official PHP Docker Image
  5. Apache
  6. Nginx
Recommended Tutorial »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.