How To Install WordPress With Docker Tutorial
Because of its numerous features and ease of use, WordPress is one of the most popular content management systems (CMS). Setting up a new web host environment, on the other hand, can be time-consuming, especially if you need to do it frequently. Docker helps to reduce the time and effort required by simplifying the installation process to a few quick commands. WordPress installation with Docker is simple; continue reading to learn more.
What is Docker?
Docker is a free and open-source containerization software that creates isolated environments in which to run applications. As a result, you develop, test, and run several applications on the same machine.
Unlike virtual machines, each container does not require its own operating system and instead shares the host's kernel. As a result, the machine's workload is much lighter, and a single server can run multiple containers concurrently.
As a result, Docker is extremely useful for WordPress developers. A WordPress test environment typically consumes a large amount of system resources, but Docker enables them to create a minimal environment without wasting server space or memory.
How To Install WordPress With Docker Tutorial
Step 1: Install Docker
Docker installation is already simple. To begin, run the usual update command for your system to ensure you have the most recent source lists.
# Debian and Ubuntu sudo apt-get update # CentOS sudo yum update
Step 2: Make sure you have the curl command-line utility installed.
It is preinstalled in most Linux distributions, but if it is not found, install it manually using the appropriate command for your operating system.
# Debian and Ubuntu sudo apt-get install curl # CentOS sudo yum install curl
Step 3: Download and install Docker
To download and install Docker, run the command below. Because the process requires root privileges, any non-root user will be prompted for their sudo password.
curl -fsSL https://get.docker.com/ | sh
You will see a suggestion near the end of the installation process to add your username to the Docker users group. This allows you to run Docker commands without having to use sudo each time.
sudo usermod -aG docker <username>
Before proceeding, log out and back in again after adding yourself to the Docker users group.
The following test program can be used to ensure that the installation was successful:
docker run hello-world
You should get something like 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 right away, restart the Docker service and try running the hello-world app again.
sudo systemctl restart docker
Docker should now be installed and operational. Continue with the rest of the WordPress setup below.
You'll need a place to store the data before you can install WordPress with Docker. MariaDB is a community-developed relational database management system that can be used in place of MySQL. It is now officially available on Docker and includes simple instructions as well as up-to-date images.
Begin by creating a new directory in which you want to store the WordPress and MariaDB files, such as your home directory.
mkdir ~/wordpress && cd ~/wordpress
A single command can be used to download and install a new MariaDB container. Before you dive in, make sure you have all of the necessary parameters.
The Docker command specifies MariaDB environment variables with -e:
-e MYSQL_DATABASE - Creates and names a new database e.g. main.
-e MYSQL_ROOT_PASSWORD - Here you can create your own password.
–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 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 in creating the container, you should see a code similar to the example above at the end of the output. The following command will confirm that the MariaDB container is running:
Install WordPress with Docker
Containerized applications run in separate userspace of the host operating system, sharing the kernel with other containers. This reduces the overhead associated with running packaged software while also allowing containers to run on any type of infrastructure. Docker supports container linking, which allows applications in different containers to communicate with one another.
WordPress is now officially available on Docker Hub; use the command below to download the image. Docker will download the most recent version available if no version is specified.
docker pull wordpress
Environment variables and Docker parameters are also accepted by the WordPress container:
- -e WORDPRESS_DB_PASSWORD= Database password here.
- –name wordpress – 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.
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, in a web browser, enter your server's domain name or IP address to test the installation. You should be redirected to the initial WordPress installation page, which is located at http://yourdomain.com/wp-admin/install.php. You're done once you've gone through the setup wizard.