今天来演示一下全容器化运行lnmp环境,使用的是centos7.6的操作系统,至于docker的安装就不再说了,docker已经装好了,直接开始。
1、nginx配置
首先要拉取nginx的镜像,如果有版本要求的可以去官方hub上找到具体的版本,然后根据提示来操作拉取,这里就以最新版nginx镜像为例:
[root@nginx ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14185 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1930 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 797 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 136
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 121
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 106 [OK]
bitnami/nginx Bitnami nginx Docker Image 92 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 82 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 76 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 65 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 46
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 44 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 27
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 16 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
flashspys/nginx-static Super Lightweight Nginx Image 8 [OK]
mailu/nginx Mailu nginx frontend 8 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 7 [OK]
bitwarden/nginx The Bitwarden nginx web server acting as a r… 7
wodby/nginx Generic nginx 1 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK]
[root@nginx ~]# docker pull nginx
[root@nginx ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 6 days ago 133MB
busybox latest 219ee5171f80 2 weeks ago 1.23MB
[root@nginx ~]#
nginx最新版的镜像已经拉下来了,busybox不用管它,之前下载来测试的,下面就启动它
[root@nginx ~]# docker run -dit -v /data/docker/web/:/data/web/ -v /data/docker/conf.d/:/etc/nginx/conf.d/ -p 8080:80 ae2feff98a0c
[root@nginx ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9f65eade813 ae2feff98a0c "/docker-entrypoint.…" 2 hours ago Up 1 minutes 0.0.0.0:8080->80/tcp brave_turing
8f5462c65a91 busybox "sh" 5 hours ago Up 5 hours thirsty_raman
[root@nginx ~]# netstat -tunlap| grep docker
tcp6 0 0 :::8080 :::* LISTEN 5341/docker-proxy
[root@nginx ~]#
来解释一下,-v是映射本地目录给容器内的地址,这样我们就不需要把网页文件或者nginx的配置文件再传到容器内了,只需要在本地修改就可以了,本地需要有这样的路径:
[root@nginx docker]# pwd
/data/docker
[root@nginx docker]# ls
conf.d web
[root@nginx docker]# cat web/index.html
docker nginx
[root@nginx docker]# cat conf.d/test1.conf
server {
listen 80;
root /data/web/;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 192.168.6.99:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/nginx/test-access.log main;
error_log /var/log/nginx/test-error.log debug;
}
[root@nginx docker]# 在nginx的配置文件中已经把php环境写好了,先用浏览器打开本机的8080端口验证nginx配置:
说明配置没问题,如果更改了nginx的配置文件,需要重启nginx有2种方法:
方法1,连接到nginx容器内部重新加载nginx
[root@nginx ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9f65eade813 ae2feff98a0c "/docker-entrypoint.…" 3 hours ago Up 5 minutes 0.0.0.0:8080->80/tcp brave_turing
8f5462c65a91 busybox "sh" 5 hours ago Up 5 hours thirsty_raman
[root@nginx ~]# docker exec -it a9f65eade813 bash
root@a9f65eade813:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@a9f65eade813:/# nginx -s reload
2020/12/22 09:53:49 [notice] 27#27: signal process started
root@a9f65eade813:/# exit
exit
[root@nginx ~]#
方法2、直接重启nginx容器:
[root@nginx ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9f65eade813 ae2feff98a0c "/docker-entrypoint.…" 3 hours ago Up 5 minutes 0.0.0.0:8080->80/tcp brave_turing
8f5462c65a91 busybox "sh" 5 hours ago Up 5 hours thirsty_raman
[root@nginx ~]# docker restart a9f65eade813
2、php配置
这里就要去官网找一下php-fpm的版本了,本次演示使用php7.2版本,首先找到php7.2-fpm的镜像:
使用的是这个版本,拉取下来
[root@nginx ~]# docker pull php:7.2.34-fpm
[root@nginx ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 6 days ago 133MB
php 7.2-fpm 28f52b60203d 10 days ago 398MB
busybox latest 219ee5171f80 2 weeks ago 1.23MB
[root@nginx ~]#
接下来启动它,php-fpm监听的是9000端口,而我演示环境中9000端口被占用了,用9001代替,在启动php-fpm容器时也是要映射目录的,映射的目录就是php文件所在的目录,和nginx相同
[root@nginx docker]# docker run -dit -v /data/docker/web/:/data/web/ -p 9001:9000 28f52b60203d
[root@nginx ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2968a9abe5df 28f52b60203d "docker-php-entrypoi…" 1 hours ago Up 1 minutes 0.0.0.0:9001->9000/tcp cranky_maxwell
a9f65eade813 ae2feff98a0c "/docker-entrypoint.…" 3 hours ago Up 10 minutes 0.0.0.0:8080->80/tcp brave_turing
8f5462c65a91 busybox "sh" 6 hours ago Up 6 hours thirsty_raman
[root@nginx ~]# netstat -tunlap| grep docker
tcp6 0 0 :::9001 :::* LISTEN 5157/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 5341/docker-proxy
[root@nginx ~]#
在/data/docker/web下面创建一个php文件,如下:
[root@nginx web]# pwd
/data/docker/web
[root@nginx web]# ls
index.php
[root@nginx web]# cat index.php
<?php
echo "this is docker php";
?>
[root@nginx web]#
然后来访问刚才的站点端口:
ok,能够正常访问。
3、mysql配置
同样的使用mysql5.7版本来演示,找到拉取地址后直接拉取:
[root@nginx ~]# docker pull mysql:5.7.32
[root@nginx ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.32 f07dfa83b528 14 hours ago 448MB
nginx latest ae2feff98a0c 6 days ago 133MB
php 7.2-fpm 28f52b60203d 10 days ago 398MB
busybox latest 219ee5171f80 2 weeks ago 1.23MB
[root@nginx ~]#
启动它,本地3306端口被占用,用3366来代替:
[root@nginx ~]# docker run -dit -p 3366:3306 -e MYSQL_ROOT_PASSWORD=123456 f07dfa83b528
[root@nginx ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c921151103df f07dfa83b528 "docker-entrypoint.s…" 47 minutes ago Up 1 minutes 33060/tcp, 0.0.0.0:3366->3306/tcp loving_curran
2968a9abe5df 28f52b60203d "docker-php-entrypoi…" 2 hours ago Up 1 hours 0.0.0.0:9001->9000/tcp cranky_maxwell
a9f65eade813 ae2feff98a0c "/docker-entrypoint.…" 3 hours ago Up 1 hours 0.0.0.0:8080->80/tcp brave_turing
8f5462c65a91 busybox "sh" 6 hours ago Up 6 hours thirsty_raman
[root@nginx ~]# netstat -tunlap| grep docker
tcp6 0 0 :::9001 :::* LISTEN 5157/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 5341/docker-proxy
tcp6 0 0 :::3366 :::* LISTEN 6501/docker-proxy
[root@nginx ~]#
可以看到mysql已经顺利起来了,启动mysql也可以映射目录,如果映射目录的话可以这样操作:
[root@nginx ~]# docker run -p 3366:3306 -dit \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 f07dfa83b528
[root@nginx ~]#
映射目录的好处也是将文件放在本地,便于修改和备份。
-e MYSQL_ROOT_PASSWORD=123456这个参数的作用是指定root密码为123456,启动完成后可以通过指定的端口用户名密码连接了,演示到此。