合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
>[info] 构建 lnmp 项目 php7.4 下载: https://www.php.net/distributions/php-7.4.33.tar.gz nginx 1.22.0 下载: https://nginx.org/download/nginx-1.22.0.tar.gz mysql 5.7 下载: https://downloads.mysql.com/archives/community/ https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.40-el7-x86_64.tar.gz >[info] 建立工作目录 ```console mkdir -p /home/{nginx,php,mysql} ``` >[info] 构建 自定义 network ```console docker network create --driver bridge --subnet=172.18.0.0/16 --gateway=172.18.0.1 mynet-lnmp ``` ![](https://img.kancloud.cn/b9/e8/b9e88ac219ab2ccae4716627c3bda9c4_760x181.png) >[info] Nginx 1. 编写Dockerfile配置文件(尽可能把命令放在一行,减少镜像层数,缩小镜像内存) ~~~ # 从 centos 镜像中构建应用 FROM centos:7 # 设置镜像的制作者 MAINTAINER wangqianshun<420029810@qq.com # 安装相关依赖 RUN yum install -y gcc gcc-c++ make gd-devel pcre-devel zlib-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel;useradd -M -s /sbin/nologin nginx # 上一条命令安装nginx和fpm # nginx 默认安装路径 /etc/nginx/ # php-fpm 默认安装路径 /etc/php/fpm/ # nginx配置文件存放目录 /etc/nginx/conf.d/ # nginx默认项目路径 /var/www/html/(/usr/share/nginx/html) # 添加 nginx 压缩包 自动解压 ADD ./tmp/tar/nginx-1.22.0.tar.gz /usr/local/src # 工作目录 WORKDIR /usr/local/src/nginx-1.22.0 # nginx编译安装 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module;make -j 2 && make install # nginx sbin路径 ENV PATH /usr/local/nginx/sbin:$PATH # 复制nginx配置文件 COPY ./config/nginx.conf /usr/local/nginx/conf/default.conf # 网站项目目录 COPY ./html/ /usr/local/nginx/html/ # 设置项目权限 删除解压编译安装的文件夹 RUN chmod 777 -R /usr/local/nginx/html/ && rm -rf /usr/local/src/nginx-1.22.0 # 暴露端口 EXPOSE 80 # 声明了容器中的目录作为匿名卷,但是并没有将匿名卷绑定到宿主机指定目录的功能。 VOLUME [ "/usr/local/nginx/html/" ] # 容器运行时启动应用 CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ] ~~~ 2. 编写 `nginx.conf` ~~~console worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; charset utf-8; location / { root /usr/local/nginx/html; index index.php index.html; } location ~ \.php$ { fastcgi_pass 192.168.244.10:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ~~~ 3. 通过 `Dockerfile` 构建镜像 ``` docker build -f Dockerfile -t nginx:lnmp . ``` ![](https://img.kancloud.cn/f7/0b/f70bbdc627f2a29ad58e7e94c4917349_1018x130.png) 4. 启动容器 ``` # 启动nginx容器并且为容器指定IP地址 docker run -d --name nginx -p 80:80 -v /home/docker/lnmp/nginx/config:/usr/local/nginx/conf --net mynet-lnmp --ip 172.110.0.10 nginx:lnmp docker run -itd --name nginx -p 80:80 -v /home/nginx/html:/usr/share/nginx/html -v /home/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf nginx:1.18 ``` ``` docker rm -f $(docker ps -aq) docker stop nginx && docker rm nginx && docker rmi nginx:lnmp docker run -itd --name nginx nginx:lnmp && docker exec -it nginx /bin/bash ```