Dokcer14_5: Docker Compose build blog wordpress

docker official website: https://docs.docker.com/samples/wordpress/

Introduction to WordPress

You can use Docker Compose to easily run WordPress in an isolated environment built with Docker containers. This quick start guide demonstrates how to use composition to set up and run WordPress. Before you begin, make sure you have installed authoring

Define project

1. Create an empty project directory.

You can give the catalogue an easy to remember name. This directory is the context of the application image. The directory should contain only the resources that build the image.

The project directory contains a docker - compose The YML file itself is complete for a good starter wordpress project.

Can be used yml or yaml the extension of this file is OK.

2. Change to your project directory

For example, if the directory my is named_ wordpress:

 cd my_wordpress/

3. Create a docker compose yml

Create a docker - compose YML start your WordPress blog and a separate MySQL instance with volume mount for data persistence:

# compose version, downward compatible, the highest version I selected
version: "3.9"

# Define services    
services:
  db:
    image: mysql:5.7
    # Mount the data volume by name, followed by the internal address of the corresponding container, and the generated local directory format: / var/lib/docker/volumes / service name_ Volume name
    volumes:
      - db_data:/var/lib/mysql
    # The definition is always restarted, and the application will be started automatically after docker is restarted
    restart: always
    environment:
      # This variable is mandatory and specifies the password that will be set for the MySQL superuser account
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    # Mount the data volume by name, followed by the internal address of the corresponding container, and the generated local directory format: / var/lib/docker/volumes / service name_ Volume name
    volumes:
      - wordpress_data:/var/www/html
    # Exposed port, external 8000 corresponding to container 80
    ports:
      - "8000:80"
    # The definition is always restarted, and the application will be started automatically after docker is restarted
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

be careful:

docker data volume db_data and wordpress_data is the update of WordPress to the database, as well as the installed themes and plug-ins. WordPress multisite works only on ports 80 and 443

For MySQL environment variables, please refer to the article: docker installs MySQL and synchronizes data to the local computer

Build project

Execute docker compose up - D to the project directory.

Run Docker compose up. In the separation mode, extract the required Docker image and start wordpress and database container, as shown in the following example.

docker-compose up -d
Note: WordPress Multisite works only on ports 80 and / or 443. If you receive an error message about binding 0.0.0.0 to port 80 or 443 (depending on the port you specify), it is likely that the port you configured for WordPress has been used by another service.

Browse WordPress

At this point, WordPress should run 8000 on the port. You can complete the "famous five minute installation" as a WordPress administrator.

Note: the WordPress site cannot use 8000 on port immediately because the container is still initializing and may take a few minutes before it is first loaded.

If you use local, you can use http://localhost As IP address and open http://localhost:8000 In a web browser.


Data volume view

[root@VM-0-3-centos pdx_haokai]# docker volume ls
DRIVER    VOLUME NAME
local     3a3d57ad35507c7833513f55243cd9e1656da241b58b68aab48e5a3c6c55a6d9
local     docker-compose_db_data
local     docker-compose_wordpress_data
[root@VM-0-3-centos pdx_haokai]# docker volume inspect docker-compose_db_data
[
    {
        "CreatedAt": "2022-01-27T22:12:34+08:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "docker-compose",
            "com.docker.compose.version": "1.28.5",
            "com.docker.compose.volume": "db_data"
        },
        "Mountpoint": "/var/lib/docker/volumes/docker-compose_db_data/_data",
        "Name": "docker-compose_db_data",
        "Options": null,
        "Scope": "local"
    }
]
[root@VM-0-3-centos pdx_haokai]# cd /var/lib/docker/volumes/docker-compose_db_data/_data
[root@VM-0-3-centos _data]# ll
total 188480
-rw-r----- 1 polkitd input       56 Jan 27 22:12 auto.cnf
-rw------- 1 polkitd input     1676 Jan 27 22:12 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 Jan 27 22:12 ca.pem
-rw-r--r-- 1 polkitd input     1112 Jan 27 22:12 client-cert.pem
-rw------- 1 polkitd input     1676 Jan 27 22:12 client-key.pem
-rw-r----- 1 polkitd input     1352 Jan 27 22:12 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Jan 27 22:21 ibdata1
-rw-r----- 1 polkitd input 50331648 Jan 27 22:21 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Jan 27 22:12 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Jan 27 22:21 ibtmp1
drwxr-x--- 2 polkitd input     4096 Jan 27 22:12 mysql
drwxr-x--- 2 polkitd input     4096 Jan 27 22:12 performance_schema
-rw------- 1 polkitd input     1680 Jan 27 22:12 private_key.pem
-rw-r--r-- 1 polkitd input      452 Jan 27 22:12 public_key.pem
-rw-r--r-- 1 polkitd input     1112 Jan 27 22:12 server-cert.pem
-rw------- 1 polkitd input     1676 Jan 27 22:12 server-key.pem
drwxr-x--- 2 polkitd input    12288 Jan 27 22:12 sys
drwxr-x--- 2 polkitd input     4096 Jan 27 22:16 wordpress
[root@VM-0-3-centos _data]#

Closing and cleaning

The command docker compose down removes the container and the default network, but retains the WordPress database.

The command docker compose down -- volumes deletes the container, the default network, and the WordPress database.

Keywords: MySQL Docker Container

Added by max_power on Sat, 29 Jan 2022 04:47:30 +0200