docker creates lnmp image

Keywords: PHP Docker CentOS MySQL

Docker is a lightweight virtualization technology, and lnmp is a powerful, open-source web running environment, so here's a demonstration of using Docker to build an lnmp image.

PS: To maintain lightweight and scalable performance, Docker encourages us to "one process per person"Container"means don't integrate too many functions into a single image. We're here to learn more, so it violates this rule. A better solution is to create mirrors for Nginx, Mysql, PHP, run in separate containers, and then communicate through interconnection between containers.


1. Pull OfficialCentos:6.9As a base image, install the base environment

[root@localhost ~]# docker pull centos:6.9
6.9: Pulling from centos
055b9989266a: Pull complete 
f1070d829305: Pull complete 
e071bce628ba: Pull complete 
Digest: sha256:e7bdc458659b6e644ae85694f2baaf3727c06ad82186fca80f4e3a8e88907cc3
Status: Downloaded newer image for centos:6.9
[root@localhost ~]# 
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
httpd               2.4.29              91199e851c7a        4 weeks ago         177.3 MB
centos              6.9                 e071bce628ba        4 weeks ago         194.7 MB
ubuntu              14.04               b44ce450cb60        11 weeks ago        188 MB
[root@localhost ~]# 
[root@localhost ~]# docker run -it centos:6.9 /bin/bash
[root@df040eea8088 /]# ls          
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@localhost ~]#
[root@df040eea8088 /]# yum install -y epel-release
.......


2. Install PHP

[root@df040eea8088 /]# yum install -y php php-fpm php-mysql
.........
.........
[root@df040eea8088 /]# php -v
PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[root@df040eea8088 /]# php-fpm -v
PHP 5.3.3 (fpm-fcgi) (built: Mar 22 2017 12:28:05)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[root@df040eea8088 /]# 
[root@df040eea8088 /]# php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
zip
zlib

[Zend Modules]


3. Install Nginx and configure PHP fastcgi

[root@df040eea8088 /]# yum install -y nginx
.......

Configure Nginx to support PHP, modify/etc/nginx/conf.d/Default.confThe following:


#
# The default server
#

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /www;   #change document_root to /www

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    #support php access
    location ~ .*\.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}


Create/www directory and modify user and user group to Apache (php-fpm default run user and group to apache)

[root@df040eea8088 /]# mkdir /www
[root@df040eea8088 /]# chown apache:apache /www 

[root@df040eea8088 /]# ll / | grep www
drwxr-xr-x.   2 apache apache  4096 Dec  4 08:21 www



4. Install mysql

[root@df040eea8088 /]# yum install -y mysql mysql-server
......
......

[root@df040eea8088 /]# /etc/init.d/mysqld start
Starting mysqld:                                           [  OK  ]

[root@df040eea8088 /]# /usr/bin/mysqladmin -u root password '123456'

[root@df040eea8088 /]# /etc/init.d/mysqld stop
Stopping mysqld:                                           [  OK  ]

5. Write startup commands

[root@df040eea8088 /]# touch /bin/startup.sh
[root@df040eea8088 /]# chmod +x /bin/startup.sh

Then toStartup.shWrite the following:

#!/bin/sh

/etc/init.d/mysqld start
/etc/init.d/php-fpm start
/etc/init.d/nginx start


#dead loop
while ((1))
do
        sleep 1h
done

6. Clean up data that is no longer needed to reduce the size of the image, and docker commit generates a new image

[root@df040eea8088 /]# yum remove -y epel-release
[root@df040eea8088 /]# yum clean all
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Cleaning up Everything
Cleaning up list of fastest mirrors

[root@df040eea8088 /]# exit
exit

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
df040eea8088        centos:6.9          "/bin/bash"         2 hours ago         Exited (0) 31 seconds ago                       sleepy_yalow        
[root@localhost ~]# docker commit df0 centos:lnmp
efa371fe9b0ede828fe1999e7831491d1dd24cda26f1a27092a9565f4c781e06

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              lnmp                efa371fe9b0e        57 seconds ago      453.4 MB
httpd               2.4.29              91199e851c7a        4 weeks ago         177.3 MB
centos              6.9                 e071bce628ba        4 weeks ago         194.7 MB
ubuntu              14.04               b44ce450cb60        11 weeks ago        188 MB



7. Test the mirror and start the container

[root@localhost ~]# docker run -d -p 8848:80 -v /www:/www centos:lnmp /bin/startup.sh
[root@localhost www]# cd /www
[root@localhost www]# echo "<?php echo time() . PHP_EOL;" > index.php
[root@localhost www]# cat index.php 
<?php echo time() . PHP_EOL;
[root@localhost www]# curl '127.0.0.1:8848/index.php'
1512380042

Test mysql again


[root@localhost www]# touch /www/db.php

Code content:

<?php

$db = new mysqli('127.0.0.1', 'root', '123456', 'mysql');

if ($db->connect_errno)
{
    die("Failed to connect to database". $db->connect_error);
}

echo "Connection Successful";

$db->close();

Access 127.0.0.1:8848/db.php

[root@localhost www]# curl '127.0.0.1:8848/db.php'
Connection succeeded [Root@localhostWww]# 

That's it ^^




Posted by darkninja_com on Fri, 17 Jul 2020 07:58:04 -0700