Updated on 24.5.2023

How to install WordPress with Docker

WordPress is one of the most popular content management software (CMS) due to its multitude of features and ease of use. However, setting up a new web host environment can be time-consuming especially if you need to do it often. Simplifying the installation process to a few fast commands greatly reduces the time and effort required, this is where Docker comes in. Installing WordPress with Docker is a breeze, read ahead to find out more.

WordPress with Docker

Docker is a container platform that allows simple and fast software installations on any system and OS. It wraps the piece of software in a complete file system that includes everything it needs to run such as code, runtime, system tools and libraries. This allows anyone to package an application with its dependencies into a standardized building block.

Install Docker

Installing Docker itself is already easy. Firstly run the usual update command for your system to make sure you have the latest source lists.

# Debian and Ubuntu
sudo apt-get update
# CentOS
sudo yum update

Check that you have the curl command line utility.

curl -V

It comes preinstalled with most Linux distributions, but if it can not be found, install it manually with the appropriate command for your OS.

# Debian and Ubuntu
sudo apt-get install curl
# CentOS
sudo yum install curl

Use the command below to download and install Docker. The process requires root privileges so you will be asked for your sudo password on any non-root user.

curl -fsSL https://get.docker.com/ | sh

Towards the end of the installation process, you will see a suggestion to add your username to the Docker users group. Doing this allows you to run Docker commands without needing to invoke sudo every time.

sudo usermod -aG docker <username>

Log out and back in again after adding yourself to the Docker users group before continuing.

You can check that the installation was successful with the following test program:

docker run hello-world

You should see an output similar to the example below.

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker.
This message shows that your installation appears to be working correctly.
...

If the command does not work immediately, restart the Docker service with the following and try to run the hello-world app again.

sudo systemctl restart docker

Docker should now be installed and working correctly. Continue on below with the rest of the WordPress setup.

MariaDB in a container

Before installing WordPress with Docker you will need to have somewhere to store the data. MariaDB is a community-developed relational database management system and a drop-in replacement for MySQL. It is officially available on Docker and provides easy instructions with up-to-date images.

Start off by making a new directory where you wish to store the files for WordPress and MariaDB for example in your home directory.

mkdir ~/wordpress && cd ~/wordpress

Downloading and installing a new MariaDB container can all be performed with a single command. Before jumping in check the required parameters.

MariaDB Environment variables are marked in the Docker command with -e:

  • -e MYSQL_ROOT_PASSWORD= Set your own password here.
  • -e MYSQL_DATABASE= Creates and names a new database e.g. wordpress.

Docker parameters:

  • –name wordpressdb – Names the container.
  • -v “$PWD/database”:/var/lib/mysql – Creates a data directory linked to the container storage to ensure data persistence.
  • -d – Tells Docker to run the container in the daemon.
  • mariadb:latest – Finally defines what to install and which version.

Then run the command below while replacing the <password> with your own.

docker run -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest
...
Status: Downloaded newer image for mariadb:latest
23df0ec2e48beb1fb8704ba612e9eb083f4193ecceb11102bc91232955cccc54

If Docker was successful at creating the container, you should see a code at the end of the output similar to the example above. You can confirm that the MariaDB container is running by using the following command:

docker ps

Check the status for your MariaDB install, it should show “Up” and the time it has been running like in the example output below.

CONTAINER ID IMAGE          COMMAND                CREATED        STATUS        PORTS      NAMES
14649c5b7e9a mariadb:latest "/docker-entrypoint.s" 12 seconds ago Up 12 seconds 3306/tcp   wordpressdb

Other useful commands for working with containers are ‘start’, ‘stop’ and ‘remove’.

docker start <container name>
docker stop <container name>
docker rm <container name>

You can find out more about available commands and options for specific commands.

docker --help
docker <command> --help

Full command-line documentation is also available over at the Docker support page.

WordPress with Docker

Applications in containers run isolated from one another in the userspace of the host operating system sharing the kernel with other containers. This reduces the overhead required to run packaged software while also enabling the containers to run on any kind of infrastructure. To allow applications within different containers to work with one another Docker supports container linking.

WordPress is also made officially available on Docker Hub, pull the image using the command below. When the version to download is not specified Docker will fetch the latest available.

docker pull wordpress

WordPress container also takes environment variables and Docker parameters:

  • -e WORDPRESS_DB_PASSWORD= Set the same database password here.
  • –name wordpress – Gives the container a name.
  • –link wordpressdb:mysql – Links the WordPress container with the MariaDB container so that the applications can interact.
  • -p 80:80 – Tells Docker to pass connections from your server’s HTTP port to the container’s internal port 80.
  • -v “$PWD/html”:/var/www/html – Sets the WordPress files accessible from outside the container. The volume files will remain even if the container was removed.
  • -d – Makes the container run on background
  • wordpress – Tells Docker what to install. Uses the package downloaded earlier with the docker pull wordpress -command.

