Installing PHP on a Jenkins server running from a Docker container involves creating a custom Docker image that includes both Jenkins and PHP. Here's a step-by-step guide to achieve this:
Create a Dockerfile that installs Jenkins and PHP. Here’s an example:
# Use the official Jenkins base image
FROM jenkins/jenkins:lts
# Switch to the root user to install PHP
USER root
# Update the package list and install PHP
RUN apt-get update && \
apt-get install -y php php-cli php-mbstring php-xml php-zip php-curl php-gd && \
apt-get clean
# Switch back to the Jenkins user
USER jenkins
Navigate to the directory containing your Dockerfile and build the image:
docker build -t jenkins-php .
Run a container from the custom image:
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins-php-server jenkins-php
After the container is running, you can verify that PHP is installed correctly.
- Access the Jenkins container:
docker exec -it jenkins-php-server bash
- Check the PHP version:
php -v
- Open Jenkins in your web browser (typically at
http://localhost:8080
). - Go to Manage Jenkins -> Global Tool Configuration.
- Scroll down to PHP and click Add PHP.
- Provide a name for your PHP installation and specify the path to the PHP executable (usually
/usr/bin/php
inside the Docker container). - Click Save.
- Click New Item to create a new job.
- Enter a name for the job, select Freestyle project, and click OK.
- In the job configuration:
- Source Code Management: Configure your source code repository (e.g., Git).
- Build: Click Add build step and select Execute shell.
- In the command box, enter the command to run your PHP script. For example:
php path/to/your-script.php
- Click Save.
- Go to the job page and click Build Now.
- Monitor the build process and check the console output to ensure that your PHP script is running correctly.
For more comprehensive PHP development, you can install additional tools like PHPUnit, PHP CodeSniffer, and PHP Mess Detector. Add the installation commands to your Dockerfile as needed. For example:
RUN apt-get update && \
apt-get install -y phpunit php-codesniffer phpmd && \
apt-get clean
- In the Jenkins job configuration, click Add build step and select Execute shell.
- Enter the command to run PHPUnit:
phpunit path/to/your-test-directory
By following these steps, you can set up a Jenkins server running in a Docker container with PHP installed, enabling you to automate your PHP project builds and tests efficiently.