Localhost WordPress on Docker with Git: setups for WordPress development.

Docker has transformed the way we manage applications, providing a consistent environment across systems. However, setting up Docker environments can be tricky, especially with permissions and integration of tools like Git. In this guide, we'll simplify the process for creating a local WordPress instance.

Prerequisites:

  1. Docker Installation:
    Make sure Docker is installed on your machine. Download and install Docker from the official website: Docker.
  2. GitHub CLI (Optional):
    If you plan to use GitHub CLI, ensure it is installed on your system. Installation instructions can be found here.
  3. SSH Key Setup (Optional):
    If using GitHub CLI with SSH, ensure your SSH key is set up on your machine.

Setup Steps:

  1. Edit Hosts File:
    Open the hosts file using the following command: sudo nano /etc/hosts
    Add the line: 127.0.0.1 m.local
  2. Create Project Folder:
    Create a folder named m.local and add docker-compose.yml and .env files.
  3. Docker Compose File:
    Create docker-compose.yml with the following content:
    version: '3.1'
    
    services:
      wordpress:
        image: wordpress
        restart: always
        ports:
          - 8085:80
        volumes:
          - ./wordpress-data:/var/www/html 
        env_file:
          - .env
          
      mysql:
        image: mysql:latest
        restart: always
        volumes:
          - ./mysql-data:/var/lib/mysql
        env_file:
          - .env
    
      adminer:
        image: adminer:latest
        ports:
          - "8084:8080"
        depends_on:
          - mysql
        
  4. Create .env File:
    Create .env with the following content:
    MYSQL_ROOT_PASSWORD =
    MYSQL_ALLOW_EMPTY_PASSWORD = true
    MYSQL_DATABASE = wp
    MYSQL_USER = wp
    MYSQL_PASSWORD = wp
    
    WORDPRESS_DB_HOST = mysql
    WORDPRESS_DB_USER = wp
    WORDPRESS_DB_PASSWORD = wp
    WORDPRESS_DB_NAME = wp
                    
  5. Set Permissions:
    Set permissions on the wordpress-data folder: sudo chmod -R 777 ./wordpress-data
  6. Clone Theme Repository:
    Clone the theme repository using GitHub CLI: gh repo clone [gitusername]/[gitrepo] ./wordpress-data/wp-content/themes/[themename]
    When prompted for authentication, use the SSH key password.
  7. Access Localhost:
    Access WordPress at: http://m.local:8085/

Useful Commands:

  • Add User to Docker Group (Important):
    sudo usermod -aG docker $USER
  • Start Docker Containers:
    docker-compose up -d
  • Stop Docker Containers:
    docker-compose down
  • Delete Project Folder:
    sudo rm -R m.local
  • Prune Docker Containers:
    docker container prune
  • Force Delete Lingering Dockers:
    docker rm -f $(docker ps -aq)

Troubleshooting:

If there are issues with WordPress updates, add the following to wp-config.php:
define('FS_METHOD', 'direct');
define('FS_CHMOD_DIR', 0755);
define('FS_CHMOD_FILE', 0644);

© essamsaad.com