Hosting WordPress on Docker
Creating a WordPress site using Docker is a streamlined and efficient way to manage your development environment. Docker ensures that your application runs consistently across different systems. Visual Studio Code (VS Code) is a powerful, lightweight code editor that can help streamline this process further. This guide will walk you through setting up a WordPress site using Docker and Visual Studio Code.
Prerequisites
Before starting, ensure you have the following installed on your machine:
Step 1: Set Up Project Directory
Create a directory for your WordPress project. This directory will contain all the files necessary for your Docker setup.
- Open Visual Studio Code.
- Open a terminal in VS Code (
Ctrl +
). - Create a new directory for your project and navigate into it:
mkdir wordpress
cd wordpress
Step 2: Create a docker-compose.yml File
Inside your project directory, create a docker-compose.yml
file. This file will define the services required for your WordPress site, including the WordPress application itself and a MySQL database.
- In Visual Studio Code, create a new file named
docker-compose.yml
. - Add the following content to the file:
services:
db:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql:delegated
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
UPLOAD_LIMIT: 500M
MEMORY_LIMIT: 1010M
MAX_EXECUTION_TIME: 100000
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content:delegated
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data: {}
This configuration sets up two services:
- db: A MySQL database with persistent storage.
- wordpress: The WordPress application, which depends on the database service.
Step 3: Start the Docker Containers
With your docker-compose.yml
file in place, you can start your WordPress site by running the following command in the terminal within Visual Studio Code:
docker-compose up -d
This command will download the necessary images and start the containers in detached mode. The WordPress site will be accessible at http://localhost:8000
.
Step 4: Complete the WordPress Installation
Open your web browser and navigate to http://localhost:8000
. You should see the WordPress installation screen. Follow the on-screen instructions to set up your site:
- Select your preferred language.
- Fill in the site information (Site Title, Username, Password, Email).
- Click “Install WordPress”.
Once the installation is complete, you can log in to your new WordPress site.
Stopping the Containers
If you need to stop the containers, you can do so with the following command in the terminal:
docker-compose down
This command will stop and remove the containers but preserve the data in the db_data
volume.
Restarting the Containers
To restart the containers, use the docker-compose up -d
command again. Your data and configuration will be intact.
Step 6: Customizing Your WordPress Site with Visual Studio Code
Adding Themes and Plugins
To add themes and plugins, navigate to the wordpress
directory within your project directory. You can place your themes in the wp-content/themes
directory and your plugins in the wp-content/plugins
directory. These changes will be reflected in your running WordPress site.
Editing Files
Visual Studio Code provides a rich set of tools for editing and managing files. You can open the WordPress directory in VS Code and customise your site by editing themes, plugins, and other WordPress files.
- Open the
wordpress
directory in VS Code. - Make the necessary changes to your theme or plugin files.
- Save your changes, and they will be reflected on your WordPress site at
http://localhost:8000
.
Hosting WordPress With Docker
Setting up a WordPress site using Docker provides a reliable and consistent environment for development and testing. Visual Studio Code streamlines your workflow with a powerful and flexible code editor. Following this guide, you can quickly spin up a WordPress site and easily manage it. Docker’s portability ensures that your WordPress site will run seamlessly across different systems, making development more efficient and hassle-free.