AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

How to install WordPress in Docker containers?

PARTAGEZ

Find out how to deploy a WordPress installation in Docker containers, both manually and with Docker composes. WordPress developers will find it useful to deploy WordPress in Docker containers. Indeed, Docker allows you to do tests with several WordPress configurations and launch a new WordPress installation with a few simple commands.

WordPress accommodation

WordPress, easier and faster thanks to AI

  • Creation and personalization of your site assisted by IA
  • 3x faster: SSD, chattering and more
  • Daily safety analyzes, DDOS protection and availability of 99.98 %

Requirements

For WordPress to work without problem in Docker containers, you must have a recent and supported Linux distribution. The previous versions such as Centos 7 or Ubuntu 14.04 are obsolete and must be replaced by modern alternatives. Are recommended:

  • Ubuntu 22.04 LTS or a more recent version.
  • Debian 12 or more recent version.
  • A recent version of Red Hat Enterprise Linux (RHEL) or Almalinux.

In addition, Docker must be installed and ready for use. The minimum required version of Docker is the version 20.10 or superiorin order to be able to benefit from the latest safety and performance improvements. If you want to use Docker composes, be sure to use at least version 2.X, because the previous versions are no longer actively maintained.

Basic knowledge on the use of the command line, on Docker composes and on Docker in general is useful. If you provide more important or scalable configurations, take a look at Kubernetes in order to carry out a professional level container orchestration.

Execution of WordPress in Docker containers

A successful installation of WordPress consists of three elements:

  • WordPress software
  • A MySQL or Mariadb database
  • The latest steps of the installation, which are carried out in a browser

For the following examples, the WordPress and MySQL/Mariadb components are executed in distinct linked containers. The container on which the WordPress software runs is associated with a port on the host so that you can access it in a browser.

Execute a MySQL/Mariadb container

After launching Docker, the first step is to configure the database. To do this, start by executing a container called My-DB. You can use MySQL or Mariadb, which is a replacement of MySQL by Drop-in.

Mysql

Start a container with the command:

sudo docker run --name my-db -e MYSQL_ROOT_PASSWORD=MOTDEPASSE_SECURISE -d mysql:latest

Mariadb

Start a container with the command:

sudo docker run --name my-db -e MYSQL_ROOT_PASSWORD=MOTDEPASSE_SECURISE -d mariadb:latest

Note

Be sure to use a secure password in all cases for your database. The use of environmental variables to store passwords is also a potential safety flaw. It is best to use a Secret Docker or a secure configuration file instead.

Create a database

After creating your container, you must create the database you want to use for your WordPress installation.

Mysql

Connect to your database container that you have just created using the following command:

docker exec -it my-db mysql -u root -p

Then create a database:

CREATE DATABASEversion: '3'
services:
    wordpress:
        image: wordpress:latest
        links:
            - wordpress_db:mysql
        ports:
            - 8080:80
    wordpress_db:
        image: mysql
        environment:
            MYSQL_ROOT_PASSWORD: MOTDEPASSE_SECURISE wordpress-db;

Mariadb

Connect to the database container you just created using the following command:

docker exec -it my-db mariadb -u root -p

Now create a database:

CREATE Database wordpress-db;

Execute a WordPress container

Now run a container from the official image of WordPress, which will be mapped to the 8080 host port and associated with the database container.

Take into account the following elements:

  • If you have a firewall, you may need to add access to the 8080 port.
  • If you have already executed another service on the 8080 port, you can select another port on the host.

The command varies slightly depending on whether you use MySQL or Mariadb:

Mysql

Start a WordPress container with the following command:

sudo docker run --name my-wordpress -p 8080:80 --link my-db:mysql -d wordpress:latest

Mariadb

Start a WordPress container with the command:

sudo docker run --name my-wordpress -p 8080:80 --link my-db:mariadb -d wordpress:latest

There are many other environment variables that you can add to this command if you want to replace the default settings, including:

  • -e WORDPRESS_DB_HOST=[hostname] : The default is the IP address and the port of the linked MySQL/Mariadb container. This variable allows you to access a MySQL/Mariadb database on another server.
  • -e WORDPRESS_DB_USER=[nom d’utilisateur] : the default value is root.
  • -e WORDPRESS_DB_PASSWORD=[mot de passe] : The default value is the MYSQL_ROOT_PASSWORD environment variable of the linked MySQL/MARIADB container.
  • -e WORDPRESS_DB_NAME=[nom] : The default value is « WordPress ».

