catalogue
2. Write the Dockerfile script of Nginx
7. Backup Nginx mirror to local
3. Write Dockerfile script for MySQL
7. Backup MySQL image to local
2. Writing Dockerfile scripts for PHP
5. Customize the ip address and start the mirror container
2. Browser access authentication
1, Deployment environment
container | operating system | ip address | Main software |
nginx | Centos7.6 | 172.18.0.11 | nginx-1.12.0.tar.gz,wordpress-4.9.4-zh_CN.tar |
mysql | Centos7.6 | 172.18.0.12 | mysql-boost-5.7.20.tar.gz |
php | Centos7.6 | 172.18.0.13 | php-7.1.10.tar |
Download address of software package http://101.34.22.188/lnmp_wordpress/ |
2, Installing centos:7
[root@docker-lnmp ~]# docker pull centos:7 [root@docker-lnmp ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 eeb6ee3f44bd 2 months ago 204MB
3, Custom network
[root@docker-lnmp ~]# docker network create --subnet=172.18.0.0/16 mynetwork ae2c0ab23b97011855e9c89099e8d1f8a6c045b185e569f86333e06d6589b7bd [root@docker-lnmp ~]# docker network ls NETWORK ID NAME DRIVER SCOPE fbed913fbc3a bridge bridge local c31e47162b35 host host local ae2c0ab23b97 mynetwork bridge local ff4a150e9cc3 none null local [root@docker-lnmp ~]# ifconfig br-ae2c0ab23b97: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255 ether 02:42:91:3f:6c:25 txqueuelen 0 (Ethernet) RX packets 204647 bytes 299898840 (286.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 90995 bytes 5920196 (5.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4, Installing nginx
1. Create Nginx directory
[root@docker-lnmp ~]# mkdir /opt/nginx [root@docker-lnmp ~]# cd /opt/nginx [root@docker-lnmp nginx]# ls nginx-1.12.0.tar.gz wordpress-4.9.4-zh_CN.tar.gz
2. Write the Dockerfile script of Nginx
[root@docker-lnmp nginx]# vim Dockerfile FROM centos:7 MAINTAINER this is nginx image RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx ADD nginx-1.12.0.tar.gz /usr/local/src/ WORKDIR /usr/local/src/nginx-1.12.0 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module;make -j 4 && make install ENV PATH /usr/local/nginx/sbin:$PATH ADD nginx.conf /usr/local/nginx/conf/ ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html RUN chmod 777 -R /usr/local/nginx/html/ EXPOSE 80 VOLUME [ "/usr/local/nginx/html/" ] CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
3. Configure nginx.conf file
[root@docker-lnmp nginx]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; charset utf-8; location / { root html; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 172.111.0.30:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }
4. Generate nginx image
[root@docker-lnmp nginx]# docker build -t lnmp:nginx1 . [root@docker-lnmp nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE lnmp nginx1 3cb059547907 13 seconds ago 523MB centos 7 eeb6ee3f44bd 2 months ago 204MB
5. Customize the ip address and start the image container (limit the maximum use of 500MB of memory and 1G of Swap by the Nginx container)
[root@docker-lnmp nginx]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.18.0.11 lnmp:nginx1 a1c5bb69bd325c14f49a3f242745d0e68b6372bf7c31fda47ca5523045a6aac6 [root@docker-lnmp nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1c5bb69bd32 lnmp:nginx1 "/usr/local/nginx/sb..." 20 seconds ago Up 19 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@docker-lnmp nginx]# docker inspect nginx ...... "IPAddress": "172.18.0.11", ......
6. Verify Nginx
[root@docker-lnmp nginx]# curl http://192.168.32.15:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
7. Backup Nginx mirror to local
[root@docker-lnmp ~]# cd /opt/ [root@docker-lnmp opt]# mkdir lamp_images_bak-2021.12.1 [root@docker-lnmp opt]# cd lamp_images_bak-2021.12.1/ [root@docker-lnmp lamp_images_bak-2021.12.1]# docker save -o nginx_image-2021.12.1 lnmp:nginx1
5, Install MySQL
1. Create MySQL directory
[root@docker-lnmp ~]# cd /opt/ [root@docker-lnmp opt]# mkdir mysql [root@docker-lnmp opt]# cd mysql/ [root@docker-lnmp mysql]# ls mysql-boost-5.7.20.tar.gz
2. Configure my.cnf file
[root@docker-lnmp mysql]# vim my.cnf [client] port = 3306 default-character-set=utf8 socket=/usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket=/usr/local/mysql/mysql.sock auto-rehash [mysqld] user = mysql basedir=/usr/local/mysql datadir=/usr/local/mysql/data port = 3306 character-set-server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket=/usr/local/mysql/mysql.sock bind-address = 0.0.0.0 skip-name-resolve max_connections=2048 default-storage-engine=INNODB max_allowed_packet=16M server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
3. Write Dockerfile script for MySQL
[root@docker-lnmp mysql]# vim Dockerfile FROM centos:7 MAINTAINER this is mysql image RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql ADD mysql-boost-5.7.20.tar.gz /usr/local/src/ WORKDIR /usr/local/src/mysql-5.7.20/ RUN cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1;make -j4;make install ADD my.cnf /etc/my.cnf EXPOSE 3306 RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf WORKDIR /usr/local/mysql/bin/ RUN ./mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH VOLUME [ "/usr/local/mysql" ] CMD ["/usr/sbin/init"]
4. Generate MySQL image
[root@docker-lnmp mysql]# docker build -t lnmp:mysql1 . [root@docker-lnmp mysql]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE lnmp mysql1 d01a7ffd13fb 4 minutes ago 9.98GB lnmp nginx1 3cb059547907 44 minutes ago 523MB centos 7 eeb6ee3f44bd 2 months ago 204MB
5. Customize the ip address and start the image container (limit the Mysql container to write / dev/sda at a rate of 10 MB/s)
[root@docker-lnmp mysql]# docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.18.0.12 lnmp: mysql14f0471889436c501fdc87b124ae4afaab90cd73a94dd157355da083f44732746 [root@docker-lnmp mysql]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f0471889436 lnmp:mysql1 "/usr/sbin/init" 53 seconds ago Up 50 seconds 3306/tcp mysql [root@docker-lnmp nginx]# docker inspect mysql ...... "IPAddress": "172.18.0.12", ......
6. Validate MySQL
[root@docker-lnmp mysql]# docker exec -it 4f0471889436 /bin/bash [root@4f0471889436 bin]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.20 Source distribution Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
7. Backup MySQL image to local
[root@docker-lnmp ~]# cd /opt/lamp_images_bak-2021.12.1/ [root@docker-lnmp lamp_images_bak-2021.12.1]#docker save -o mysql_image-2021.12.1 lnmp:mysql1
6, Install PHP
1. Create PHP directory
root@docker-lnmp ~]# cd /opt/ [root@docker-lnmp opt]# mkdir php [root@docker-lnmp opt]# cd php/ [root@docker-lnmp php]# ls php-7.1.10.tar.bz2
2. Writing Dockerfile scripts for PHP
[root@docker-lnmp php]# vim Dockerfile FROM centos:7 MAINTAINER this is php image RUN yum install -y gd \ libjpeg libjpeg-devel \ libpng libpng-devel \ freetype freetype-devel \ libxml2 libxml2-devel \ zlib zlib-devel \ curl curl-devel \ openssl openssl-devel \ gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx ADD php-7.1.10.tar.bz2 /usr/local/src/ WORKDIR /usr/local/src/php-7.1.10 RUN ./configure \ --prefix=/usr/local/php \ --with-mysql-sock=/usr/local/mysql/mysql.sock \ --with-mysqli \ --with-zlib \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-openssl \ --enable-fpm \ --enable-mbstring \ --enable-xml \ --enable-session \ --enable-ftp \ --enable-pdo \ --enable-tokenizer \ --enable-zip ; make -j 4 ; make install ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH ADD php.ini /usr/local/php/lib/ ADD php-fpm.conf /usr/local/php/etc/ ADD www.conf /usr/local/php/etc/php-fpm.d/ EXPOSE 9000 CMD /usr/local/php/sbin/php-fpm -F
3. Modify three profiles
(1) Php.ini configuration file. The configuration file template is located in php-7.1.10/php.ini-development in the installation directory
[root@docker-lnmp php]# vim php.ini date.timezone = Asia/Shanghai #Line 939, uncomment, modify mysqli.default_socket = /usr/local/mysql/mysql.sock #Line 1170, modify
[root@docker php]# egrep -v "^;" php.ini | egrep -v "^$" [PHP] engine = On short_open_tag = Off precision = 14 output_buffering = 4096 zlib.output_compression = Off implicit_flush = Off unserialize_callback_func = serialize_precision = -1 disable_functions = disable_classes = zend.enable_gc = On expose_php = On max_execution_time = 30 max_input_time = 60 memory_limit = 128M error_reporting = E_ALL display_errors = On display_startup_errors = On log_errors = On log_errors_max_len = 1024 ignore_repeated_errors = Off ignore_repeated_source = Off report_memleaks = On track_errors = On html_errors = On variables_order = "GPCS" request_order = "GP" register_argc_argv = Off auto_globals_jit = On post_max_size = 8M auto_prepend_file = auto_append_file = default_mimetype = "text/html" default_charset = "UTF-8" doc_root = user_dir = enable_dl = Off file_uploads = On upload_max_filesize = 2M max_file_uploads = 20 allow_url_fopen = On allow_url_include = Off default_socket_timeout = 60 [CLI Server] cli_server.color = On [Date] date.timezone = Asia/Shanghai [filter] [iconv] [intl] [sqlite3] [Pcre] [Pdo] [Pdo_mysql] pdo_mysql.cache_size = 2000 pdo_mysql.default_socket= [Phar] [mail function] SMTP = localhost smtp_port = 25 mail.add_x_header = On [SQL] sql.safe_mode = Off [ODBC] odbc.allow_persistent = On odbc.check_persistent = On odbc.max_persistent = -1 odbc.max_links = -1 odbc.defaultlrl = 4096 odbc.defaultbinmode = 1 [Interbase] ibase.allow_persistent = 1 ibase.max_persistent = -1 ibase.max_links = -1 ibase.timestampformat = "%Y-%m-%d %H:%M:%S" ibase.dateformat = "%Y-%m-%d" ibase.timeformat = "%H:%M:%S" [MySQLi] mysqli.max_persistent = -1 mysqli.allow_persistent = On mysqli.max_links = -1 mysqli.cache_size = 2000 mysqli.default_port = 3306 mysqli.default_socket = /usr/local/mysql/mysql.sock mysqli.default_host = mysqli.default_user = mysqli.default_pw = mysqli.reconnect = Off [mysqlnd] mysqlnd.collect_statistics = On mysqlnd.collect_memory_statistics = On [OCI8] [PostgreSQL] pgsql.allow_persistent = On pgsql.auto_reset_persistent = Off pgsql.max_persistent = -1 pgsql.max_links = -1 pgsql.ignore_notice = 0 pgsql.log_notice = 0 [bcmath] bcmath.scale = 0 [browscap] [Session] session.save_handler = files session.use_strict_mode = 0 session.use_cookies = 1 session.use_only_cookies = 1 session.name = PHPSESSID session.auto_start = 0 session.cookie_lifetime = 0 session.cookie_path = / session.cookie_domain = session.cookie_httponly = session.serialize_handler = php session.gc_probability = 1 session.gc_divisor = 1000 session.gc_maxlifetime = 1440 session.referer_check = session.cache_limiter = nocache session.cache_expire = 180 session.use_trans_sid = 0 session.sid_length = 26 session.trans_sid_tags = "a=href,area=href,frame=src,form=" session.sid_bits_per_character = 5 [Assertion] zend.assertions = 1 [COM] [mbstring] [gd] [exif] [Tidy] tidy.clean_output = Off [soap] soap.wsdl_cache_enabled=1 soap.wsdl_cache_dir="/tmp" soap.wsdl_cache_ttl=86400 soap.wsdl_cache_limit = 5 [sysvshm] [ldap] ldap.max_links = -1 [mcrypt] [dba] [opcache] [curl] [openssl]
(2) Php-fpm.conf configuration file, which is located in / usr/local/php/etc/php-fpm.conf.default
[root@docker-lnmp php]# vim php-fpm.conf pid = run/php-fpm.pid #Line 17, delete the comment symbol ";"
[root@docker php]# vim php-fpm.conf [global] pid = run/php-fpm.pid include=/usr/local/php/etc/php-fpm.d/*.conf
(3) Www.conf configuration file, which is located in usr / local / PHP / etc / PHP FPM. D / www.conf.default
[root@docker-lnmp php]# vim www.conf user = nginx #Line 23, modify user group = nginx #Line 24, modify group listen = 172.18.0.13:9000 #Line 36, modify the listening IP and port to container IP:9000 port
[root@docker php]# vim www.conf [www] user = nginx group = nginx listen = 172.88.0.30:9000 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3
4. Generate PHP image
[root@docker-lnmp php]# docker build -t lnmp:php1 . [root@docker-lnmp php]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE lnmp php1 aaea7ef44552 10 seconds ago 1.21GB lnmp mysql1 d01a7ffd13fb 32 minutes ago 9.98GB lnmp nginx1 3cb059547907 About an hour ago 523MB centos 7 eeb6ee3f44bd 2 months ago 204MB
5. Customize the ip address and start the mirror container
[root@docker-lnmp php]# docker run -itd --name php --net mynetwork --ip 172.18.0.13 -p 9000:9000 --volumes-from nginx --volumes-from mysql lnmp:php1 14425a2f64997faba7ac790bec8c9fc4ea3acc42200b3a585168e4a12d706a5e [root@docker-lnmp php]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14425a2f6499 lnmp:php1 "/bin/sh -c '/usr/lo..." 10 seconds ago Up 9 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php 4f0471889436 lnmp:mysql1 "/usr/sbin/init" 27 minutes ago Up 27 minutes 3306/tcp mysql a1c5bb69bd32 lnmp:nginx1 "/usr/local/nginx/sb..." 56 minutes ago Up 56 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@docker-lnmp php]# docker inspect php ...... "IPAddress": "172.18.0.13", ......
6. Validate PHP
[root@docker-lnmp php]# docker exec -it 14425a2f6499 /bin/bash [root@14425a2f6499 php-7.1.10]# ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 113028 7520 pts/0 Ss+ 08:36 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nginx 8 0.0 0.0 113028 3768 pts/0 S+ 08:36 0:00 php-fpm: pool www nginx 9 0.0 0.0 113028 3768 pts/0 S+ 08:36 0:00 php-fpm: pool www root 10 0.0 0.0 11828 1900 pts/1 Ss 08:39 0:00 /bin/bash root 24 0.0 0.0 51732 1728 pts/1 R+ 08:40 0:00 ps -aux
7. Backup PHP image to local
[root@docker-lnmp ~]# cd /opt/ [root@docker-lnmp opt]# cd lamp_images_bak-2021.12.1/ [root@docker-lnmp lamp_images_bak-2021.12.1]#docker save -o php_image-2021.12.1 lnmp:php1
7, Start Wordpress service
1. MySQL authorization
[root@docker-lnmp ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 14425a2f6499 lnmp:php1 "/bin/sh -c '/usr/lo..." 15 minutes ago Up 15 minutes 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php 4f0471889436 lnmp:mysql1 "/usr/sbin/init" 43 minutes ago Up 42 minutes 3306/tcp mysql a1c5bb69bd32 lnmp:nginx1 "/usr/local/nginx/sb..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@docker-lnmp ~]# docker exec -it 4f0471889436 /bin/bash [root@4f0471889436 bin]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.20 Source distribution Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
2. Browser access authentication
http://192.168.32.15/wordpress/index.php