Run the command below while replacing the <password> as you did for the MariaDB container.

docker run -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html -d wordpress

Then open your server’s domain name or IP address in a web browser to test the installation. You should be redirected to the initial WordPress setup page at http://<public IP>/wp-admin/install.php. Go through the setup wizard and you are done.

WordPress initial setup page

If you get an error linking your server’s public IP address to the WordPress container’s internal address, remove the failed container using the following command:

docker rm wordpress

Restart Docker and the database container, and also make sure no other service is already bound to port 80.

sudo systemctl restart docker
docker start wordpressdb

Then try creating the WordPress container again.

Conclusions

Congratulations, you should now have a simple WordPress with Docker running in a container and an easy way to configure another one if needed. Before continuing on building your new WordPress site, make sure to pay attention to the security on your server. To find out more, check out our article How To Secure Your Linux Cloud Server.

Janne Ruostemaa

Editor-in-Chief

  1. Hi, I am a bit confused. What does Docker actually do? Does it allow us to scale WordPress to multiple servers without the downtime and automatic replication?

  2. Janne Ruostemaa

    Hi Prasad, thanks for the question. Docker packages WordPress into a neat little container that is easy to manage. For example, it allows you to deploy multiple instances of WordPress onto the same server while keeping them separate from one another. The scaling you are referring to is something more akin to a cluster that Docker Swarm is then intended for.

  3. Hi Janne, any chance you might have a tutorial on how to deploy multiple instances of WP onto the same server too?

  4. Janne Ruostemaa

    Hi Jonathan, thanks for the question. Deploying multiple WP instances on a single server is pretty easy using Docker but you’ll need a proxy to direct connections to the right web site. Unfortunately, we don’t have a tutorial for this right at this moment but I’ll mark down a request for one.

  5. Hello, don’t you guys have autopWordpress image.

    I signed up opnUpCLoud and was trying to deploy a new server for my WP website, but I can’t find any WordPress image, Only Were listed their and as I am not so Technical Sound, it’s very hard for me to instal lwordpress manually…

    Will your team helping It.

  6. Janne Ruostemaa

    Hi there, thanks for reaching out. Our cloud images are clean slates with no preinstalled software like WordPress but it’s really easy to install, for example, using Docker like described in this tutorial. Give it a go and see for yourself :)

  7. Gustavo Cardoso

    Thank you for this article. It really helped e a lot.

    What if I need to install more than one WordPress site? What do you recommend? A single MariaDB container and various WP containers!?!

    Thanks again!

  8. Janne Ruostemaa

    Hi Gustavo, thanks for the question. Indeed, you could reuse the database container but it’s not necessary and having a separate DB for each WordPress container would likely be easier to manage. Please note though that you will need a reverse proxy to be able to access multiple WP sites running on a single host, for example using Nginx.

  9. I had to put `-d wordpress` at the beginning, after `docker run`, or else it gave me a `exec: –: invalid option` error.

  10. Janne Ruostemaa

    Hi Benji, thanks for the comment. The error likely happened if you try to execute the run command twice. Using docker exec -d wordpress lets you start the already created container.

  11. Thank you! Thanks to you I was able to create my blog :)
    Regards!

  12. Installation is a bit complicated for non-techie, I have researched that upcloud performs better than Linode, If I want to install multiple websites in a single server how to do that, any guide?

  13. Janne Ruostemaa

    Hi there, thanks for the question. If you want to install and maintain multiple websites on a single server, I’d recommend having a look at Cloudron. While it requires a little setup, it’s not that difficult if you follow our tutorial for getting started with Cloudron.

  14. Hi, if I want to add a domain to my website, how would I do this? Also, could I migrate my wordpress website in a standard wordpress way(e.g. follow other tutorials) to another server?

  15. How do I enable Https with this tutorial?

  16. Janne Ruostemaa

    Hi Stanley, thanks for the question. To set a domain to your WordPress site, you’ll need to create an A record with your domain name registrar. As for migrating an existing WordPress site, you can simply copy the HTML directory from your current web server to ~/wordpress/html which is linked as a directory volume into the container. However, you’d also need to migrate your database and start the MariaDB container using the existing database.

  17. Janne Ruostemaa

    Enabling access over HTTPS on a docker web server requires the use of a proxy container, for example, nginx-proxy and to obtain SSL certificates using Let’s Encrypt.

  18. Hey @Janne Ruostemaa, this is a very nice tutorial. It would also be much awesome if there is a part 2 that outlines multiple WP sites with swarm setup, assigning domain names and https too! :) Thanks!

  19. Janne Ruostemaa

    Hi Carl, thanks for the suggestion. We certainly like to hear feedback on our tutorials and will take your request into account when planning new additions.

  20. Hi, i’m a noob in Docker.

    I have installed WordPress with Docker (SO: Win 7)

    I can’t login using docker-compose and the yml file. I had this problem: “MySQL Connection Error”.

    So, i found your post and did that:


    $docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress –name wordpressdb -v “$PWD/database”:/var/lib/mysql -d mariadb:latest

    $docker pull wordpress

    $docker run -e WORDPRESS_DB_PASSWORD=password –name wordpress –link wordpressdb:mysql -p 8083:80 -v “$PWD/html”:/var/www/html -d wordpress

    I could enter to the initial WordPress setup page at http://192.168.99.100:8083/wp-admin/install.php;
    and it work it!

    I could work with my site, but then i rebooted the PC and now, i can’t access to my site.

    The container’s status were “Exited”. So, i restarted the containers, but wordpress appared as if i have never used it.

  21. Janne Ruostemaa

    Hi Javier, thanks for the question. As you noticed, restarting your computer will require you to also restart the docker containers. However, you can have any changes saved by using the volume folders. You need to start the containers with the same volume parameters e.g. -v "$PWD/database”:/var/lib/mysql to continue where you left off. Docker-compose is the easiest way to do this by simply calling docker-compose up -d to start your WordPress site again after a reboot.

  22. Thanks for the answer.
    So, I have to start the containers running:

    $docker start #id_container -v “$PWD/database”:/var/lib/mysql

    or using docker-compose?

  23. I start the containers, and then I make the docker-compose file and execute “$docker-compose up -d”
    I got the message:
    “Creating wordpress-compose_mariadb_1 … done
    Creating wordpress-compose_wordpress_1 … done”
    But when i go to my site, i don´t have my last site.

  24. Janne Ruostemaa

    If you already had manually started your WP and database containers, compose created a new pair. Just stop the old containers and copy the html and database folders of your first WordPress site to where your docker-compose.yml file is saved. Then restart the site using docker-compose.

  25. I used docker-compose with the same run command’s data and i can access to wordpress, but when i reboot, i can’t start the db_container.
    It starts, but the status inmediatly changes to “Exitted”

    Thanks for your answers.

  26. Vincent Cheng

    Hi thanks for your sharing, I’ve got an issue when I followd your instruction when

    docker run -e MYSQL_ROOT_PASSWORD= -e MYSQL_DATABASE=wordpress –name wordpressdb -v “$PWD/database”:/var/lib/mysql -d mariadb:latest

    The screen was shown
    “docker run” requires at least 1 argument.
    See ‘docker run –help’.

    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

    May I know the reason?

    Thanks for your help

  27. Janne Ruostemaa

    Hi Vincent, thanks for the question. The error message indicates that docker could not read the parameters as expected. The bare minimum for running docker e.g. the MariaDB container docker run mariadb:latest . However, you also need to give the other parameters to connect the database with the WordPress container as explained in the tutorial. If you have difficulties copy/pasting the database docker run command, try typing it manually to see that there are no missing characters.

  28. Hi,
    i did everything just like You did and set My own Password and so on. but it keeps saying “Error establishing a database Connection”
    I cant fix that what is the Problem ?

  29. How can we provide the security (hardening the wordpress)for the wordpress application without any help from plugins

  30. I found the below URL but little confused to implement that..
    https://wordpress.org/support/article/hardening-wordpress

  31. Janne Ruostemaa

    Hi Sayer, thanks for the question. Check that you’ve set the same password for both MYSQL_ROOT_PASSWORD= in MariaDB container and WORDPRESS_DB_PASSWORD= for WordPress. If these are correct, make sure your database container is running or try to recreate it and then restart your WordPress container.

  32. Janne Ruostemaa

    Hi Mohan, thanks for the question. Plugins on WordPress are often one of the main reasons for security vulnerabilities and you should only install plugins you trust.

  33. Janne Ruostemaa

    The instructions are quite generic as to help in most use cases. However, you may wish to focus on the steps to limiting access, keeping all software up to date and making sure all password are secure and complex enough. As you are running WordPress on docker, you should also have a look at their security guide.

  34. YouBeRelentless

    Thanks for taking the time to write this step by step tutorial on docker and wordpress. Just fired up my first wordpress container :D

  35. Very informative Documentation thanks to your effort. This docker make things easy for Linux users to develop wordpress web and other applications. I may say goodbye to hard core installation and configuration in Linux system.

  36. Hi,

    Thanks for the artical.

    Just wondering what’s the “mysql” in below command (–link wordpressdb:mysql) creating WordPress container? the WordPress container is named as WordPress, while the mariadb is named as wordpressdb….

    docker run -e WORDPRESS_DB_PASSWORD= –name wordpress –link wordpressdb:mysql -p 80:80 -v “$PWD/html”:/var/www/html -d WordPress

    Thanks,
    Chance

  37. Janne Ruostemaa

    Hi Chance, thanks for the question. The --link wordpressdb:mysql creates an alias called mysql for the wordrpessdb container within the wordpress container. This allows you to use MariaDB container as a drop-in replacement for MySQL without having to change anything in the WordPress container itself.

  38. Excellent easy to follow guide. Quite new to docker and this helped a treat. Thanks again.

  39. Hi. Thanks for your article. I have the same problem. I checked the docker db and this is working. I have checked the two passwords and they are the same. I have also updated the firewall with parts and services. But it still doesn’t work. Some idea?

  40. Janne Ruostemaa

    Hi Rosario, thanks for the question. If the WordPress container fails to start or stay up, you might have something already binding to port 80. Try running the following to start the WordPress container in port 8080 instead:

    docker run -e WORDPRESS_DB_PASSWORD= --name wordpress2 --link wordpressdb:mysql -p 8080:80 -v "$PWD/html":/var/www/html -d wordpress
  41. Hi,
    i have the same problem – it does not work :-(
    Any further idea?

  42. Janne Ruostemaa

    Hi Mike, thanks for the comment. Seems an update to Docker has changed the default username which now needs to be defined manually by adding -e WORDPRESS_DB_USER=root to the WordPress container command. We’ve updated the tutorial to show the correct command.

  43. Thanks this was the easiest tutorial I could find on running WordPress on Docker. I’m new to docker so I’m not sure what command the run if I do a reboot of the machine? If you could clarify that for me please?

  44. Janne Ruostemaa

    Hi Jaco, thanks for the question. You can manually start the containers by running the following command.

    docker start wordpressdb wordpress

    It’s also possible to have Docker restart the containers automatically by enabling a restart policy. Update the policy on the currently running containers by using the command below.

    docker update --restart unless-stopped $(docker ps -q)

    Now the containers should be restarted when the Docker daemon starts at boot.

  45. what does mean by multiple websites on a single server.
    Like here we have upcloud.com is a website on a server (godaddy).
    and my website is showing blog for one container
    and if I change the container of shop website
    It will show me the new shop website with upcloud.com?
    Kindly clear it.

  46. Janne Ruostemaa

    Hi Suny, thanks for the question. You are right that if you have a domain name configured for your web server and change the running container, the new website will respond instead. However, the above comment refers to running multiple websites on a single server at the same time. This can be done easily by using an application management system such as Cloudron.

  47. Dennis Yildirim

    Well, I tried all these recipes, and guidelines along the web none worked out so far. Every guide have their own glitch, it either does not work straight from the yaml file, or end up with not being able to communicate with the maria db at all no matter what (including user names, passwords, ports, and so on.)

    Just curious, whats the deal with creating a new database inside docker? Say I have a synology already, I would already have my own DB running. Obviously, the only major reason why people want to use docker WordPress is to run a second website.

    So in this case, why are you insisting on using maria 10 db in docker rather than to use the existing one already?

  48. Janne Ruostemaa

    Hi Dennis, thanks for the comment. Sorry to hear you are having difficulty completing the steps in the tutorial. The process outlined here assumes a rather clean starting point. For example, a newly deployed Cloud Server. It’s possible to follow the tutorial on many other systems but you might need to adapt some steps.

    As for your question regarding the database configuration. You are of course free to use an existing database you might have installed on your system, but as mentioned, the tutorial is written to not require one. The advantage of running the database within a Docker container is just that, it’s self-contained. Therefore, you can have as many independent database instances as you need.

  49. Excellent!!

  50. Hi Janne, a great post! A few minutes and I have the WordPress running! Thanks!

  51. Jack Northrup

    Great instructions. Took me about 10 minutes to have a WordPress Site up and running.
    Thank you

  52. Thanks so much for this post! I’m a Docker newbie and really apricate you explaining this topic using a real life example.

  53. Yep, that’s sort of the idea. You could use a front end proxy that uses health monitors to detect container availability and essentially take your old container offline once you’re satisfied the updated version works

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top