码迷,mamicode.com
首页 > 其他好文 > 详细

docker创建lnmp镜像

时间:2020-02-27 18:54:22      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:写入   镜像   for   group   oca   可伸缩   openssl   ash   from   

  docker是一个轻量级的虚拟化技术,而lnmp是一个强大、开源的web运行环境,这里我们就演示用Docker来构建一个lnmp镜像。

  PS:为了保持轻量化和可伸缩,Docker鼓励我们 “one process per container”,也就是不要在一个镜像中集成太多的功能,我们这里主要是为了学习研究,所以违背了这个准则,更好的方案是Nginx、Mysql、PHP各自创建镜像,在单独的容器里运行,然后通过容器间的互联实现通信。

 

1、拉取官方的 centos:6.9 作为基础镜像,安装好基础环境

[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、安装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、安装Nginx并配置PHP fastcgi

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

配置Nginx以支持PHP,修改 /etc/nginx/conf.d/default.conf 如下:

#
# 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 {
    }
 
}

创建 /www 目录,并且修改用户和用户组为 apache (php-fpm默认运行用户和组为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、安装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、编写启动命令

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

然后向startup.sh写入如下内容:

#!/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、清理不再需要的数据以缩减镜像的尺寸,然后docker commit生成新镜像

[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、测试镜像,启动容器

[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

再测试mysql是否正常

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

代码内容:

<?php
 
$db = new mysqli(127.0.0.1, root, 123456, mysql);
 
if ($db->connect_errno)
{
    die("连接数据库失败". $db->connect_error);
}
 
echo "连接成功";
 
$db->close();

访问 127.0.0.1:8848/db.php

[root@localhost www]# curl 127.0.0.1:8848/db.php
连接成功[root@localhost www]#

 

docker创建lnmp镜像

标签:写入   镜像   for   group   oca   可伸缩   openssl   ash   from   

原文地址:https://www.cnblogs.com/is-possible/p/12373378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!