Skip to main content
Version: 1.50

Okteto with PHP

This guide takes the PHP Sample App through the whole Okteto workflow, in an application environment in your Kubernetes cluster. The PHP Sample App is a hello world service. Its page shows a message, the PHP version, and the pod that serves it.

The sample is small, but its configuration is optimized, so you can use it as a reference for your own PHP application. Along the way, the guide points out what makes each step fast in a PHP project: the cache mount that the build and the tests share, the ignore files that keep every upload small (.dockerignore, .oktetoignore, and .stignore), and PHP's built-in web server and Xdebug for hot reload and debugging from PhpStorm.

Prerequisites​

  • The Okteto CLI, installed and configured to access your Okteto instance. See the installation guide.
  • To debug the application: PhpStorm.

Clone the PHP Sample App and move into its folder. Every command in this guide runs from there:

git clone https://github.com/okteto/php-getting-started
cd php-getting-started

Build​

The PHP Sample App uses two container images. dev is the image you develop in: it has PHP with its development settings and the tools to test and debug the application. hello-world is the production image: a small image with nothing but PHP and the application's files. The build section of the okteto.yml file defines both:

okteto.yml
build:
dev:
context: .
target: dev
hello-world:
context: .
target: prod
depends_on:
- dev

Both images come from the same Dockerfile. target picks the stage each image builds, and depends_on tells Okteto to build dev first:

Dockerfile
FROM php:8.5.12-cli-trixie AS dev

