conclusion

This year, we met the technical transformation of the company. All the projects need to be changed from uploading files to the server through Git to the k8S configuration and Docker construction and deployment, forming a fully automated pipeline.

Because I have several WordPress projects at hand, the burden of the initial construction and deployment of the Docker of WordPress has been placed on me, who is pure white. I knew nothing about K8S and Docker before, let alone the deployment of the project in a short time, so at the beginning, I spent about two or three days to make up for the principle and operation of Docker, and then completed the pit of WordPress in the next few days, and finally successfully deployed it in the real environment.

The following is my record of stepping pits, very suitable and I like the small white.

The project requirements

All WordPress type websites are constructed and deployed through K8S configuration and Docker, forming a fully automated pipeline. After that, the project just needs to submit the code locally, and it can be automatically updated online after a few minutes.

Kubernetes is an open source application for managing containerized applications on multiple hosts in the cloud platform. The goal is to make the deployment of containerized applications simple and efficient. It provides a one-stop service for application deployment, planning, updating, and maintenance.

Docker is an open source application container engine, which is based on Go language and open source in compliance with Apache2.0 protocol. It can package applications and dependency packages into a lightweight and portable container, and then release them to any popular Linux machine to realize virtualization. It contains three basic concepts. These are Image, Container, and Repository.

Equipment specification

Pure white + Mac10.15.5

Install the Docker

Download the Docker software that matches the native version directly

Docker official website installation address: docs.docker.com/get-docker/

Docker added Ali Cloud image accelerator

  1. Login ali Cloud container image service cr.console.aliyun.com/cn-hangzhou… Replicator accelerator address

  2. Click the Docker Desktop application icon on the taskbar -> Perferences, select Docker Engine in the left navigation menu, and add the following code to the right input field.

    “registry-mirrors”: [“xxxxx.mirror.aliyuncs.com”]

The sample

Common Docker commands

  • Docker pull XXX downloads the XXX image (Docker automatically downloads the image when using a non-existent image on the localhost)
  • Docker ps view all images
  • Docker runit XXX runs the XXX image
  • Docker exec -it [containerName] /bin/sh Executes container scripts in interactive mode
  • Docker Image Ls or Docker images lists images on the local host
  • Docker image rm [imageName] Deletes the image
  • Docker Container ls views all running containers
  • Docker container ls –all View all containers (including stop)
  • Docker search XXX command to search for XXX images
  • Docker rmi -f XXX Forcibly deletes the XXX image
  • The exit exit
  • Docker Hub for image search, Docker Hub website: hub.docker.com/

Building Docker containers (Tests)

New project

Create a new test project docker-test

In the project root directory, create a new text file Dockerfile and write the following

FROM the node: 8.4 the COPY/app WORKDIR/app RUN NPM install - registry=https://registry.npm.taobao.org EXPOSE was 3000Copy the code

Statement interpretation

  • FROM node:8.4: This image file inherits the official Node image. The colon represents the label, which in this case is 8.4, or version 8.4 of a node.
  • COPY. /app: COPY all files in the current directory (except the path excluded by.dockerignore) into the /app directory of the image file.
  • WORKDIR /app: specify the next working path as /app.
  • RUN NPM install: In the /app directory, RUN the NPM install command to install dependencies. Note that all dependencies will be packaged into the image file after installation.
  • EXPOSE 3000: Exposes the container 3000 port, allowing external connections to this port.

Create the image file

docker image build -t docker-test .
Copy the code
  • -t parameter: Specifies the name of the image file
  • . Parameter: Be sure to include a dot that indicates the path where the Dockerfile resides

Note: If no label information is specified, the default value is latest

View the newly generated image file

docker image ls
Copy the code

or

docker images
Copy the code

The sample

Generate containers

docker container run -p 8000:3000 -it demo-test /bin/bash
Copy the code

Statement interpretation

  • -p: Port 3000 of the container is mapped to port 8000 of the local machine
  • -it argument: The container’s Shell is mapped to the current Shell, and commands entered in the native window are passed to the container
  • /bin/bash: the first command executed internally after the container is started

Now that docker-test is up and running, open http://127.0.0.1:8000 in your browser

