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:
- Docker Installation:
Make sure Docker is installed on your machine. Download and install Docker from the official website: Docker. - GitHub CLI (Optional):
If you plan to use GitHub CLI, ensure it is installed on your system. Installation instructions can be found here. - SSH Key Setup (Optional):
If using GitHub CLI with SSH, ensure your SSH key is set up on your machine.
Setup Steps:
- Edit Hosts File:
Open the hosts file using the following command:sudo nano /etc/hosts
Add the line:127.0.0.1 m.local
- Create Project Folder:
Create a folder namedm.local
and adddocker-compose.yml
and.env
files. - Docker Compose File:
Createdocker-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
- 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
- Set Permissions:
Set permissions on thewordpress-data
folder:sudo chmod -R 777 ./wordpress-data
- 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. - 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);