Skip to main content
Version: 1.45

Getting Started with Okteto and PHP

Okteto runs your PHP application in Kubernetes while you edit it on your machine: every time you save a file, the change runs in your cluster a few seconds later, and you can debug it with breakpoints from your IDE. The PHP Sample App, a hello world HTTP server, is configured so that Okteto builds, deploys, and tests it directly in your Kubernetes cluster. To do the same in your own repository, we recommend using an AI agent with the Okteto agent skills.

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:

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

The Dockerfile​

The Dockerfile of the PHP Sample App defines two images, one per stage:

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 ./
RUN 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 ./
CMD ["php", "-S", "0.0.0.0:8080"]
  • dev is the development image. It contains PHP with its development settings and its built-in web server for hot reload, Xdebug for debugging, Composer, the dependencies in composer.json (PHPUnit for the unit tests), and the source code. composer install runs before COPY . ., so a change to a .php file doesn't install the dependencies again.
  • prod is the application image. It contains PHP with its production settings and the .php files of the application, served by PHP's built-in web server on port 8080.

The dev stage follows the recommendations for development images: the versions of PHP, Xdebug, and Composer are pinned, and composer.lock pins the versions of the dependencies.

The xdebug.ini file configures Xdebug in the development image:

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, which okteto up forwards to the debugger of your IDE.

The Okteto Manifest​

The okteto.yml file is the Okteto Manifest of the PHP Sample App.

Build​

The build section builds both images:

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

target selects a stage of the Dockerfile. depends_on makes Okteto build the dev image first, so hello-world reuses its cached layers. Okteto pushes both images to the Okteto Registry and stores their references in the OKTETO_BUILD_DEV_IMAGE and OKTETO_BUILD_HELLO_WORLD_IMAGE variables.

Deploy​

The deploy section installs the Helm chart in the chart folder, with the application image that Okteto built:

okteto.yml
deploy:
image: alpine/helm:4.3.2
commands:
- helm upgrade --install hello-world chart --set image=${OKTETO_BUILD_HELLO_WORLD_IMAGE}

The command runs with Remote Execution, in a container inside your cluster that uses the image set in deploy.image, so you don't need Helm on your machine.

Deploying the PHP Sample App​

Run okteto deploy from the root folder of the PHP Sample App:

okteto deploy

Okteto builds the dev and hello-world images, runs the deploy command, and prints the endpoint of the application:

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 'helm upgrade --install hello-world chart --set image=${OKTETO_BUILD_HELLO_WORLD_IMAGE}'
Executing command 'helm upgrade --install hello-world chart --set image=${OKTETO_BUILD_HELLO_WORLD_IMAGE}'...
Release "hello-world" does not exist. Installing it now.
NAME: hello-world
NAMESPACE: cindy
STATUS: deployed
REVISION: 1
i Endpoints available:
- https://hello-world-cindy.okteto.example.com
✓ Development environment 'php-getting-started' successfully deployed

Open the endpoint in your browser to see the Hello World! response. The Okteto UI shows the same endpoint:

Okteto UI showing the cindy Namespace with the php-getting-started Dev Environment deployed, its hello-world Deployment running, and its endpoint

Okteto creates an HTTPS endpoint for the Ingress in the Helm chart.

Testing the PHP Sample App​

The test section of the Okteto Manifest defines a Test Container that runs the unit tests in tests/IndexTest.php:

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
  • image: the development image, so the tests run with the same PHP version and Composer as the Development Container.
  • caches: the Composer cache, kept between runs of the Test Container, so composer install copies the packages from it instead of downloading them.
  • commands: the commands that install the dependencies and run PHPUnit. XDEBUG_MODE=off turns off Xdebug, which otherwise tries to connect to a debugger on every PHP command. The Test Container fails if any of them exits with a non-zero code.
  • artifacts: the files that Okteto copies to your machine when the tests finish, in this case the JUnit report of PHPUnit.

Test Containers run with Remote Execution, in a container inside your cluster. The Test Container receives the files of the PHP Sample App in its own folder, not in the working directory of the development image, so it installs the dependencies with composer install before it runs PHPUnit.

