关于Docker的简介安装可以参考:Docker-容器简介及安装-云野 (yunyeblog.com)

一.拉取lnmp环境所需要的Nginx,Mysql,php镜像

1. 在Docker中拉取Nginx,Mysql,php镜像

[root@client_01 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
2f44b7a888fa: Pull complete 
8b7dd3ed1dc3: Pull complete 
35497dd96569: Pull complete 
36664b6ce66b: Pull complete 
2d455521f76c: Pull complete 
dc9c4fdb83d6: Pull complete 
8056d2bcf3b6: Pull complete 
Digest: sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

[root@client_01 ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
bce031bc522d: Pull complete 
cf7e9f463619: Pull complete 
105f403783c7: Pull complete 
878e53a613d8: Pull complete 
2a362044e79f: Pull complete 
6e4df4f73cfe: Pull complete 
69263d634755: Pull complete 
fe5e85549202: Pull complete 
5c02229ce6f1: Pull complete 
7320aa32bf42: Pull complete 
Digest: sha256:4ef30b2c11a3366d7bb9ad95c70c0782ae435df52d046553ed931621ea36ffa5
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

[root@client_01 ~]# docker pull php:fpm
fpm: Pulling from library/php
2f44b7a888fa: Already exists 
3a95dcec6035: Pull complete 
e22afa33f327: Pull complete 
1aa61ea11ee8: Pull complete 
204e8b7d4798: Pull complete 
bd0ac81ef3f5: Pull complete 
f336e448fed0: Pull complete 
baa3d47d5d6d: Pull complete 
bc429966211c: Pull complete 
7b7d6c564677: Pull complete 
Digest: sha256:953e43ba087ba8cb694c61ceca3fe02b305f272aeb59ad0558854f99af0ed395
Status: Downloaded newer image for php:fpm
docker.io/library/php:fpm

2. 查看拉取的镜像

[root@client_01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php fpm 6339e6067153 4 days ago 499MB
mysql latest 73246731c4b0 3 weeks ago 619MB
nginx latest a8758716bb6a 2 months ago 187MB

二. 在容器中运行拉取的镜像

1. 运行nginx,mysql,php镜像

[root@client_01 ~]# docker run --name nginx -p 80:80 -d nginx
99658e99c4fef9a7087d65186672bb17001f78596aea87f2b5669e9b95ee9393
[root@client_01 ~]# docker run --name php-fpm -p 9000:9000 -d php:fpm 
1a2242d06621778287b6b506bdc5a47245d3b3ab817aa004056f7a6b4e0402f1
[root@client_01 ~]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
c924e3550e77a69e55ede206a7a1810559fee289addfd698870d99c9e31daf67

2. 查看运行的镜像

[root@client_01 ~]# docker ps
CONTAINER ID IMAGE  COMMAND                  CREATED              STATUS            PORTS                                                NAMES
c924e3550e77 mysql "docker-entrypoint.s…"    14 seconds ago       Up 13 seconds     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
1a2242d06621 php:fpm "docker-php-entrypoi…"  50 seconds ago       Up 49 seconds     0.0.0.0:9000->9000/tcp, :::9000->9000/tcp            php-fpm
99658e99c4fe nginx "/docker-entrypoint.…"    About a minute ago   Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp                    nginx

三. 在服务器创建项目目录,把 运行镜像的配置文件复制到创建的本地服务器目录

1. 创建项目目录

[root@client_01 ~]# mkdir -p nginx/conf 
[root@client_01 ~]# mkdir -p nginx/log 
[root@client_01 ~]# mkdir -p nginx/www

2. 把 运行镜像的配置文件复制到创建的本地服务器目录

复制nginx配置文件到本地服务器目录
[root@client_01 ~]# docker cp nginx:/etc/nginx/conf.d/default.conf /root/nginx/conf/default.conf
Successfully copied 3.07kB to /root/nginx/conf/default.conf
[root@client_01 ~]# docker cp nginx:/usr/share/nginx/html /root/nginx/www/
Successfully copied 4.1kB to /root/nginx/www/
[root@client_01 ~]# docker cp nginx:/var/log/nginx/ /root/nginx/log/
Successfully copied 2.56kB to /root/nginx/log/

复制php-fpm配置文件到本地服务器目录
root@client_01 ~]# docker ps      ##查看镜像ip
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c924e3550e77 mysql "docker-entrypoint.s…" 14 seconds ago Up 13 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
1a2242d06621 php:fpm "docker-php-entrypoi…" 50 seconds ago Up 49 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php-fpm
99658e99c4fe nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp nginx

[root@client_01 ~]# docker exec -it 1a2242d06621 /bin/bash    ##进入到正在运行的php-fpm容器中
root@1a2242d06621:/var/www/html#
root@1a2242d06621:/var/www/html# cd /usr/src/
root@1a2242d06621:/var/www/html# ls
php.tar.xz   php.tar.xz.asc
root@1a2242d06621:/usr/src# xz -d php.tar.xz  #解压php.tar.xz包为php.tar格式
root@1a2242d06621:/usr/src# ls 
php.tar php.tar.xz.asc
root@1a2242d06621:/usr/src# tar zvf php.tar  #再解压php.tar包
root@1a2242d06621:/usr/src# ls 
php-8.3.1 php.tar php.tar.xz.asc
root@1a2242d06621:/usr/src# exit   #退出容器

[root@client_01 ~]# docker cp php-fpm:/usr/local/etc/php-fpm.d/www.conf /root/nginx/conf/www.conf
Successfully copied 24.1kB to /root/nginx/conf/www.conf
[root@client_01 ~]# docker cp php-fpm:/usr/src/php-8.3.1/php.ini-production /root/nginx/conf/php.ini
Successfully copied 75.3kB to /root/nginx/conf/php.ini

四. 删除容器,修改从nginx , php-fpm容器中复制出来的配置文件

停止容器运行,删除容器

[root@client_01 ~]# docker stop nginx 
nginx
[root@client_01 ~]# docker rm nginx 
nginx
[root@client_01 ~]# docker stop php-fpm 
php-fpm
[root@client_01 ~]# docker rm php-fpm 
php-fpm
[root@client_01 ~]# docker ps
CONTAINER ID IMAGE  COMMAND               CREATED        STATUS        PORTS                                                NAMES
c924e3550e77 mysql "docker-entrypoint.s…" 55 minutes ago Up 55 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
修改本地服务器修改复制出来的nginx配置文件default.conf,使其支持php-fpm容器

[root@client_01 ~]# vim nginx/conf/default.conf

8 location / {
9     root /usr/share/nginx/html;
10    index index.html index.htm index.php;   ##添加php-fpm的测试页
11    }
.......
30 location ~ \.php$ {
31    root /usr/share/nginx/html;   ##修改成nginx容器里面默认发布目录的地址
32    fastcgi_pass php-fpm:9000;    ##修改成php-fpm容器的名称
33    fastcgi_index index.php;
34    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  ##$fastcgi_script_name; #脚本名称   
35    include fastcgi_params;
36    }

在本地服务器修改复制出来的php-fpm配置文件 php.ini 的内容,删除前面的注释,设置cgi.fix_pathinfo=0

[root@client_01 ~]# vim nginx/conf/php.ini

817 cgi.fix_pathinfo=0

在本地服务器修改复制出来的php-fpm配置文件 www.conf 的内容,修改其监听端口和地址

[root@client_01 ~]# vim nginx/conf/www.conf

41 listen = 0.0.0.0:9000

五. 映射本地服务器更改后的配置文件,重新启动容器

重新启动容器
[root@client_01 ~]# docker run --name php-fpm -p 9000:9000 --link mysql:mysql -v /root/nginx/www/html:/usr/share/nginx/html -v /root/nginx/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf -v /root/nginx/conf/php.ini:/usr/local/etc/php-8.3.1/php.ini -d php:fpm
a9d8416f3549ff4f0ac0b5c35f395ba48abe5a497c3dd0466310c233273230a9
[root@client_01 ~]# docker run --name nginx -p 80:80 --link php-fpm -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf -v /root/nginx/log:/var/log/nginx -d nginx
5c5c30bda256bafaef82c3c1e28017fbbe9431b1737900476659fcd8a2553f90
[root@client_01 ~]# docker ps
CONTAINER ID IMAGE   COMMAND                CREATED           STATUS           PORTS                                      NAMES
5c5c30bda256 nginx   "/docker-entrypoint.…" 5 seconds ago     Up 5 seconds     0.0.0.0:80->80/tcp, :::80->80/tcp          nginx
a9d8416f3549 php:fpm "docker-php-entrypoi…" 45 seconds ago    Up 45 seconds    0.0.0.0:9000->9000/tcp, :::9000->9000/tcp  php-fpm
c924e3550e77 mysql   "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql

六. 在本地创建php-fpm测试目录,在浏览器进行测试

测试nginx , ip

测试php-fpm , ip/index.php

 

 


云野 » Docker部署LNMP环境

1 评论

发表回复