Install jenkins on docker container for running php applications

Simplified Jenkins Setup for PHP Projects using Docker and Docker Compose

Install jenkins on docker container for running php applications
Install jenkins on docker container for running php applications

Setting Up Jenkins in Docker for Centos: Fetching from GitHub and Deploying to PHP Server

In this blog post, we'll explore how to deploy your code from a GitHub repository to a live PHP server using Jenkins running within a Docker container on a Centos machine. This setup offers portability, consistency, and automation for your deployment process.

Prerequisites:

  • Centos machine with Docker and Docker Compose installed
  • GitHub repository containing your code
  • Basic understanding of Docker and Jenkins

System Setup:

  1. Directory Permissions: Ensure the following directory permissions are set as mentioned:

    • /var/www/docker/jenkins: 0755 (apache:apache)
    • /var/www/html/php: 0755 (apache:apache)
  1. docker-compose.yml: Create a docker-compose.yml file with the following content:

YAML
version: '3.8'
services:
  jenkins:
    image: jenkins/jenkins:lts
    user: jenkins
    ports:
      - 8080:8080
    restart: always
    container_name: jenkins
    volumes:
      - /var/www/docker/jenkins:/var/jenkins_home
      - /var/www/html/php:/var/www/html/php # Mount PHP server directory
  1. Start Jenkins: Run docker-compose up -d to start the Jenkins container.

Jenkins Configuration:

  1. Access Jenkins Interface: Open your web browser and navigate to http://localhost:8080.
  1. Initial Setup: Follow the on-screen instructions to complete the initial Jenkins setup.
  1. Install Plugins: Install the necessary plugins, such as "Git" and "Publish Over SSH."
  1. Create Job:
    • Job Type: Freestyle Project
    • Git Repository: Configure the URL of your GitHub repository.
    • Build Steps: Add a "Shell Script" build step with the following content:
#!/bin/bash
remote_dir="/var/www/html/php/ommsoev2_live/"
cp -r "${WORKSPACE}"/* "$remote_dir"
  1. Configure Credentials: Add SSH credentials for authorized access to your live server.

Deployment Process:

  1. Trigger Build: Push code changes to your GitHub repository. This will automatically trigger a Jenkins build.
  1. Build Execution: Jenkins fetches changes from GitHub, executes the build script (copying files to the PHP server directory), and deploys the updated code to your live site.

Security Considerations:

  • Avoid using privileged mode in the Docker container unless absolutely necessary.
  • Store SSH credentials securely using Jenkins credential management.
  • Implement proper access control for Jenkins and the live server.

By utilizing Docker and Jenkins in this way, you can achieve an efficient and automated deployment pipeline for your Centos-based PHP application. Remember to adapt and enhance this setup based on your specific project requirements and security best practices.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow