Let’s dig into the process for building and deploying a simple PHP application to Hyper.sh.

Building a Dockerized PHP application

In order to demonstrate Hyper.sh’s hosting, we’ll need to build a simple PHP application. In a previous tutorial, I showed you how to run a basic PHP web application using Docker and PHP’s official Docker image. Here’s the PHP code we’ll use:

Pushing our application to Docker Hub

In order to deploy our application, we’ll need to push it to a container registry. Docker Hub is free for open source projects, and it works well with Hyper.sh, so we’ll start there.

First, create an account on Docker Hub. You’ll also need to log into your Docker Hub account on your local command line by running docker login and entering your credentials.

Next, we need to build the PHP project by creating a new Docker image. Add a Dockerfile to the root of the project:

FROM php:apache
COPY index.php /var/www/html/index.php

Once you’ve got the Dockerfile in the root directory with your index.php file, build an image from the command line using your Docker Hub username:

docker build .-t <YOUR_USERNAME>/tiny-php-app

Finally, push that image to Docker Hub:

docker push <YOUR_USERNAME>/tiny-php-app

If everything worked, you should see a bunch of hashes followed by stages of your build, and finally a long string with a size designation like this: latest: digest: sha256:035d2fb05c6545823383adf31fa4b4dc947f72f79f68320863051b9ab2b8eb4b size: 1993. That means that your Docker image was uploaded and is now available on Docker Hub.

Deploying to Hyper.sh

Obviously, you will need an account on Hyper.sh to host your project there, but you should also download the Hyper.sh command line application for this tutorial. Be sure to log into your Hyper.sh account and log into Docker Hub as well by running hyper login.

Once you’ve logged in via the command line, you can go ahead and run your site on Hyper.sh:

hyper run -d-p 80:80 <YOUR_USERNAME>/tiny-php-app

Hyper.sh will automatically pull the latest version of the image and start up a new container, leaving you with the ID in your command line. To ensure the container is running, run hyper ps to see a list of your running Hyper.sh containers.

While our container is running on Port 80, we haven’t assigned a public IP address to the container, so you won’t be able to access it from the web. In order to attach an IP address, create one in Hyper.sh:

hyper fip allocate 1

And then attach it to your container:

hyper fip attach <YOUR_IP> <YOUR_CONTAINER_ID>

Your tiny PHP application should now be hosted live on the web at <YOUR_IP> address. If you don’t see it, you can log into the running container on Hyper.sh just like you would a regular Docker container to debug any issues. Hyper’s CLI has most of the same commands as Docker’s CLI, so the interface should feel quite familiar.

Updating the Code

Finally, you may want to know how to update your application. First, make a change to the index.php file. Next, you will need to rebuild the image, push it to Docker Hub, pull it from Hyper.sh, and re-run the container. In short, you will need to run something like this in your command line: