源码gitee
• 使用 Dockerfile 定义应用程序的环境。
• 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。
• 最后,执行 docker-compose up -d 命令来启动并运行整个应用程序。
这里说明一下:mysql 待续
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"
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
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
environment:
- TZ=Asia/Shanghai
ports:
- "80:80"
- "443:443"
networks:
- frontend
networks:
frontend:
backend:
# 拉取 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 ./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"]
#!/bin/bash
/usr/local/nginx/sbin/nginx -g "daemon off;"
tail -f /dev/null
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>
# 拉取 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/
EXPOSE 9000
COPY run.sh /etc/run.sh
RUN chmod +x /etc/run.sh
# 启动
ENTRYPOINT ["/etc/run.sh"]
echo "ce";
echo "ceshi";
echo phpinfo();
#!/bin/bash
/usr/local/php/sbin/php-fpm
tail -f /dev/null
# 拉取 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"]
bind 0.0.0.0
daemonize yes
#!/bin/bash
/usr/local/redis/bin/redis-server /etc/redis/redis.conf > /usr/local/redis/logs/redis.log
tail -f /dev/null
docker-compose up -d
在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 扩展了
后续…