RUN apt-get update && \
apt-get install -y --no-install-recommends unzip && \
rm -rf /var/lib/apt/lists/* && \
pecl install xdebug-3.5.3 && \
docker-php-ext-enable xdebug && \
cp "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
COPY --from=composer:2.11.2 /usr/bin/composer /usr/local/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
COPY xdebug.ini $PHP_INI_DIR/conf.d/

WORKDIR /usr/src/app
COPY composer.json composer.lock ./
# The cache mount keeps Composer's download cache in the Okteto Build Service,
# which reuses it between builds. The tests of the Okteto Manifest use the
# same cache
RUN --mount=type=cache,target=/root/.composer/cache \
composer install
COPY . .

FROM php:8.5.12-cli-alpine3.24 AS prod

RUN cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
WORKDIR /usr/src/app
COPY *.php ./
COPY static static
CMD ["php", "-S", "0.0.0.0:8080"]

The Dockerfile installs the tools, Xdebug for debugging and Composer with the dependencies in composer.json (PHPUnit for the unit tests), before COPY . ., so a code change doesn't install them again. There's no hot reload tool to install: PHP has no compile step, and its built-in web server reads the files again on every request.

The RUN instruction that runs composer install mounts Composer's download cache (/root/.composer/cache) as a cache mount, which the Okteto Build Service keeps between builds. When a change to composer.json or composer.lock runs composer install again, Composer downloads only the packages that changed and doesn't download any package twice.

The build context makes the biggest difference, here and in any application. Every okteto build starts by uploading the Dockerfile's folder to the Okteto Build Service, and the .dockerignore file decides what goes in. The PHP Sample App's .dockerignore excludes everything and then allows only what the Dockerfile copies: the PHP files, composer.json and composer.lock, the static folder with the page, and xdebug.ini. Its last line leaves out the vendor folder, whose PHP files would otherwise go in.

.dockerignore
*
!composer.json
!composer.lock
!**/*.php
!static
!xdebug.ini
vendor/**

The upload stays small, and a change to any other file, such as the README or k8s.yml, doesn't rebuild the images.

Deploy​

okteto deploy first builds the images that aren't built yet. Okteto pushes them to the Okteto Registry and stores their references in the OKTETO_BUILD_DEV_IMAGE and OKTETO_BUILD_HELLO_WORLD_IMAGE variables.

The PHP Sample App deploys with a plain Kubernetes manifest. Its deploy section has a single command, which applies k8s.yml with kubectl:

okteto.yml
deploy:
- envsubst < k8s.yml | kubectl apply -f -

In k8s.yml, the Deployment's image is the OKTETO_BUILD_HELLO_WORLD_IMAGE variable. envsubst replaces it with the image Okteto built, and then kubectl applies the file.

The deploy has its own ignore file. okteto deploy uploads your folder to the container that runs the command, and the .oktetoignore file decides what goes in. Its deploy section excludes everything except k8s.yml, the only file the command reads, so there's almost nothing to upload. Its destroy section does the same for okteto destroy:

.oktetoignore
[deploy]
*
!k8s.yml

[destroy]
*
!k8s.yml

To deploy the application, run okteto deploy:

okteto deploy

Okteto builds the two images, runs the deploy command, and prints the application's endpoint:

i Using cindy @ okteto.example.com as context
✓ Image 'registry.okteto.example.com/cindy/php-getting-started-dev:okteto' successfully pushed
✓ Image 'registry.okteto.example.com/cindy/php-getting-started-hello-world:okteto' successfully pushed
i Running stage 'envsubst < k8s.yml | kubectl apply -f -'
Executing command 'envsubst < k8s.yml | kubectl apply -f -'...
deployment.apps/hello-world created
service/hello-world created
Command 'envsubst < k8s.yml | kubectl apply -f -' successfully executed
i Endpoints available:
- https://hello-world-cindy.okteto.example.com
✓ Development environment 'php-getting-started' successfully deployed

Open the endpoint in your browser. The page shows the application's message, the PHP version it runs on, and the pod that served the request:

The page of the PHP Sample App: Hello World, the Okteto logo, the message Built, deployed, and tested on Kubernetes, the PHP 8.5.12 runtime, and the name of the hello-world pod

Test​

The PHP Sample App has unit tests in tests/IndexTest.php. The test section of the Okteto Manifest defines a test, unit, that runs them:

okteto.yml
test:
unit:
image: ${OKTETO_BUILD_DEV_IMAGE}
caches:
- /root/.composer/cache
commands:
- XDEBUG_MODE=off composer install
- XDEBUG_MODE=off vendor/bin/phpunit --log-junit junit.xml tests
artifacts:
- junit.xml

The test runs with Remote Execution, in a container inside your cluster. The container starts from the development image, so it has the same PHP version and Composer as the Development Container. If any of the commands exits with a non-zero code, the test fails. When the test finishes, Okteto copies its artifacts, here PHPUnit's JUnit report, to your machine.

The test gets the PHP Sample App's files in a folder of its own, not in the development image's working directory, so it runs composer install before PHPUnit. XDEBUG_MODE=off turns off Xdebug, which otherwise tries to connect to a debugger on every PHP command.

The caches entry is the same folder that the Dockerfile mounts, and it's the same cache too: Okteto identifies a cache by its repository, context, and path, and the test and the build share all three. So composer install starts with the packages the build downloaded, and installs them without downloading anything.

The tests have their own section in the .oktetoignore file, so okteto test uploads only what Composer and PHPUnit read:

.oktetoignore
[test]
*
!composer.json
!composer.lock
!**/*.php
!static
vendor/**

Run the unit tests with okteto test:

okteto test
i Using cindy @ okteto.example.com as context
i Images were already built. To rebuild your images run 'okteto build'
i Executing test container 'unit'
i Running 'XDEBUG_MODE=off composer install'
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 26 installs, 0 updates, 0 removals
Generating autoload files
✓ Command 'XDEBUG_MODE=off composer install' successfully executed
i Running 'XDEBUG_MODE=off vendor/bin/phpunit --log-junit junit.xml tests'
PHPUnit 13.4.7 by Sebastian Bergmann and contributors.
Runtime: PHP 8.5.12
.. 2 / 2 (100%)
Time: 00:00.001, Memory: 20.00 MB
OK (2 tests, 3 assertions)
✓ Command 'XDEBUG_MODE=off vendor/bin/phpunit --log-junit junit.xml tests' successfully executed
✓ Test container 'unit' passed

The JUnit report is now in junit.xml, in the root folder of the PHP Sample App on your machine. CI systems and IDEs read this format to show the result of each test.

Running okteto deploy and okteto test after each change is usually fast enough, especially when a coding agent writes and tests the code on its own. With the Okteto skills, the agent runs this loop in its own Namespace and reports back the tests it ran, the application's endpoints, and a link to that Namespace.

Develop​

A Development Container feels like developing on your machine: every change you save reloads the application, and you can attach a debugger. When you run okteto up, Okteto replaces the hello-world Deployment with the Development Container, keeps your local files synchronized with it, and runs the development image's hot reload and debugging tools in your cluster. This step is optional: coding agents work well with the okteto deploy and okteto test loop and don't need a Development Container.

While okteto up is running, a coding agent with the Okteto skills stops building, deploying, and testing, and only edits the code: File Sync copies each change to the Development Container, and hot reload applies it to the running application.

The dev Section​

The dev section of the Okteto Manifest defines the PHP Sample App's Development Container:

okteto.yml
dev:
hello-world:
image: ${OKTETO_BUILD_DEV_IMAGE}
command: php -S 0.0.0.0:8080
sync:
- .:/usr/src/app
persistentVolume:
enabled: false
reverse:
- 9003:9003

The hello-world key is the name of the Deployment in k8s.yml that the Development Container replaces. This is what each field does:

  • image: the development image from the build section.
  • command: starts PHP's built-in web server on port 8080. PHP has no compile step, and the web server reads the .php files again on every request, so it serves every change without a rebuild or a restart.
  • sync: synchronizes the PHP Sample App's root folder with /usr/src/app, the development image's working directory.
  • persistentVolume: disables the Development Container's persistent volume, so there's no volume to create and attach. Okteto synchronizes every file on each okteto up, and the web server starts right away: there's nothing to compile, and the dependencies in the vendor folder come from the development image. Composer's download cache starts empty, because the build's cache mount isn't part of the development image. For a larger project, where you install packages in the Development Container, keep its Composer cache in a persistent volume. Caches in Development Containers explains how.
  • reverse: forwards port 9003 of the Development Container, where Xdebug connects, to port 9003 on your machine, where your IDE's debugger listens.

The third ignore file is .stignore, for File Sync. okteto up synchronizes your folder with the Development Container in both directions, and this file leaves out what shouldn't travel. That's the Git metadata, which is large and only matters on your machine, and the files that Composer and PHPUnit generate inside the Development Container, which would otherwise come back to your machine:

.stignore
.git
/vendor
/junit.xml
.vscode
.idea
.DS_Store

Hot Reload​

Run okteto up to replace the hello-world Deployment with the Development Container:

okteto up

Okteto synchronizes your files and starts PHP's built-in web server, which logs every request it serves:

i Using cindy @ okteto.example.com as context
i 'php-getting-started' was already deployed. To redeploy run 'okteto deploy' or 'okteto up --deploy'
✓ Images successfully pulled
✓ Files synchronized
Context: okteto.example.com
Namespace: cindy
Name: hello-world
Reverse: 9003 <- 9003

[Thu Nov 19 03:34:25 2026] PHP 8.5.12 Development Server (http://0.0.0.0:8080) started
[Thu Nov 19 03:34:26 2026] 10.0.0.12:54744 Accepted
[Thu Nov 19 03:34:26 2026] 10.0.0.12:54744 [200]: GET /api/info
[Thu Nov 19 03:34:26 2026] 10.0.0.12:54744 Closing

Now change the code. Open index.php in your IDE, edit the message on line 4, and save the file:

index.php
const MESSAGE = "Built, deployed, and tested on Okteto.";

Okteto synchronizes index.php to the Development Container. PHP has no compile step and nothing to restart: the web server reads the file again on the next request.

A moment later, the page in your browser shows the new message. You don't even have to reload it, because the page asks the service for its info every second. Your change is running in your cluster, and you didn't build an image or redeploy the application.

When you're done, press Ctrl+C to stop the web server and exit okteto up.

Debugging with PhpStorm​

To debug the application, you don't need another command: Xdebug runs in the same PHP process as the web server, so the Development Container that okteto up starts is ready to debug. The development image's xdebug.ini file configures it:

xdebug.ini
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003

Xdebug starts a debugging session on every request and connects to port 9003 of the Development Container. That's the port okteto up forwards to your machine, where PhpStorm listens for Xdebug by default.

Run okteto up if it isn't running. Then open the PHP Sample App in PhpStorm and click Start Listening for PHP Debug Connections in the toolbar. Now add a breakpoint on line 15 of index.php, the first line of the hello function. The page in your browser asks the service for its info every second, so execution stops at the breakpoint right away, and you can inspect the request, the $_SERVER variable, and the other variables.

The first time Xdebug connects, PhpStorm asks you to accept the incoming connection and to map your project's files to the files in the Development Container. Map the PHP Sample App's root folder to /usr/src/app, the same path as in the sync field of the Okteto Manifest. If you synchronize your code to another path, map it to that path.

When PhpStorm isn't listening, Xdebug can't connect, and the web server answers the request without stopping. To end the debugging session, click Stop Listening for PHP Debug Connections. The Development Container keeps running with hot reload.

Stopping the Development Container​

When you finish developing, run okteto down. It stops the Development Container and brings back the hello-world Deployment with the production image:

okteto down
i Using cindy @ okteto.example.com as context
✓ Development container 'hello-world' deactivated

Destroy​

To delete the application, run okteto destroy. It removes the PHP Sample App's Deployment and Service from your Namespace:

okteto destroy
i Using cindy @ okteto.example.com as context
i Images were already built. To rebuild your images run 'okteto build'
✓ Development environment 'php-getting-started' successfully destroyed

FAQ​

How do I set up my own repository the same way?​

Use a coding agent with the Okteto skills. Ask it to set up your repository for Okteto: it discovers your services, writes the Okteto Manifest, and validates it with okteto build and okteto deploy.

How do I make builds, deploys, and okteto up faster?​

Upload only the files that each command needs: the context upload is key to performance. Every okteto build, okteto deploy, okteto test, and okteto up uploads files from your machine, and three ignore files decide what gets uploaded: .dockerignore for the image builds, .oktetoignore for okteto deploy, okteto test, and okteto destroy, and .stignore for File Sync. A coding agent with the Okteto skills configures them for your application and measures how much faster your builds, deploys, and okteto up get. Optimize Your Application Environment covers these and other optimizations.

Do I need to deploy the whole application in every Namespace?​

No. Okteto supports two models, and you choose one for your application:

  • Full replica: every service of the application runs in your Namespace. In a monorepo, the deploy section of the Okteto Manifest deploys every service, as in the quickstart, where one Helm chart deploys the three services of Kube Movies. In a multirepo, each repository has its own Okteto Manifest and lists the other repositories in its dependencies section, as in Deploy a Multi-Repo Application with Dependencies. Full replicas are simpler to configure and give you full isolation: nothing one environment does affects the others. Use them for an application with fewer than 20 services.
  • Overlay Namespace: your Namespace runs only the services you change, and Divert routes every other request to a Shared Namespace that runs the rest of the application. Overlay Namespaces are more cost-effective, faster to deploy, and more reliable, because each environment builds, deploys, and runs only the services it changes. Use them for an application with 20 services or more.

How do I create a Preview Environment of my application for every pull request?​

Add a workflow to your CI pipeline that deploys the application in a Preview Environment when a pull request opens or changes, runs okteto test against it, and destroys it when the pull request closes. The workflow uses the Okteto Manifest, so okteto build, okteto deploy, and okteto test work the same way in your CI pipeline, for coding agents, and on developer machines. Follow the guide for GitHub Actions or for GitLab CI/CD.

How do I start each environment with realistic data?​

Load the data with a script in your deploy commands, or copy the data of another application environment with a Volume Snapshot, which creates the volumes of your database from a snapshot of its volumes. Data from Volume Snapshots shows how.

How do I create resources outside Kubernetes, such as a cloud database?​

Create them in the deploy and destroy commands with your infrastructure as code tool, so every environment gets its own resources. See the Create Cloud Infrastructure with Terraform tutorial, which creates an Amazon S3 bucket for each environment.

Can I access my application environment with kubectl?​

Yes. Run okteto kubeconfig to add the credentials of your Okteto Namespaces to your kubeconfig file, and use kubectl as usual: kubectl get pods, kubectl logs, or kubectl describe. See Kubernetes Credentials for how Okteto generates those credentials.