源码gitee
compose 使用的三个步骤:
• 使用 Dockerfile 定义应用程序的环境。
• 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。
• 最后,执行 docker-compose up -d 命令来启动并运行整个应用程序。
这里说明一下:mysql 待续
下面先展示如下存在的目录及文件结构
docker-compose.yml 文件内容
version: "1.0"
services:redis:image: redis5.0.12:v1container_name: lnmp_redisbuild:context: ./redisdockerfile: Dockerfilevolumes:- ./redis/conf:/etc/redis- ./redis/logs:/usr/local/redis/logs- ./redis/data:/usr/local/redis/dataenvironment:- TZ=Asia/Shanghaiports:- "6379:7379"networks:- backendphp: image: php8:v1container_name: lnmp_php8build: context: ./phpdockerfile: Dockerfiledepends_on:- "redis"volumes:- ./php/project:/data/wwwroot/project- ./php/logs:/usr/local/php/logsenvironment:- TZ=Asia/Shanghaiports:- "9000:9000"networks:- frontend- backendnginx:image: nginx1.22:v1container_name: lnmp_nginxbuild:context: ./nginxdockerfile: Dockerfiledepends_on:- "php"volumes:- ./nginx/html:/data/wwwroot/html- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.confenvironment:- TZ=Asia/Shanghaiports:- "80:80"- "443:443"networks:- frontend
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/htmlCOPY ./nginx.conf /usr/local/nginx/conf/nginx.confCOPY ./html/index.html /data/wwwroot/html# 声明要把容器的哪些端口映射到宿主机
EXPOSE 80 433COPY run.sh /etc/run.sh
RUN chmod +x /etc/run.shENTRYPOINT ["/etc/run.sh"]
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>ceshinginx镜像测试文件
</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-develRUN yum install -y libwebp-develRUN 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/projectCOPY ./project/index.php /data/wwwroot/project/EXPOSE 9000
COPY run.sh /etc/run.sh
RUN chmod +x /etc/run.sh# 启动
ENTRYPOINT ["/etc/run.sh"]
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.logtail -f /dev/null
执行docker-compose 命令
docker-compose up -d
php新增redis扩展
在php Dockerfile文件内容后面新增如下内容:# 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
然后使用 docker-compose up -d 重新镜像,在php 配置中就能看到redis 扩展了
后续…