Publish to your own repository

  1. Log in to your DockerHub account first

    docker login

  2. Create a new image

    Docker build -t/imagename: 1.0

  3. Set the label of the image

    Docker tag [imageID] [username] / [imagename] : 1.0

Statement interpretation

Docker tag [Image ID] [user name]/[Image source name]: new tag name

  1. Push images to DockerHub

    Docker push [username] / [imagename] : 1.0

Deploying WordPress locally

Obtaining a WordPress Image

For WordPress versions see the example on Dockerhub

To obtain the WordPress image, go to

Docker pull wordpress: 5.5Copy the code

Create mysql container

Because WordPress needs to configure the database, it also needs to pull the image of mysql

docker pull mysql
Copy the code

Create mysql container and run with database password 123456

docker run --name mysql_db -e MYSQL_ROOT_PASSWORD=123456 -d mysql
Copy the code

–name: Specifies the name of the container

-e: Specifies the environment variable

Creating a WordPress container

Then create and run a new WordPress container (named Wp-Demo), connect to the database (mysql),

Docker run --name wp-demo --link mysql_db:mysql -p 8080:80 -d wordpress:5.5Copy the code

— Link: Used to link containers, so that the source (the linked container) and the receiver (the actively linked container) can communicate with each other, and the receiver can get some data from the source container

-p: specifies the port mapping. The format is host (host) port: container port

-d: Runs the container in the background and returns the container ID

View the theme files in WP-Demo

docker exec -it wp-demo /bin/sh
Copy the code

You can then start installing WordPress by typing http://127.0.0.1:8080/ into your browser

The new theme

Method one platform directly adds a new theme

Select the button “Add New” to upload our own theme

If the file upload failure message is displayed:

The link you followed has expired.

Note WordPress limits the file size. Use the following method to view the file size limit, as shown in the figure

File upload solution

Method 1: Install the plug-in

  • Install the plug-in File Manager

  • Modifying a Configuration File

  • Add the following lines of code

    php_value upload_max_filesize 64M php_value post_max_size 64M php_value max_execution_time 300 php_value max_input_time 300

The sample is as follows

Method 2 The server modifies the file

Change the contents of the.htaccess file directly in the server

Now we can safely upload our own theme pack.

Method two copies the theme file into the container

  • Create a new wordpress-Docker folder and put the theme package in it

The sample

  • Look at the running WordPressversion 5.5 CONTAINER and get its CONTAINER ID

    docker container ls

The sample

  • View the location of the theme package

    pwd

The sample

  • View container Location

    docker exec -it wp-demo /bin/sh pwd

  • Docker cp [filename] [CONTAINER ID]:[CONTAINER name]

    docker cp /Users/skywen_cp/Documents/demo/wordpress-docker/skywen-theme f4e4ea3c21c1:/var/www/html/wp-content/themes

  • Check whether the theme exists in WordPress

    docker exec -it wp-demo /bin/sh cd wp-contents/themes ls

The sample

  • Take a look at the theme inside the website, there are two of the same theme

Deploying WordPress online

Note: This is not a drill, it is best to deploy online after successful local testing

Familiarize yourself with the following commands

Docker image build-t wp:1.0. Run the docker exec it [containerID] /bin/sh # command to check whether the themes file contains the added themes CD wp-content/themesCopy the code

Setting up the code repository

Note: It is best to clear out excess code in the codebase in case the build fails because the file is too large

  • Create a new repository

  • Put the Dockerfile in the repository and modify the parameters in it (mainly the location of the theme package)

    Wp-content /themes /var/ WWW/HTML /wp-content/themes

  • Create a new file. Dockerignore, write the following code

    .git/ wp-admin/ wp-includes/

The sample

New assembly line

In the warehouse select pipeline, New, select blank template, directly create

Enter the task name and select Build cluster

Add steps in task steps, select Build, select mirror build and push Ali Cloud mirror warehouse, select authorization, the area is (East China 1 (Hangzhou), and fill in the rest parameters according to the following figure

If the repository cannot be found, add the repository as follows

Build the mirror

After the code is uploaded, the image is automatically built; Click log to view the build process

At this point, all build deployments are complete, and there are error signs.