phpgoogle-cloud-platformgoogle-cloud-workstations

Install PHP in home folder of Persistent Disk in Google Cloud Workstations


I am trying to install PHP on a Google Cloud Workstation, and have it persist after reboot. The problem I am having is that although I can get it installed in the default location, it does not persist after reboot. Here are the steps I tried:

First I updated and upgraded my packages with:

sudo apt-get update
sudo apt-get upgrade

Then I installed PHP afterwards:

sudo apt-get install php

Next I verified the location of the PHP executable using the command whereis php and received this output:

php: /usr/bin/php /usr/bin/php7.4 /usr/lib/php /etc/php /usr/share/php7.4-readline /usr/share/php7.4-opcache /usr/share/php7.4-json /usr/share/php7.4-common /usr/share/man/man1/php.1.gz

Finally, after confirming that PHP was installed, I was then able to debug my PHP application using the command php -S localhost:8080.

To help reduce costs, Cloud Workstations use an activity timeout that will automatically shutdown idle workstations. The problem arises when my Cloud Workstation is subsequently launched after being shutdown. The PHP installation is no longer present at the location where I previously installed it.

I suspect the reason why PHP is missing after reboot is because it was installed on the ephemeral Compute Engine VM where all runtime data is deleted when the workstation is stopped.

I though it might be possible to install PHP in the persistent home directory of the workstation mounted to the /home folder so that it will persist between sessions.

Is this the best strategy for installing PHP on Google Cloud Workstations? If so, how do I install PHP in the /home folder?


Solution

  • Since you are trying to persist a system package, the recommended method of doing so would be to customize a container image. If you build and push a modified workstations image to a container registry, you can then specify it as a workstations start image, which is an option in your workstation configuration.

    An example Dockerfile that installs PHP as you've described might look something like this:

    FROM us-central1-docker.pkg.dev/cloud-workstations-images/predefined/code-oss:latest
    
    RUN \
      sudo apt-get update && \
      sudo apt-get install -y php
    

    To build and test it locally:

    # Build the image locally, setting a custom tag of "my-custom-workstations image":
    docker build -t my-custom-workstations-image
    
    # Run the container locally, removing it after it exits:
    docker run --rm -it --entrypoint=bash my-custom-workstations-image:latest
    
    # Inside of the container, to verify that PHP was installed:
    root@fad45fb2d1c5:/# php --version
    PHP 7.4.30 (cli) (built: Jul  7 2022 15:51:43) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies
    

    Once pushed, to use the image in Cloud Workstations, you can either create a new configuration, or modify an existing one. The option to set the container image will look like so:

    enter image description here

    To see which options are available as base images for your custom image, see Preconfigured base images from the Cloud Workstations documentation.