Run the unit tests from the root folder of the PHP Sample App:

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
. 1 / 1 (100%)
Time: 00:00.001, Memory: 20.00 MB
OK (1 test, 1 assertion)
✓ 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 an AI agent writes and tests the code autonomously. With the Okteto agent skills, the agent runs this loop in its own Namespace and reports back the tests it ran, the endpoints of the application, and a link to that Namespace.

Developing in a Development Container​

If you use AI agents, the Development Container is optional: agents work well with the okteto deploy and okteto test cycle, and don't need one. A Development Container gives you the experience of developing on your machine, with hot reload and a debugger on every change. 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 hot-reload and debugging tools of the development image in your cluster.

While okteto up is running, an AI agent with the Okteto agent skills skips the build and deploy cycle: your changes already reach the Development Container through File Sync, so the agent waits for you to tell it what to run next.

Configuration​

The dev section of the okteto.yml file defines the Development Container of the PHP Sample App:

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 matches the name of the Deployment in the Helm chart. These are the fields of the Development Container:

  • image: the development image built in the build section.
  • command: starts PHP's built-in web server on port 8080. It reads the .php files on every request, so it serves every change without a restart.
  • sync: synchronizes the root folder of the PHP Sample App with /usr/src/app, the working directory of the development image.
  • persistentVolume: disables the persistent volume of the Development Container. Okteto synchronizes the files again on every okteto up, and the Composer dependencies come from the development image, so there's no volume to create and attach. In projects with many dependencies, enable it and add /root/.composer/cache to volumes to keep the Composer cache between okteto up sessions.
  • reverse: forwards port 9003 of the Development Container, where Xdebug connects, to port 9003 of your machine, where the debugger of your IDE listens.

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:

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

Open index.php in your IDE, change the response message on line 2 to Hello World from Okteto!, and save the file:

index.php
<?php
$message = "Hello World from Okteto!";
echo($message);

Okteto synchronizes index.php to the Development Container, and the web server runs the new code on the next request. Reload the endpoint in your browser to see the new message. The change runs in your cluster without building an image or redeploying the application.

Press Ctrl+C to stop the web server and exit okteto up.

Debugging with PhpStorm​

Xdebug runs in the same PHP process as the web server, so the Development Container that okteto up starts is ready to debug. On every request, Xdebug connects to port 9003 of the Development Container, and okteto up forwards that connection to port 9003 of your machine, the port where PhpStorm listens for Xdebug by default.

With okteto up running, open the PHP Sample App in PhpStorm, add a breakpoint on line 2 of index.php, and click Start Listening for PHP Debug Connections in the toolbar. Reload the endpoint in your browser. Execution stops at the breakpoint, 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 the files of your project to the files of the Development Container. Map the root folder of the PHP Sample App to /usr/src/app, the path 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. Ctrl+C stops the web server and exits okteto up.

Cleaning up​

okteto down stops the Development Container and brings back the hello-world Deployment with the application image:

okteto down

When you're done, okteto destroy uninstalls the Helm release of the PHP Sample App from your Namespace:

okteto destroy

FAQ​

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. An AI agent with the Okteto agent skills configures them for your application and measures how much faster your builds, deploys, and okteto up get. Optimize your Development Environments covers these and other optimizations.

How do I deploy an application with several services and a database?​

Follow Deploy Your Application to Okteto if you deploy with a Helm chart, or Develop a Docker Compose application with Okteto if you use Docker Compose. If your services live in several repositories, see Deploy a multi-repo application with dependencies.

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

No. Divert lets you deploy only the services you change and reuse the services of a shared environment.

How do I start each environment with realistic data?​

Create its database from a Volume Snapshot instead of running a seed script on every deploy. 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 cloud resources. Create cloud infrastructure with Terraform shows how, with an Amazon S3 bucket for each environment.

Where can I find every field of the Okteto Manifest?​

The Okteto Manifest reference lists every field of build, deploy, destroy, test, and dev.