Categories
Cloud Sotfware & DevOps Tools & HowTo

How to Host WordPress Using Docker-Compose?

WordPress (WP) is a free-open-source website creation platform. It is the most popular Content Management System due to its simplicity and effectiveness. Check out our article on how to host WordPress from scratch. However, setting up a new environment every time is a bit tedious and time taking if you need to do it often. In this article, we’ll showWordPress Using Docker-Compose in a container. WP is made using PHP, MySQL and of course JavaScript and HTML.

Why WordPress Using Docker-Compose in a Container?

 A container is a standard unit of software that bundles up code and all the dependencies so the application can run in the intended setup. Some of the benefits are, reusability, flexibility, less resource consumption, robustness, isolation. Container tools like Docker, Podman, Rocket are in huge demand and exponentially increasing day by day. Checkout our interesting article on hosting Jitsi on Docker.

So we got an approach to run the same workloads with more efficiency. But the question of setting up the system again and again from scratch remains unsolved.

Docker-compose seems a good option. Basically, Docker compose is a tool for defining and running containers in a unified manner. Specifically, with compose, we can use the YAML file to configure the application service. Use the declarative approach and mirror user-defined requirements and deploy containers with a single simple command.

WordPress Using Docker Compose.
WordPress (WP) on Docker

Demonstration:

Here, we’ve taken Ubuntu Linux as a Docker host and it is running on AWS cloud with root powers. On this host OS, we can run multiple different containers with different OS and packages. Containers are isolated yet use the resources from the host system.

Step 1: Installation of Docker on host system:

We have to install the Docker package to run the Docker daemon and run compose files. Use the below command for installation:

To update package manage: apt-get update
To install Docker:  curl -fsSL https://get.docker.com/ | sh
To start services: systemctl start docker
Docker installation
Docker installation

There are multiple ways to install Docker.
The systemctl command will start the Docker service, allowing us to utilize container operations such as creating, deleting containers, managing container networks, attaching volume, and many more.
Verify the running service with the systemctl status docker command.

You landed the Cloud Storage of the future internet. Cloud Storage Services Sesame Disk by NiHao Cloud

Use it NOW and forever!

Support the growth of a Team File sharing system that works for people in China, USA, Europe, APAC and everywhere else.
Step:2 : Installation of Docker-compose

After installing and configuring the Docker service, now is the time to set up docker-compose and enable its capabilities. Using compose, we can simply declare the container blueprint and provision containers with the same program file. It gives the benefit of reusability with a simple way of use.
Note: Install curl command-line utility in the system if not present: apt-get install curl) Check out our article on curl for more information.

Install compose using this command: curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Alter the execution permissions: chmod +x /usr/local/bin/docker-compose

Verify the compose installation using: docker-compose –v command
Docker-compose installation
Docker-Compose installation

Finally, done with the one-time setup of the server. Now we’ve docker service and composer running in the server and are ready to start. Although, we can launch containers without compose. But for large-scale infrastructure, it is recommended to use such an “IaC” approach.

There is a concept of Docker image. Basically, an image is kind of a blueprint or base of a container. It is the essential unit to launch a container. We store and maintain these images in repositories. Docker hub is a public repository for docker images. We can find frequently used and custom-created docker images here.

Here, we’ve taken readily available images for WordPress (WP) and MariaDB. We’ve used MariaDB as a database to persistently store the content of the website. Using composer, we’ll link both the front-end and backend and create a working website.

Step 3: Creating composer file for WP
mkdir wordpress 
cd wordpress
vim docker-compose.yml 

Naming the composer file as above is a standard convention. Alternatively, we can explicitly pass the compose filename with the -f flag. Good practice to implement compose files in separate directories. Indentation is compulsory in YAML

wordpress:
  image: wordpress
  links:
    - mariadb:mysql
  environment:
    - WORDPRESS_DB_PASSWORD = your password
    - WORDPRESS_DB_USER= root
    - WORDPRESS_DB_NAME= db name
  ports: '-8081:80'
mariadb:
  image: mariadb
  environment:
    - MYSQL_ROOT_PASSWORD = your password
    - MYSQL_DATABASE = db name

Here, we have declared two containers named WordPress and MySQL in the flavor of MariaDB respectively. The image tag refers to the base image for containers. WordPress and MariaDB are the image names. The links tag implicitly represents the link of the mentioned container. The environment keyword is used to pass environmental variables (also known as env vars in OS development jargon) while launching the container to fulfil the dependencies. The ports keyword is used to expose the container to the outer world. Here we’ve taken post 8081 of host os and bind to port 80 of the WordPress Using Docker Compose. So any client coming to port 8081 will be redirected to container port 80. In addition, make sure the password in the environmental variable is correct in both containers.

Now the composer file is ready. Create the whole setup with the command below:

Docker-compose up –d
docker compose command execution
docker-compose command execution

The process is successful if the above command doesn’t return any error. Cross-check the same by hitting the site at public_ip:8081. Complete the installation process and the end page would look like this:

wordpress dashboard
WP dashboard

Hence, the installation is done and linked with the backend database. This is how containers help to provision lightweight os. And compose makes it even more feasible by bundling multiple tasks in a file. That is WordPress Using Docker Compose. Thanks for reading.!



By Poojan Mehta

Poojan Mehta lives in Gujrat, India, and pursuing a bachelor's degree in computer science. He has extensive experience in the software industry. As a technical enthusiast, he has been involved in various roles that include public cloud administration, database administration with SQL Server, web and mobile app development, DevOps work, technical writing, and mentoring. He is proficient in various operating systems, including Red Hat Linux and Windows. He is especially keen on developing new systems with emerging technologies.

Leave a Reply

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

Start Sharing and Storing Files for Free

You can also get your own Unlimited Cloud Storage on our pay as you go product.
Other cool features include: up to 100GB size for each file.
Speed all over the world. Reliability with 3 copies of every file you upload. Snapshot for point in time recovery.
Collaborate with web office and send files to colleagues everywhere; in China & APAC, USA, Europe...
Tear prices for costs saving and more much more...
Create a Free Account Products Pricing Page