第七章 Dockerfile安装lnmp环境

一、环境配置

使用自建的网络
环境配置
操作系统:CentOS Linux release 7.6.1810
Docker 版本:20.10.2
Nginx 版本:1.18.0
PHP 版本:7.4.14
MySQL 版本:8.0.18
Redis 版本:6.0.9

二、架构预想

#1.端口
  - 80
  - 443

#2.服务
  - nginx
  - web01  nginx + php
  - web02  nginx + php
  - web03  nginx + php
  - redis
  - mysql

#3.镜像
  - nginx
  - php
  - redis
  - mysql

#4.调用链
  - nginx -> php -> redis|mysql

三、安装Docker

1.查看系统内核版本号
[root@m01 ~]# uname -a
Linux m01 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

# 备份原来YUN源
[root@m01 ~]# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

# 更换YUM源
[root@m01 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 刷新yum源缓存
[root@m01 ~]# yum makecache

#新机器更新系统
[root@alvin-test-os ~]# yum update -y --exclud=kernel*

2.安装依赖包
[root@m01 ~]#  yum install -y yum-utils device-mapper-persistent-data lvm2

3.添加docker镜像源
[root@m01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@m01 ~]# ll /etc/yum.repos.d/
total 12
-rw-r--r--. 1 root root 2523 Aug  6 00:39 CentOS-Base.repo
-rw-r--r--  1 root root 2640 Mar 16  2020 docker-ce.repo
-rw-r--r--. 1 root root  664 Aug  6 00:40 epel.repo

4.安装docker
[root@m01 ~]# yum -y install docker-ce 

5.设置开机自启动
[root@m01 ~]# systemctl  enable  --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

6.验证
[root@m01 ~]# docker --version  
Docker version 20.10.2, build 2291f61

7.设置加速器
[root@m01 ~]# sudo mkdir -p /etc/docker
[root@m01 ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://8mh75mhz.mirror.aliyuncs.com"]
}
EOF
[root@m01 ~]# sudo systemctl daemon-reload
[root@m01 ~]# sudo systemctl restart docker

四、创建公用网络

[root@m01 ~]# docker network  create  lnmp
60b3851bb861eedcd81552ab6b2cd872949382b41058958828f353290fba0435

[root@m01 /docker/nginx]# docker network  ls
NETWORK ID     NAME         DRIVER    SCOPE
e59f461f55f2   bridge       bridge    local
fc5d4a69c90d   host         host      local
60b3851bb861   lnmp         bridge    local
286591e7f6a6   new_bridge   bridge    local
16b8d6c8be22   none         null      local

五、创建Dockerfile目录

[root@m01 ~]# mkdir /docker
[root@m01 ~]# cd /docker
[root@m01 /docker]# mkdir nginx
[root@m01 /docker]# mkdir nginx_php
[root@m01 /docker]# mkdir php
[root@m01 /docker]# mkdir redis
[root@m01 /docker]# mkdir mysql

六、构建lnmp镜像

1.Nginx 代理Dockerfile

1.构建nginx代理Dockerfile
[root@m01 /docker]# cd ./nginx
[root@m01 /docker/nginx]# vim Dockerfile 
FROM nginx:1.18.0
#维护者信息
MAINTAINER gf
#时区
ENV TZ Asia/Shanghai
RUN date -R
#代理文件
ADD proxy.conf /etc/nginx/conf.d
#容器启动时执行指令
CMD ["nginx", "-g", "daemon off;"]

2.编写proxy.conf
[root@m01 /docker/nginx]# vim proxy.conf
#设定负载均衡的服务器列表
upstream mysvr {
    #weigth参数表示权值,权值越高被分配到的几率越大
    #本机上的Squid开启3128端口
    server web01;
    server web02;
    server web03;
}

server {
    #侦听80端口
    listen       80;
    #定义使用www.xx.com访问
    server_name  linux.proxy.com;
    #默认请求
    location / {
        proxy_pass  http://mysvr; #请求转向mysvr 定义的服务器列表
    }
}

3.构建镜像
[root@m01 /docker/nginx]# docker build -t nginx:upstream .

4.查看镜像
[root@m01 /docker/nginx]# docker images 
REPOSITORY   TAG        IMAGE ID       CREATED         SIZE
nginx        upstream   44b2f2e5dcd5   7 seconds ago   133MB
nginx        1.18.0     b9e1dc12387a   2 days ago      133MB

2.web Dockfile

1.构建web Dockerfile
[root@m01 /docker/nginx]# cd ..
[root@m01 /docker]# cd nginx_php/
[root@m01 /docker/nginx_php]# vim Dockerfile
FROM nginx:1.18.0
#维护者信息
MAINTAINER gf
#时区
ENV TZ Asia/Shanghai
RUN date -R
#代理文件
ADD web.conf /etc/nginx/conf.d
#容器启动时执行指令
CMD ["nginx", "-g", "daemon off;"]

2.编写default.conf
[root@m01 /docker/nginx_php]# vim default.conf
server {
    listen 80;
    server_name _;
    location / {
        root /usr/share/nginx/html;
        index index.html index.php;
    }
    location ~* \.php$ {
        root /usr/share/nginx/html;
        fastcgi_index index.php;
        fastcgi_pass php:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
        include fastcgi_params;
    }
}

3.构建镜像
[root@m01 /docker/nginx_php]# docker build -t nginx:web .

4.查看镜像
[root@m01 /docker/nginx_php]# docker images
REPOSITORY   TAG        IMAGE ID       CREATED          SIZE
nginx        web        fbf3b6d89506   16 seconds ago   133MB
nginx        upstream   44b2f2e5dcd5   15 minutes ago   133MB
nginx        1.18.0     b9e1dc12387a   2 days ago       133MB

3.Php Dockerfile

1.下载redis扩展包
[root@m01 /docker/nginx_php]# cd ..
[root@m01 /docker]# cd php/
[root@m01 /docker/php]# wget https://pecl.php.net/get/redis-5.3.2.tgz

2.构建PHP Dockerfile
[root@m01 /docker/php]# vim Dockerfile
FROM php:7.4.14-fpm
#维护者信息
MAINTAINER gf
#时区
ENV TZ Asia/Shanghai
RUN date -R
#redis扩展文件
ADD redis-5.3.2.tgz /opt
#安装redis扩展库
RUN mkdir -p /usr/share/nginx/html
RUN cd /opt/redis-5.3.2 && phpize && ./configure --with-php-config=php-config && make && make install && echo "extension=redis.so" >> /usr/local/etc/php/php.ini
#指定PHP端口
EXPOSE 9000
#容器启动时执行指令
CMD ["php-fpm"]

3.构建镜像
[root@m01 /docker/php]# docker build -t php:php .

4.查看镜像
[root@m01 /docker/php]# docker images
REPOSITORY   TAG          IMAGE ID       CREATED          SIZE
php          php          57f675b72138   13 seconds ago   428MB
nginx        web          fbf3b6d89506   14 minutes ago   133MB
nginx        upstream     44b2f2e5dcd5   29 minutes ago   133MB
nginx        1.18.0       b9e1dc12387a   3 days ago       133MB
php          7.4.14-fpm   f5460fa2369d   3 days ago       405MB

4.Redis Dockerfile

1.构建redis Dockerfile
[root@m01 /docker/php]# cd ..
[root@m01 /docker]# cd redis/
[root@m01 /docker/redis]# vim Dockerfile
From redis:6.0.9
#维护者信息
MAINTAINER gf
#时区
ENV TZ Asia/Shanghai
RUN date -R
#容器启动时执行指令
CMD ["redis-server"]

2.构建镜像
[root@m01 /docker/redis]# docker build -t redis:redis .

3.查看镜像
[root@m01 /docker/redis]# docker images
REPOSITORY   TAG          IMAGE ID       CREATED          SIZE
redis        redis        126f7c8a8f2b   18 seconds ago   104MB
php          php          57f675b72138   8 minutes ago    428MB
nginx        web          fbf3b6d89506   23 minutes ago   133MB
nginx        upstream     44b2f2e5dcd5   38 minutes ago   133MB
redis        6.0.9        6060df96cef3   2 days ago       104MB
nginx        1.18.0       b9e1dc12387a   3 days ago       133MB
php          7.4.14-fpm   f5460fa2369d   3 days ago       405MB

5.Mysql Dockerfile

1.构建mysql Dockerfile
[root@m01 /docker/redis]# cd ..
[root@m01 /docker]# cd mysql/
[root@m01 /docker/mysql]# vim Dockerfile
From mysql:5.7
#维护者信息
MAINTAINER gf
#时区
ENV TZ Asia/Shanghai
RUN date -R
#容器启动时执行指令
CMD ["mysqld"]

2.构建镜像
[root@m01 /docker/mysql]# docker build -t mysql:mysql .

3.查看镜像
[root@m01 /docker/mysql]# docker images 
REPOSITORY   TAG          IMAGE ID       CREATED              SIZE
mysql        mysql        53e8ab5f0193   About a minute ago   449MB
redis        redis        126f7c8a8f2b   4 minutes ago        104MB
php          php          57f675b72138   13 minutes ago       428MB
nginx        web          fbf3b6d89506   27 minutes ago       133MB
nginx        upstream     44b2f2e5dcd5   43 minutes ago       133MB
redis        6.0.9        6060df96cef3   2 days ago           104MB
nginx        1.18.0       b9e1dc12387a   3 days ago           133MB
mysql        5.7          cc8775c0fe94   3 days ago           449MB
php          7.4.14-fpm   f5460fa2369d   3 days ago           405MB

七、启动docker实例

1.新建测试文件
[root@m01 /docker]# mkdir ./data/
[root@m01 /docker]# cd data/
[root@docker /docker/data]# cat phpinfo.php 
<?php
phpinfo();

[root@docker /docker/data]# cat redis.php 
<?php
$redis = new Redis();
$redis->connect('redis', 6379);
// $redis->auth('mypassword');
$redis ->set( "test" , "Hello World, Hello China");
echo $redis ->get( "test");

[root@docker /docker/data]# cat mysql.php 
<?php
$servername="mysql";
$username="root";
$password="123456";
$dbname="mysql";
// 创建连接
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
 
$sql = "show databases";
$result = mysqli_query($conn, $sql);
 
if (mysqli_num_rows($result) > 0) {
    // 输出数据
    while($row = mysqli_f

2.启动docker实例
[root@docker /docker/data]# docker run -d --network=lnmp --name redis redis:redis
e19bc4def599140c7ceb8bea4b52185c9a1c2a377eb31e7c83ffb3877e4a88c7

[root@docker /docker/data]# docker run -d --network=lnmp -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:mysql
0b851142ae328791956f1024dd9295a417e08e7a1e259719c8dd9af2109e32f8

[root@m01 /docker/data]# docker run -d --network=lnmp --name php -v /docker/data:/usr/share/nginx/html php:v1
0e4967177f25e85275cb646207140b55f14020405698d1cdc6814be97ae296fa

[root@m01 /docker/data]# docker run -d --network=lnmp --name web01 -v /docker/data:/usr/share/nginx/html nginx:web
38403308deaaad64fd1731affad6c58a651d989e03be2341a158aae96c66229a

[root@m01 /docker/data]# docker run -d --network=lnmp --name web02 -v /docker/data:/usr/share/nginx/html nginx:web
ca64e2022436ad65f7e338c204a4f3fdbce59bde5103625668d10e359a556500

[root@m01 /docker/data]# docker run -d --network=lnmp --name web03 -v /docker/data:/usr/share/nginx/html nginx:web
340550a37d0187588c834025415915e7764c8e2e15733722a434f5ce36479307

[root@m01 /docker/data]# docker run -d --network=lnmp -p 80:80 --name proxy nginx:upstream 
99aa03602605c52954ac3224071269b829757cc01ee394c5cb2e9faff161041f

3.查看docker进程

[root@m01 /docker/data]# docker ps 
CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS          PORTS                 NAMES
99aa03602605   nginx:upstream   "/docker-entrypoint.…"   43 seconds ago   Up 41 seconds   0.0.0.0:80->80/tcp    proxy
340550a37d01   nginx:web        "/docker-entrypoint.…"   3 minutes ago    Up 3 minutes    80/tcp                web03
ca64e2022436   nginx:web        "/docker-entrypoint.…"   3 minutes ago    Up 3 minutes    80/tcp                web02
38403308deaa   nginx:web        "/docker-entrypoint.…"   4 minutes ago    Up 4 minutes    80/tcp                web01
11973602ec92   php:php          "docker-php-entrypoi…"   17 minutes ago   Up 17 minutes   9000/tcp              php
0b851142ae32   mysql:mysql      "docker-entrypoint.s…"   19 minutes ago   Up 19 minutes   3306/tcp, 33060/tcp   mysql
e19bc4def599   126f7c8a8f2b     "docker-entrypoint.s…"   20 minutes ago   Up 20 minutes   6379/tcp              redis

八、访问测试

http://linux.proxy.com/mysql.php         #ok
http://linux.proxy.com/phpinfo.php		#ok
http://linux.proxy.com/redis.php		#ok

posted @ 2022-09-23 15:58  高压锅炖主播  阅读(282)  评论(0编辑  收藏  举报