This guide explains how to set up a local WordPress environment using Docker, retrieve the live site files and database via SSH, and restore the site locally.
- Docker installed on your machine.
- SSH access to your remote WordPress server.
-
Create a Project Directory:
mkdir wordpress-docker cd wordpress-docker -
Create Required Files:
Dockerfiledocker-compose.yml
-
Write the
Dockerfile:# Base image FROM wordpress:latest # Install additional PHP extensions if needed RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install gd # Set permissions (optional but useful) RUN chown -R www-data:www-data /var/www/html # Expose port 80 EXPOSE 80
-
Write the
docker-compose.yml:version: '3.8' services: wordpress: build: . container_name: wordpress ports: - "8000:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress_user WORDPRESS_DB_PASSWORD: wordpress_password WORDPRESS_DB_NAME: wordpress_db volumes: - ./wordpress:/var/www/html db: image: mysql:5.7 container_name: wordpress_db environment: MYSQL_ROOT_PASSWORD: root_password MYSQL_DATABASE: wordpress_db MYSQL_USER: wordpress_user MYSQL_PASSWORD: wordpress_password volumes: - db_data:/var/lib/mysql volumes: db_data:
-
Start Docker Containers:
docker-compose up -d
-
Verify the environment by visiting
http://localhost:8000.
-
Copy
wp-contentfolder:scp -r user@your-remote-server:/path/to/wordpress/wp-content ./wordpress/wp-content
Replace:
userwith your SSH username.your-remote-serverwith your server's domain or IP./path/to/wordpress/wp-contentwith the actual path to thewp-contentfolder on your remote server.
-
Copy Additional Files (if needed):
scp user@your-remote-server:/path/to/wordpress/wp-config.php ./wordpress/wp-config.php
-
SSH into the remote server:
ssh user@your-remote-server
-
Export the database using
mysqldump:mysqldump -u db_user -p db_name > wordpress-database-backup.sqlReplace:
db_userwith your database username.db_namewith your database name.
-
Copy the database backup to your local machine:
scp user@your-remote-server:/path/to/wordpress-database-backup.sql ./wordpress-database-backup.sql
Copy the wp-content folder and any additional files into the local project directory:
cp -r ./wordpress/wp-content/* ./wordpress/wp-content/-
Import the SQL file into the MySQL Docker container:
docker exec -i wordpress_db mysql -u wordpress_user -pwordpress_password wordpress_db < ./wordpress-database-backup.sql
-
Update Site URLs in the Database:
docker exec -it wordpress_db mysql -u wordpress_user -pwordpress_password wordpress_dbRun the following SQL commands:
UPDATE wp_options SET option_value = 'http://localhost:8000' WHERE option_name = 'siteurl'; UPDATE wp_options SET option_value = 'http://localhost:8000' WHERE option_name = 'home';
-
Exit MySQL:
exit
- Open
http://localhost:8000in your browser. - Log in to the WordPress admin dashboard.
- Fix Permalinks (if needed):
- Go to Settings > Permalinks in the dashboard.
- Save your preferred structure to regenerate the
.htaccessfile.
- Stop containers:
docker-compose down
- Restart containers:
docker-compose up -d
This guide covers all steps for setting up a local WordPress environment, retrieving files and the database from a remote server, and restoring them locally using Docker.