Edited on 17.5.2022

How to install WordPress with Docker

Try UpCloud for free! Deploy a server in just 45 seconds

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 reduce 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 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 containers 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, 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 for How To Secure Your Linux Cloud Server.

Janne Ruostemaa

  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?

    Reply
  2. 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.

    Reply
  3. 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!

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

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

    Reply
  6. 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?

    Reply
  7. 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?

    Reply
  8. How do I enable Https with this tutorial?

    Reply
  9. 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!

    Reply
  10. 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.

    Reply
  11. 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

    Reply
  12. 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 ?

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

    Reply
  14. 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

    Reply
  15. 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.

    Reply
  16. 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

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

    Reply
  18. 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?

    Reply
  19. 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?

    Reply
  20. Excellent!!

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

    Reply
  22. Jack Northrup

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

    Reply

Leave a Reply

Your email address will not be published.

Back to top