源码gitee
compose 使用的三个步骤:
• 使用 Dockerfile 定义应用程序的环境。
• 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。
• 最后,执行 docker-compose up -d 命令来启动并运行整个应用程序。
下面先展示如下存在的目录及文件结构
docker-compose.yml 文件内容
version: "1.0"
services:
redis:
image: redis5.0.12:v1
container_name: lnmp_redis
build:
context: ./redis
dockerfile: Dockerfile
volumes:
- ./redis/conf:/etc/redis
- ./redis/logs:/usr/local/redis/logs
- ./redis/data:/usr/local/redis/data
environment:
- TZ=Asia/Shanghai
ports:
- "6379:7379"
networks:
- backend
php:
image: php8:v1
container_name: lnmp_php8
build:
context: ./php
dockerfile: Dockerfile
depends_on:
- "redis"
- "mysql"
volumes:
- ./php/project:/data/wwwroot/project
- ./php/logs:/usr/local/php/logs
environment:
- TZ=Asia/Shanghai
ports:
- "9000:9000"
networks:
- frontend
- backend
nginx:
image: nginx1.22:v1
container_name: lnmp_nginx
build:
context: ./nginx
dockerfile: Dockerfile
depends_on:
- "php"
volumes:
- ./nginx/html:/data/wwwroot/html
environment:
- TZ=Asia/Shanghai
ports:
- "80:80"
- "443:443"
networks:
- frontend
mysql:
image: mysql8.0.32:v1
container_name: lnmp_mysql
build:
context: ./mysql
dockerfile: Dockerfile
environment:
- TZ=Asia/Shanghai
ports:
- "3306:3306"
networks:
- backend
networks:
frontend:
backend:
nginx中Dockerfile文件内容
# 拉取 CentOS
FROM centos:7.8.2003
# 作者信息
MAINTAINER "LuNan"
# 下载相关依赖
RUN yum update -y \
&& yum install -y vim git wget gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
# 创建用户
RUN useradd www
# 设置 nginx 版本
ENV NGINX_VERSION 1.22.0
# 下载安装及编译
RUN wget http://nginx.org/download/nginx-1.22.0.tar.gz \
&& tar -zxf nginx-$NGINX_VERSION.tar.gz \
&& cd nginx-$NGINX_VERSION \
&& ./configure --prefix=/usr/local/nginx --with-http_sub_module --with-http_ssl_module && make && make install
# 配置
RUN mkdir -p /data/wwwroot/html
COPY ./conf/nginx.conf /usr/local/nginx/conf/nginx.conf
COPY ./html/index.html /data/wwwroot/html
# 声明要把容器的哪些端口映射到宿主机
EXPOSE 80 433
COPY run.sh /etc/run.sh
RUN chmod +x /etc/run.sh
ENTRYPOINT ["/etc/run.sh"]
nginx中nginx.conf 配置文件变更
user www;
worker_processes auto;
location / {
root /data/wwwroot/html;
index index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass lnmp_php8:9000; # lnmp_php8指的是php容器名称
fastcgi_index index.php;
# /data/wwwroot/project/ 为php容器中的项目目录,可以看一下php中的Dockerfile文件
fastcgi_param SCRIPT_FILENAME /data/wwwroot/project/$fastcgi_script_name;
include fastcgi_params;
}
nginx中run.sh文件内容
#!/bin/bash
/usr/local/nginx/sbin/nginx -g "daemon off;"
tail -f /dev/null
nginx中html目录下的index.html文件内容
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
ceshi
nginx镜像测试文件
</body>
</html>
php中Dockerfile文件内容
# 拉取 CentOS
FROM centos:7.8.2003
# 作者信息
MAINTAINER "LuNan"
# 下载相关依赖
RUN yum update -y \
&& yum install -y vim git wget gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel libcurl-devel
RUN yum install -y libwebp-devel
RUN yum install -y http://down.24kplus.com/linux/oniguruma/oniguruma-6.7.0-1.el7.x86_64.rpm \
http://down.24kplus.com/linux/oniguruma/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
# 保证nginx 和php 用户相同
RUN groupadd www && useradd -g www www
# 设置 php 版本
ENV PHP_VERSION 8.2.8
#下载及编译
RUN cd /opt \
&& wget https://www.php.net/distributions/php-$PHP_VERSION.tar.gz --no-check-certificate \
&& tar -zxf php-$PHP_VERSION.tar.gz \
&& cd php-$PHP_VERSION \
&& ./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-openssl \
--enable-fpm \
--enable-gd \
--with-curl \
--with-zlib \
--with-jpeg \
--with-webp=/usr/local/include/webp \
--with-freetype=/usr/local/freetype \
--without-sqlite3 \
--without-pdo-sqlite \
--disable-rpath \
--enable-mbstring \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--with-fpm-user=www \
--with-fpm-group=www \
&& make && make install
# 执行shell命令
RUN cp /opt/php-$PHP_VERSION/php.ini-development /usr/local/php/etc/php.ini \
&& cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \
&& cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \
&& sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf \
&& sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.d/www.conf \
&& mkdir -p /data/wwwroot/project
COPY ./project/index.php /data/wwwroot/project/
COPY ./project/mysql.php /data/wwwroot/project/
EXPOSE 9000
COPY run.sh /etc/run.sh
RUN chmod +x /etc/run.sh
# 启动
ENTRYPOINT ["/etc/run.sh"]
# redis 扩展版本
ENV REDIS_PECL_VERSION 5.3.7
# 新增redis扩展
RUN cd /opt \
&& wget https://pecl.php.net/get/redis-$REDIS_PECL_VERSION.tgz \
&& tar -zxvf redis-$REDIS_PECL_VERSION.tgz \
&& cd redis-$REDIS_PECL_VERSION \
&& /usr/local/php/bin/phpize \
&& ./configure -with-php-config=/usr/local/php/bin/php-config \
&& make && make install \
&& sed -i "90a \extension=redis.so" /usr/local/php/etc/php.ini
php中project目录下的index.php 文件内容
<?php
echo "ce";
echo "ceshi";
echo phpinfo();
php中run.sh 文件内容
#!/bin/bash
/usr/local/php/sbin/php-fpm
tail -f /dev/null
redis中Dockerfile文件内容
# 拉取 CentOS
FROM centos:7.8.2003
# 维护者
MAINTAINER lunan
# 设置 Redis 版本
ENV REDIS_VERSION 5.0.12
# 下载依赖
RUN yum update -y \
&& yum install -y vim wget gcc gcc-c++ make perl tar
# 下载包和编译
RUN cd /opt \
&& wget http://download.redis.io/releases/redis-$REDIS_VERSION.tar.gz \
&& tar -zxf redis-$REDIS_VERSION.tar.gz \
&& cd redis-$REDIS_VERSION \
&& make && make PREFIX=/usr/local/redis install
# 相关配置
RUN cd /opt/redis-$REDIS_VERSION \
&& mkdir -p /usr/local/redis/{logs,data} \
&& mkdir -p /etc/redis && chmod -R 755 /etc/redis
#&& chmod +x redis.conf \
#&& mv redis.conf /etc/redis/redis.conf \
#&& sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /etc/redis/redis.conf \
#&& sed -i "s/daemonize no/daemonize yes/g" /etc/redis/redis.conf
# 复制本地文件到容器中
COPY ./conf/redis.conf /etc/redis/redis.conf
# 设置端口
EXPOSE 6379
# 脚本运行
COPY run.sh /etc/run.sh
RUN chmod +x /etc/run.sh
ENTRYPOINT ["/etc/run.sh"]
redis中conf目录下的redis.conf变更
bind 0.0.0.0
daemonize yes
redis中run.sh文件内容
#!/bin/bash
/usr/local/redis/bin/redis-server /etc/redis/redis.conf > /usr/local/redis/logs/redis.log
tail -f /dev/null
执行docker-compose 命令
docker-compose up -d
检查各个进程状态
访问各个相关页面
-
nginx默认页面
-
php默认页面
-
php redis扩展信息
mysql 相关配置
mysql未配置之前页面,出现的错误:
首先配置mysql相关授权:
进入mysql 容器: docker exec -it lnmp_mysql bash,密码在之前日志文件中/usr/local/mysql/error.log,进入到mysql 命令行中,执行如下操作:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; # 重新设置密码
create user 'lunan'@'%' identified by 'root' ; # 新增lunan用户
grant all privileges on *.* to lunan@'%'; # 授权
flush privileges; # 刷新
需要更改php容器中,mysql.php 文件的相关信息:
需要注意的是$host 变量: 为mysql的ip ,可以通过如下命令查看:
然后再次访问mysql.php 文件:
文章来源:https://www.toymoban.com/news/detail-622495.html
至此docker-compose 安装 lnmpr已完成!!!文章来源地址https://www.toymoban.com/news/detail-622495.html
到了这里,关于使用docker-compose搭建lnmpr环境的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!