Docker: Building the first LNMP website platform for Young people

Source: Internet
Author: User
Tags install wordpress wordpress blog docker ps docker run

1. Background

? LNMP is a combination of (Linux system, Nginx service, MySQL service, PHP service), the image used in this exercise is the official Docker image source https://hub.docker.com, the image is described as follows:

Image Name Official Mirror Address
Nginx PHP Image https://hub.docker.com/r/richarvey/nginx-php-fpm/
MySQL image https://hub.docker.com/_/mysql/

? My nginx PHP is richarve/nginx-php-fpm This integrated image, you can also be nginx, PHP these two mirrors (using two mirrors). Also have to explain, everyone in practice when try not to copy the command, their own manual to knock these commands, the practice environment of this article is on the centos7.4.

2. Start deployment of the 2.1 Department process
    • Install Docker first, this step is not highlighted in this article;
    • Create a custom network for LNMP;
    • Create a MySQL database container and database;
    • Create a nginx/php container;
    • Download wordpress blog system to test;
2.2 Viewing version information
[[email protected] ~]# cat  
[[email protected] ~]# docker versionClient: Version:   17.12.0-ce API version:   1.35 Go version:    go1.9.2 Git commit:    c97c6d6 Built: Wed Dec 27 20:10:14 2017 OS/Arch:   linux/amd64Server: Engine:  Version:  17.12.0-ce  API version:  1.35 (minimum version 1.12)  Go version:   go1.9.2  Git commit:   c97c6d6  Built:    Wed Dec 27 20:12:46 2017  OS/Arch:  linux/amd64  Experimental: false

? It is recommended to use the CentOS 7.4 version, Docker is 17.12.0-CE version (CE is Docker Community Edition, EE is Docker Enterprise Edition).

2.3 Creating a custom network Lnmp
[[email protected] ~]# docker  network  create  lnmpbaa8fe1582a7c38f9663668c4abbf1863e45d2c9ad6c140a93a8b1a41b78e2f6[[email protected] ~]# docker network  lsNETWORK ID          NAME                DRIVER              SCOPE4e09a7db5303        bridge              bridge              locald77497b635bb        host                host                localbaa8fe1582a7        lnmp               bridge              local637e45b21ed3        none                null                local

Note: From the above display can be seen, the Docker service runs, the default is Bridge, host, none of the 3 networks, the network is not the focus of this article, you can see that the LNMP network has been created.

2.4 Modifying a mirror source

We go to download the image by default is a foreign source, the download will be very slow, it is recommended to configure the domestic image source to download:

[[email protected] ~]# vim /etc/docker/daemon.json{  "registry-mirrors": [ "https://registry.docker-cn.com"]}

After modifying the image source, you will also need to restart the Docker service:

[[email protected] ~]# systemctl  restart docker
2.5 Create MySQL Database container download MySQL Image:
[[email protected] ~]# docker pull mysqlUsing default tag: latestlatest: Pulling from library/mysql8176e34d5d92: Pull complete 17e372a8ec90: Pull complete 47b869561d3a: Pull complete c90ab4483f28: Pull complete d6af16572c5c: Pull complete 6d16794d04ac: Pull complete aaf442a8fe75: Pull complete 7c6fa8f07ec4: Pull complete ece17b689642: Pull complete c55b06e76eaf: Pull complete 661fabfb4fc2: Pull complete Digest: sha256:227d5c3f54ee3a70c075b1c3013e72781564000d34fc8c7ec5ec353c5b7ef7faStatus: Downloaded newer image for mysql:latest[[email protected] ~]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEmysql               latest              5d4d51c57ea8        2 days ago          374MB

Description: You can see the MySQL image download the latest latest version, the total size is 374MB.

To run the MySQL container:
[[email protected] ~]# docker run -itd --name lnmp_mysql --net lnmp -p 3306:3306 --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --character-set-server=utf8f187cd6feb9e2ca3ef39b51317e7ea0568f9c84d52e2538ff1a3fe9c57bea890

Parameter description:
-ITD: Open a pseudo terminal in the container to interoperate and run in the background;
--name: Assigns a name to the container lnmp_mysql;
--net: Specify a network environment for the container for the LNMP network;
-P: The container exposes port mappings, mapping the host's 3306 port to the container's 3306 port;
--mout: Mount the data volume Mysql-vol to the/var/lib/mysql directory of the container, which is also the equivalent of data persistence;
-e: Set a variable Mysql_root_password;
MySQL: Specify the MySQL image you just downloaded to start the container;

To see if the MySQL container is running:
[[email protected] ~]# docker top  lnmp_mysql UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMDpolkitd             16488               16473               0                   11:31               pts/0               00:00:00            mysqld --character-set-server=utf8[[email protected] ~]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMESf187cd6feb9e        mysql               "docker-entrypoint.s…"   9 minutes ago       Up 9 minutes        0.0.0.0:3306->3306/tcp   lnmp_mysql
Create a database WP:

Database can be arbitrarily named, I use EXEC here to create a database for WP, created a warning message, do not have to control it:

[[email protected] ~]# docker exec lnmp_mysql sh  -c ‘exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database wp"‘ mysql: [Warning] Using a password on the command line interface can be insecure.

Then we look at whether the database is created:

[[email protected] ~]# docker exec lnmp_mysql sh -c ‘exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"show databases"‘ mysql: [Warning] Using a password on the command line interface can be insecure.Databaseinformation_schemamysqlperformance_schemasyswp
To view the Mysql-vol volume:
[[email protected] ~]# ll/var/lib/docker/volumes/mysql-vol/_data/total 188484-rw-r-----1 POLKITD input 1 11:31 auto.cnf-rw-------1 polkitd input 1675 Mar 1 11:31 ca-key.pem-rw-r--r--1 polkitd input 1107 Mar 1 11  : ca.pem-rw-r--r--1 polkitd input 1107 Mar 1 11:31 client-cert.pem-rw-------1 polkitd input 1675 Mar 1 11:31 Client-key.pem-rw-r-----1 POLKITD input 1335 Mar 1 11:31 ib_buffer_pool-rw-r-----1 POLKITD Input 79691776 Mar 1 1 1:31 ibdata1-rw-r-----1 polkitd input 50331648 Mar 1 11:31 ib_logfile0-rw-r-----1 POLKITD input 50331648 Mar 1 11:31 I B_logfile1-rw-r-----1 POLKITD input 12582912 Mar 1 11:31 ibtmp1drwxr-x---2 polkitd input 4096 Mar 1 11:31 mysqldrw Xr-x---2 polkitd input 8192 Mar 1 11:31 performance_schema-rw-------1 polkitd input 1675 Mar 1 11:31 private_k ey.pem-rw-r--r--1 POLKITD Input 451 Mar 1 11:31 public_key.pem-rw-r--r--1 polkitd input 1107 Mar 1 11:31 Serv ER-CERT.PEM-RW-------1POLKITD input 1675 Mar 1 11:31 server-key.pemdrwxr-x---2 polkitd input 8192 Mar 1 11:31 sysdrwxr-x---2 polkitd Input 1 11:43 WP

You can see from the above that the WP library is also synced to the Mysql-vol data volume.

2.6 Create Nginx, PHP container download Image:
[[email protected] ~]# docker pull richarvey/nginx-php-fpm
To run the container:

First create a/app/wwwroot directory to mount this directory to the/var/www/html in the container:

[[email protected] ~]# mkdir /app/wwwroot -p然后运行容器,以下运行的参数不做解释了,和上文中运行mysql时的参数差不多:[[email protected] ~]# docker run -itd --name lnmp_web --net lnmp -p 80:80 --mount type=bind,src=/app/wwwroot,dst=/var/www/html richarvey/nginx-php-fpmaaea1b4543d34522a9e0d06353f6c65fe858a445c963743ec223bc6a364d0167

To see if the container is functioning properly:

[[email protected] ~]# docker ps -lCONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                                   NAMESaaea1b4543d3        richarvey/nginx-php-fpm   "docker-php-entrypoi…"   2 hours ago         Up 2 hours          443/tcp, 0.0.0.0:80->80/tcp, 9000/tcp   lnmp_web
Get a page and test the container:

A index.html page is generated in the/app/wwwroot/directory, which is already mounted to the/var/www/html of the container, so the resulting index.html is synchronized to the container:

[[email protected] /]# echo  "lnmp" > /app/wwwroot/index.html

Then, access this container host IP:

2.7 Download wordpress blog system test Download to the/app/wwwroot directory:
[[email protected] /]# cd /app/wwwroot/[[email protected] wwwroot]# wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz--2018-03-01 13:52:05--  https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gzResolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 8507412 (8.1M) [application/octet-stream]Saving to: ‘wordpress-4.7.4-zh_CN.tar.gz’100%[=========================================================>] 8,507,412   1.08MB/s   in 8.9s   
Unzip and access the test:
[[email protected] wwwroot]# tar -zxvf wordpress-4.7.4-zh_CN.tar.gz

After the decompression is done with the browser access:
/HTTP Container host Ip/wordpress

Configure WordPress Blog:

The database name can be viewed with Docker PS:

Then, write the configuration file for installation:

As you can see, you need to create the wp-config.php file manually and paste the above information into this new file

[[email protected] wordpress]# vim /app/wwwroot/wordpress/wp-config.php<?php/** * WordPress基础配置文件。 * * 这个文件被安装程序用于自动生成wp-config.php配置文件, * 您可以不使用网站,您需要手动复制这个文件, * 并重命名为“wp-config.php”,然后填入相关信息。 * * 本文件包含以下配置选项: * * * MySQL设置 * * 密钥 * * 数据库表名前缀 * * ABSPATH * * @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php * * @package WordPress......省略部分内容

Install WordPress:

Login WordPress:

In this case, the young man's first LNMP container practice was successfully built.

3. Summary
    • To familiarize yourself with the concept of containers and mirrors
    • To familiarize yourself with the basic commands that Docker uses, Docker run, Docker PS, Docker RM, Docker image, Docker top, Docker network, etc., is understood by many parameters in the Docker run
    • Learn to help with--help, such as Docker run--help
    • There is the concept of data persistence in this article (that is, mount) to understand, using two kinds of mount, one is data volume mount, one is directory mount. Data volume mount does not require you to manually create a data volume, directory mount prerequisite source mount directory you want to exist or you want to create a good

Docker: Building a young man's first LNMP Web site platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.