For increased security, it may be useful not to run the containers on the default Bridge network. You can use your own network instead:

docker network create my-wp-network
docker run --name my-db --network my-wp-network -e MYSQL_ROOT_PASSWORD=MOTDEPASSE_SECURISE -d mysql:latest
docker run --name my-wordpress --network my-wp-network -p 8080:80 -d wordpress:latest

Here, your containers are better isolated from other containers, which reduces the risk of unwanted connections. In addition, network rules can be configured more detailed, for example with specific firewall rules or the use of a Reverse proxy.

Finish the installation in a browser

For the latest steps in the installation, you must access the WordPress container via a browser.

In the previous example, we have mapped the 8080 port on the host to Port 80 (web services) on the container. In this way, you can access the container in a browser either by the IP address or by the server URL:

  • http://:8080
  • http://example.com:8080

Visit the URL in a browser, choose your installation language, then click on  » Continuous  » ( » Following « ).

Image: screenshot of the choice of WordPress language
You can now choose the installation language in your browser.

On the next page, you will receive a message that will prepare you as a result of the installation. Click on the « Let's Go! To continue.

Image: WordPress installation message screenshot
The WordPress installation message informs you of the next step in the configuration.

You must now indicate your database so that the file wp-config.php May be created:

Image: screenshot from connection to the WordPress database
Specify the requested information regarding your database.

Note that the default user is « root », unless otherwise specified. Indicate here the name of the database you have created as well as the chosen password. A click on « submit » (« submit ») will guide you throughout the installation process.

Image: WordPress installation screenshot: site information
Provide the information requested on your WordPress site.
  • Website name : Fill in the name of your website.
  • User name : This is the most important admin username for your website. Note: For security reasons, we recommend that you not Use « Admin » or the name/URL of your website.
  • Password : Note this password in a secure place before continuing.
  • Email: This is the email address of the main admin user.

Then click on the button  » WordPress Install To finish the installation.

Image: WordPress's successful installation of the successful installation
The message indicates that the installation has succeeded.

Once the installation is successful, you will receive a success message and can then connect to WordPress.

Execute WordPress with Docker composed

The use of Docker composes to execute WordPress is also a possibility. For more information on the installation and use of Docker composes, see our article dedicated to the Docker orchestration with Swarm and compose. Basically, the tool allows you to define all the services you need in a single file and launch them together.

Create a yaml file

First of all create a directory for your project and access it:

sudo mkdir wordpress
cd wordpress

Create a yaml file called docker-compose.yml with the command:

sudo nano docker-compose.yml

The content of the file varies slightly depending on whether you use MySQL or Mariadb:

Mysql

Add the following in the file:

wordpress:
    image: wordpress:latest
    links:
        - wordpress_db:mysql
    ports:
        - 8080:80
wordpress_db:
    image: mysql
    environment:
        MYSQL_ROOT_PASSWORD: MOTDEPASSE_SECURISE

Save and leave the file.

Mariadb

Add the following in the file:

wordpress:
    image: wordpress:latest
    links:
        - wordpress_db:mariadb
    ports:
        - 8080:80
wordpress_db:
    image: mariadb
    environment:
        MYSQL_ROOT_PASSWORD: MOTDEPASSE_SECURISE

Save and leave the file.

Start containers

Now use Docker composes to start these containers with the following command:

sudo docker compose up -d

The following command allows you to verify that the containers have been created:

To finish the installation, visit the WordPress container in a browser. To do this, use either the IP address or the server URL:

  • http://192.168.0.1:8080
  • http://example.com:8080

Use Kubernetes for evolutionary WordPress deployments

For larger and productive deployments, you can use Kubernetes (K8S). Kubernetes allows you to orchestrate WordPress containers on several nodes and manage them automatically. The use of a Kubernetes cluster offers a number of advantages such as automatic scaling, self-Healing mechanisms and centralized resource management. An implementation can be carried out for example with Helma packet manager for Kubernetes:

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install my-wordpress bitnami/wordpress --set service.type=LoadBalancer

This ensures a resilient and scalable WordPress deployment with Load Balancing, automatic updates and support for failover.

New call-to-action

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Localhost: how to connect to 127.0.0.1?

When you call an IP address, you are usually trying to contact another computer on the Internet. However, if you call the IP address 127.0.0.1,

Web Marketing

What is Proxmox? – IONOS

Proxmox is an open source platform dedicated to virtualization and containerization. It allows you to manage and operate virtual machines, containers and high availability clusters.

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact