How to Install WordPress with Docker
In previous post, we learn How to create Docker image using Dockerfile, in this post, we are going to learn how to install WordPress with Docker container. Docker is a system that provides pre-configured, self-contained applications, frameworks, and software stacks, such as WordPress, Golang, or LAMP. All the application pre-configured in Docker container format. Containers for WordPress and MySQL are available from Docker Hub in the form of images so we will use 2 Docker images one for WordPress image and second MySQL image.
Let’s start to create MySQL container first and move to second WordPress container.
Before creating the new container, create a new directory for the WordPress data, database directory, and the WordPress code directory.
~]$ mkdir wordpress
~]$ mkdir -p wordpress/db
~]$ mkdir -p wordpress/html
Configure the MariaDB Container
In this step, we will download a new MariaDB images from the docker hub registry and create a new container on base image. We will configure a new database and user for the WordPress installation.
~]$ docker pull mariadb
Now you can see the new docker MariaDB image with the command below:
~]$ docker images
Now We will create the new MariaDB container with name ‘dockerwp_db’ using below command :
~]$ docker run -e MYSQL_ROOT_PASSWORD=RanD0mPass -e MYSQL_USER=wpdocker -e MYSQL_PASSWORD=Ghans2019 -e MYSQL_DATABASE=dockerwp_db -v /home/ghansham/wordpress/db:/var/lib/mysql –name wpdatabase -d mariadb
If you haven’t seen any error then you can see the below results :
~]$ docker ps
Please refer the below parameter explanation :
- MYSQL_ROOT_PASSWORD = configure the password for the MySQL root user.
- MYSQL_USER = create a new MySQL user ‘wpdocker’ for the WordPress database.
- MYSQL_PASSWORD = set the password ‘Ghans2019’ for the user ‘wpdocker’.
- MYSQL_DATABASE = create a new database for the ‘wordpress’ installation with name ‘dockerwp_db’.
- -v /home/ghansham/wordpress/db:/varlib/mysql = linked database directory to the mysql directory ‘/var/lib/mysql’ on the container to ensure data persistence.
Now, we have successfully created MariaDB container, now check the new user and the database for the WordPress installation to ensure there is no error with command on top.
We will check wordpressdb container IP address from host machine using below docker command
~]$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' wpdatabase
Next, We will try to connect the wordpressdb container with mysql command from the host machine:
~]$ mysql -u wpdocker -h 172.17.0.2 -p Enter password: <Enter-the-password>
Now check new database ‘wordpress_db’:
MariaDB [(none)]> show databases;
We have successfully created the New MySQL docker container with MySQL user, MySQL Password and MySQL database.
Configure the WordPress Container
Once the MariaDB database container has been created, we will pull the wordpress dcoker image from docker hub registry.
~]$ docker pull wordpress:latest
Now you can see the new docker wordpress image with the command below:
~]$ docker images
Once the Docker wordpress image download has finished, create a new container from the images with the name ‘ wpwebsite ‘.
~]$ docker run -e WORDPRESS_DB_USER=wpdocker -e WORDPRESS_DB_PASSWORD=Ghans2019 -e WORDPRESS_DB_NAME=dockerwp_db -p 1987:80 -v /home/ghansham/wordpress/html:/var/www/html --link wpdatabase:mysql --name wpwebsite -d wordpress
If you haven’t seen any error then you can see the below results :
~]$ docker ps
Please refer the below parameter explanation :
- WORDPRESS_DB_USER = define database user ‘wpdocker’ for the dockerwp_db.
- WORDPRESS_DB_PASSWORD = password ‘Ghans2019@’ for the database user ‘wpdocker’.
- WORDPRESS_DB_NAME = name of the database ‘ dockerwp_db’ created on step 2.
- -p 1987:80 = mapping the port 80 on the container to port 1987 on the host.
- -v /home/ghansham/wordpress/html:/var/www/html = Linking the web root directory ‘/var/www/html’ on the container to the local host directory ‘wordpress/html’.
- –link dockerwp_db:mysql = linking the mariadb container ‘ wpdatabase ‘ to the new continer wordpress ‘ wpwebsite ‘.
You can check it with the curl command on the host IP and port 1987.
~]$ curl -l 192.168.0.111:1987
You can also check on browser using Host IP and Port 1987.
http://192.168.0.111:1987
Now, We have submitted the required information in the above and see the next wordpress admin panel.
The WordPress installation with a MariaDB container and a WordPress container has been successful.
Docker Document Reference : Docker Docs
Hopefully “How to Install WordPress with Docker”, this will helps your understanding. More post related to Docker