How to install WordPress in docker container using a Dockerfile?

Spread the love

Many of us wanted to host our website using docker but some of us are not sure how can we do it. In this post I will install WordPress using Dockerfile. I have already wrote a brief description about Dockerfile in another post. You can choose whatever web server you want but here, I have chosen nginx for some advantages over Apache web server. Also, I have use PHP 7.3 and MySQL for database.

The easiest way to write a Dockerfile is to first create a container using the base image and then run each command one by one yourself. For example, if I am going to use base image of Ubuntu:18.04 then I will first run a container of this image as following and then run commands inside container.

I have used a new option here --rm which means to automatically remove container when it exists. Do not get confused…

Now install a web server Nginx, PHP and required extensions and database server MySQL. Before that make sure to update packages otherwise we will not be able to install any of the above.

Now we can install require packages.

Install PHP and extensions that is require by WordPress

We will also require few other basic packages.

Start the services

We have started all the services we require and if we do a curl request on localhost, it will show us the Apache default page. We can browse docker container IP address and check in our browser. We will see something like below

Nice! Our next goal is to download WordPress from official website and replace current /var/www/html directory. We can do it in the following way

If we browse the same IP address now, we will possible not see WordPress installation wizard, instead we will see 403 forbidden message

This is because we have not configured our web server correctly. You can directly replace /etc/nginx/sites-enabled/default with following

Now reload Nginx

You should now see a WordPress installation wizard. Great!

To install WordPress, we will need MySQL database username, password and a database name. We will create it manually. Enter to MySQL prompt using the following command and create MySQL database name, username and password.

Enter these credentials on the installation windows as following

Finally, run the installation

Congratulations! We have installed WordPress in a docker container manually. Now copy every command in the Dockerfile

Since we have used Ubuntu 18.04 as base image we will tell Dockerfile about this using FROM

In front of every above line we will use RUN which tells to run those command

You can even combine all the installation part as one like

Further I will add label

I have removed lines where we have started the service. I will create a separate file called and paste those commands there. I have also introduce EXPOSE, since we are using web server we will require to expose port 80. In above Dockerfile, I have copied to / dir of container and run it at the end.

Similarly, we will save the Nginx configuration default in default file and paste the content there. Lets put both of these files inside a folder called config. You can name your folder as it is. The final Dockerfile will look like below

NOTE: ENV DEBIAN_FRONTEND=noninteractive is to set avoid interaction. Otherwise you will be asked to set your region. Please visit

One more thing before we build an image out of above Dockerfile. To keep container running we will need a process running in foreground. Since we have used all three services eg MySQL, PHP, nginx, to run in background, the container will die instantly. To keep our container running, we will use simple hack. We are using nginx therefore the nginx log file /var/log/nginx/error.log exists. We will tail the output which will keep our docker container alive. This is not the best way but for this post is will work.

The config/ file will look like

Build our docker container using Dockerfile

Run the container

If you browse the IP of docker container then you will see the WordPress installation wizard.

The post is quite long now. I should limit the post for now. We will see how can we automate MySQL in WordPress installation later.

Let me know if you have any questions or suggestions.

Spread the love

Leave a Reply

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