How to Install WordPress on Docker (Windows, macOS, and Linux)
Setting up an isolated environment for developing WordPress websites can initially seem challenging. Luckily, containerization tools like Docker exist, which help to streamline the development, testing, and deployment processes.
This tutorial will show you how to install and deploy a local WordPress site on a Docker container. Additionally, we will touch on the best security and development practices for WordPress Docker containers.
Download all in one WordPress cheat sheet
What Is Docker?
Docker is an open-source containerization software that creates isolated environments to run various applications. Users can develop, test, and run multiple applications on the same physical and virtual servers.
Unlike virtual machines, each container does not require its own OS as it shares the host kernel. Thus, the machine’s workload is much more lightweight, and such a server can run multiple containers simultaneously without losing performance.
For example, Docker is highly useful for WordPress developers. A WordPress test environment usually uses up a lot of system resources, while Docker allows developers to make a minimal environment without wasting server space and memory.
How to Deploy WordPress Image as a Docker Container
The following steps will show you how to install a WordPress content management system on a Docker container.
1. Install Docker
Docker is available for Windows, macOS, and Ubuntu. Here’s how you can install it on any of the three operating systems:
How to Install Docker on Ubuntu
In order to install Docker on a Linux VPS, you need to have a virtual private server (VPS) with Ubuntu running as an operating system.
Now, just follow the steps as shown:
- Update the package list:
sudo apt-get update
- Install the required packages:
sudo apt-get install ca-certificates curl gnupg lsb-release
- Create a directory for the Docker GPG key:
sudo mkdir -p /etc/apt/keyrings
- Add Docker’s GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- Set up the repository:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Update Docker’s repository:
sudo apt-get update
- Lastly, install the latest version of Docker Engine, containerd, and Docker Compose.
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- To confirm that the installation process was successful, run the following command. The following success message should appear:
sudo docker run hello-world
Important! Other Linux distributions—such as CentOS, Debian, or Fedora—have different installations steps. If you don’t use Ubuntu, see Docker’s official documentation page.
Alternatively, consider using Hostinger’s VPS as your Docker host. It offers a pre-made template to speed up the setup process – Ubuntu 22.04 64bit with Docker. If you have Hostinger VPS already, you can install Docker with a few clicks:
Then navigate to the Operating System section:
Hit the Applications button and select a template with Docker:
Lastly, log in via SSH and proceed with the setup.
How to Install Docker on macOS
In order to install Docker on a macOS machine, these requirements must be met:
- 4 GB of RAM
- macOS version 10.15 or newer
- No previous versions of VirtualBox 4.3.30 can be installed
Here’s how you can install Docker on macOS:
- Download Docker for Mac and double-click the .dmg file you’ve saved. Then, drag and drop the Docker icon into your Applications folder.
You can find download links here:
- Open your Applications folder and double-click docker.app. During the configuration process, you’ll be asked to enter your password.
- When prompted, Accept the service agreement; otherwise, the installation will fail.
- Once the installation process is finished, you should see the Docker menu on your desktop’s status bar.
How to Install Docker on Windows
In order to install Docker Desktop on a Windows machine, these requirements must be met:
- 4 GB of RAM
- 64-bit processor from 2010 or more recent
- Virtualization enabled in BIOS
- Linux kernel update package installed if you are using the WSL 2 Docker back-end
Here’s how you can install Docker on Windows 10 64-bit:
- Enable Hyper-V on your system.
- Download Docker Desktop for Windows and open the Docker for Windows Installer file.
- In the Configuration dialog window, check the boxes based on your preferences. Click Ok.
- Once the installation is finished, click Close and restart and wait for your computer to reboot.
- After reboot, Accept the service agreement, and Docker will be ready to use.
2. Set Up a WordPress Container on Docker
In order to set up WordPress on Docker, two methods are available ‒ the CLI and Docker compose. In this tutorial, we will use the Docker compose method as it’s more straightforward and systematic.
It’s worth noting that all required images are acquired from Docker Hub:
- WordPress – the official WordPress Docker image. Includes all WordPress files, Apache server, and PHP.
- MySQL – required for MySQL root user, password, and database connection variables.
- phpMyAdmin – a web application for managing databases.
- Open your operating system’s preferred command line interface and check the Docker Compose Installation version:
docker compose version
This will confirm that the Compose module is working correctly.
- Create a new project directory for WordPress application with the following command:
mkdir wordpress
- Navigate to the new directory:
cd wordpress
- Using your preferred text editor, create a new docker-compose.yml file, and paste the contents below:
version: "3" # Defines which compose version to use services: # Services line define which Docker images to run. In this case, it will be MySQL server and WordPress image. db: image: mysql:5.7 # image: mysql:5.7 indicates the MySQL database container image from Docker Hub used in this installation. restart: always environment: MYSQL_ROOT_PASSWORD: MyR00tMySQLPa$$5w0rD MYSQL_DATABASE: MyWordPressDatabaseName MYSQL_USER: MyWordPressUser MYSQL_PASSWORD: Pa$$5w0rD # Previous four lines define the main variables needed for the MySQL container to work: database, database username, database user password, and the MySQL root password. wordpress: depends_on: - db image: wordpress:latest restart: always # Restart line controls the restart mode, meaning if the container stops running for any reason, it will restart the process immediately. ports: - "8000:80" # The previous line defines the port that the WordPress container will use. After successful installation, the full path will look like this: http://localhost:8000 environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: MyWordPressUser WORDPRESS_DB_PASSWORD: Pa$$5w0rD WORDPRESS_DB_NAME: MyWordPressDatabaseName # Similar to MySQL image variables, the last four lines define the main variables needed for the WordPress container to work properly with the MySQL container. volumes: ["./:/var/www/html"] volumes: mysql: {}
- With the Docker Compose file created, run the following command in the same wordpress directory to create and start the containers:
docker compose up -d
3. Complete the WordPress Installation on a Web Browser
Open your browser and enter http://localhost:8000/
. WordPress setup screen will appear. Select the preferred language and continue.
Important! Ensure you are not running any other content management system or service on the same 8000 port. Otherwise, it won’t work properly.
Fill in your site name, username, password, and email.
When a Success! message pops-up, log in using your newly created details.
Lastly, you’ll be presented with the main WordPress dashboard screen.
Setting up phpMyAdmin
phpMyAdmin is a great tool for viewing and managing any existing databases. All you need to do is include these lines to an existing .yml file just after the services line along with the MySQL database service:
version: "3" services: db: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: MyR00tMySQLPa$$5w0rD MYSQL_DATABASE: MyWordPressDatabaseName MYSQL_USER: MyWordPressUser MYSQL_PASSWORD: Pa$$5w0rD phpmyadmin: image: phpmyadmin/phpmyadmin:latest restart: always environment: PMA_HOST: db PMA_USER: MyWordPressUser PMA_PASSWORD: Pa$$5w0rD ports: - "8080:80"
Save the file and run the docker-compose Docker command:
docker compose up -d
Once done, open http://localhost:8080/, and you’ll be able to see the phpMyAdmin interface along with your WordPress database.
Pro Tip
We recommend double-checking for images that you no longer use, then removing Docker images and other unnecessary files.
How to Secure WordPress Installation With Docker Secrets
Sensitive data such as passwords, SSH keys, and other types of critical information should be treated with extra care. That is where Docker secrets come in. Users can use Docker secrets to manage sensitive data and securely transmit it to particular containers that need access to it only.
In this tutorial, we will use Docker secrets to mask our WORDPRESS_DB_PASSWORD variable. WordPress will get the database password from a secret file we shall provide ourselves. Here’s an example:
wordpress: depends_on: - db image: wordpress:latest restart: always ports: - "8000:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: MyWordPressUser WORDPRESS_DB_PASSWORD_FILE: /run/secrets/wordpress_password WORDPRESS_DB_NAME: MyWordPressDatabaseName secrets: - wordpress_password secrets: wordpress_password: file: ./wordpress_password.txt
As you can see, the WordPress database password is taken from a wordpress_password.txt file that we created in the same main directory.
Website Development on WordPress Docker Container
Docker also serves as an excellent development tool. It allows developers to run Docker Compose to quickly use WordPress instances in an isolated environment built with Docker containers.
For example, suppose a developer wants to test out a plugin or theme on some specific WordPress version. In that case, they can just edit the main YAML file to include the WordPress version they specifically need and test everything there.
Finding and manipulating files is also very straightforward. Whenever a user pulls an official WordPress image via Docker, it creates all the necessary files and folders, such as wp-content, wp-admin, and wp-includes. Thus, the whole development environment acts the same as a live WordPress website.
Docker also makes the process of sharing the development builds with your team simple and convenient, as all you need to do is create your own registry. Then the whole team will be able to share images with docker pull and docker push commands.
Sugested Reading
Discover our guide to learn how to install WordPress on Ubuntu using LAMP Stack.
Conclusion
Docker is an excellent containerization tool to help streamline the development process for content management systems like WordPress. Its minimal environment enables you to maintain the efficiency of your system resources.
In this tutorial, we’ve learned how to install Docker on Ubuntu, macOS, and Windows and deploy a WordPress container for each of these operating systems. We’ve also covered the best WordPress security practices with Docker secrets and showed how to deploy your website from a Docker container to a live production server.
We hope this tutorial was helpful. If you have any further questions, don’t hesitate to share them in the comments section.
Comments
June 29 2017
Great tutorial. Thanks for putting this out there. Once someone is finished developing their wordpress based site, do you provide the container to a host or export the wordpress and associated database in the traditional sense? Maybe I'm picturing this inaccurately, but I'm assuming you shove the docker container into a slot on some docker compliant host and it just runs from there. Any guidance appreciated! Thanks.
September 10 2019
Great tutorial. Really helped me getting my first babysteps towards Docker. Two things that aren't mentioned and took some time for me to figure out, since newer versions are out. I got an error when I tried to run "docker-compose up -d" >> ERROR: client version 1.22 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version This one is very simple to solve: Open the yml file and change the version from 2 to 2.1 >> Pulling from library/php no matching manifest for windows/amd64 in the manifest list entries Also very simple to solve: 1 Right click Docker instance 2 Go to Settings 3 Daemon 4 Advanced 5 Set the "experimental": true 6 Restart Docker
September 24 2019
Hey Kenji, Thanks for your input!
November 22 2019
just followed this tutorial on my Mac (10.15) and I cannot believe how simple this is. It just works! Great!!
April 24 2021
Hey, Domantas G. Thanks to you I am able to set up WordPress using docker. thanks a lot
September 14 2021
If you're running this on Linux you have use "sudo docker-compose up-d" or you will receive an error message about http+docker://localhost not runnning.
December 19 2021
Excellent tutorial, I was stuck at a place as I was working with docker for one of my clients, and this article completely helped me in satisfying the cleint. Thank you so much Domantas, Kudos!
December 22 2021
Happy it worked out!
January 02 2022
Thanks, this worked amazingly well, and quickly! One thing though, I don't know where the active `wp_content` folder is. How do I develop themes and plugins in this?
January 04 2022
Hi Mike, generally your wp-content will be right next to wp-admin and wp-includes folder. You can use
find
command on Linux or Mac to find the location of your folder! Here's how to use the find command :)March 26 2022
pleace i have "Error establishing a database connection"
March 29 2022
Hi there! It looks like you might have a problem with either of these things: (1) you haven't created a database yet; (2) your database credentials on your wp-config.php file are incorrect; (3) you're reaching the memory limit of your server. I'd suggest to start by checking them :)
March 30 2022
thanks
June 22 2022
Same here. I see the comment below saying that I haven't created a database. How do you do this? It might be helpful to include those steps in the instructions
July 01 2022
Hey there! Thanks for the feedback! You can setup a database inside Docker by checking out this resource! ?
August 21 2022
Where can the WordPress files be found so that I can customize themes and plugins? Since I have numerous wordpress installs on this computer it is not a simple search. Thanks!
August 25 2022
Hey! The installation folder of WordPress will be located in your website's public_html folder. You can access it through a File Manager or FTP client depending on where your websites are hosted ?
May 06 2023
I spent several hours debugging issues on this because WordPress would properly escape the special characters in the passwords but my database client and the mysql command line did not do this automatically. I ended up changing the password to "password" and it worked for me. However Docker also keeps the original username and password unless you delete the volume used. the combination of WordPress not having issues and not knowing that the volume had the old username and password caused a lot of grief. hopefully this comment can help someone avoid that in the future.
May 12 2023
Hello! Thank you for sharing Brandon.
September 05 2024
Followed this guide and it just errors everywhere. If you try to change the default settings this isn't liked. If you try and set up phpMA, it just gives a connection error. phpMA error always states the below (it doesn't even matter what you set the credentials to be) ``` phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server. ``` This guide needs to be updated with working configurations. Time to find a guide that actually works and doesn't error everywhere. Shame because this has been the most concise guide I have seen so far
September 20 2024
Hello! Thanks for your feedback and for taking the time to point this out. We’ll be reviewing the tutorial to ensure that it reflects working configurations ;)