windows 10 配置 docker lnmp 開發環境

charliecen發表於2021-08-06

win10 部署 docker lnmp 開發環境

windows 安裝 docker

這步省略,網上很多。自己安裝

下載地址

安裝LNMP

使用倉庫安裝,或者匯入,都可以

php Dockerfile
FROM centos:7
MAINTAINER www.aliangedu.com
RUN yum -y install wget
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all && yum makecache

RUN yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libmcrypt libmcrypt-devel autoconf

WORKDIR /tmp
ADD https://www.php.net/distributions/php-7.2.34.tar.gz /tmp/
RUN tar zxvf /tmp/php-7.2.34.tar.gz
WORKDIR  /tmp/php-7.2.34
RUN ls -al
RUN ./configure \
--prefix=/usr/local/php \
--with-mcrypt=/usr/local/libmcrypt \
--with-zlib \
--enable-mbstring \
--with-openssl \
--with-mysql \
--with-mysqli \
--with-mysql-sock \
--with-gd \
--with-jpeg-dir=/usr/lib \
--enable-gd-native-ttf  \
--enable-pdo \
--with-pdo-mysql \
--with-gettext \
--with-curl \
--with-pdo-mysql \
--enable-sockets \
--enable-bcmath \
--enable-xml \
--with-bz2 \
--enable-zip \
--enable-freetype \
--enable-opcache \
--enable-fpm

RUN make && make install

RUN ln -s /usr/local/php/bin/pear /usr/bin/pear
RUN ln -s /usr/local/php/bin/pecl /usr/bin/pecl
RUN ln -s /usr/local/php/bin/php /usr/bin/php
RUN ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
RUN ln -s /usr/local/php/bin/phpize /usr/bin/phpize

RUN cp ./php.ini-development /usr/local/php/lib/php.ini
RUN cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
RUN cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
RUN sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.d/www.conf
RUN sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf

WORKDIR /usr/local/php

EXPOSE 9000
CMD php-fpm
nginx Dockerfile
# nginx Dockerfile
# Version 1.0
# author fendo

# Base images 基礎映象
FROM centos:centos7

#安裝相關依賴
RUN yum -y install  gcc gcc-c++ autoconf automake make
RUN yum -y install  zlib zlib-devel openssl* pcre* wget lua-devel
RUN yum -y install unzip zip

RUN mkdir -p /etc/nginx/lua


#安裝lua-waf
#WORKDIR  /tmp
#ADD https://github.com/loveshell/ngx_lua_waf/archive/master.zip /tmp/
#RUN unzip master.zip
#RUN cp -r ngx_lua_waf-master /etc/nginx/waf

#安裝lua
#WORKDIR  /tmp
#ADD https://github.com/agentzh/lua-resty-memcached/archive/v0.11.tar.gz /tmp/
#RUN tar zxf v0.11.tar.gz
#RUN cp -r ./lua-resty-memcached-0.11/lib/resty/memcached.lua  /etc/nginx/lua/
#WORKDIR  /tmp
#ADD https://github.com/LuaJIT/LuaJIT/archive/v2.0.5.tar.gz /tmp/
#RUN tar zxf v2.0.5.tar.gz
#WORKDIR  /tmp/LuaJIT-2.0.5
#RUN make PREFIX=/usr/local/luajit
#RUN make install PREFIX=/usr/local/luajit
#WORKDIR  /tmp
##ADD https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz /tmp/
#RUN tar -xzvf v0.3.0.tar.gz
#RUN cp -r ngx_devel_kit-0.3.0/ /usr/local/src/
#WORKDIR  /tmp
#ADD https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz /tmp/
#RUN tar -xzvf v0.10.13.tar.gz
#RUN cp -r lua-nginx-module-0.10.13/ /usr/local/src/
#設定環境變數
#RUN export LUAJIT_LIB=/usr/local/lib
#RUN export LUAJIT_INC=/usr/local/include/luajit-2.0




#nginx ngx_cache_purge模組
#ADD http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz  /tmp/
#WORKDIR  /tmp
#RUN tar -xzvf ngx_cache_purge-2.3.tar.gz
#RUN cp -r ngx_cache_purge-2.3/ /usr/local/src/



#nginx echo模組
#ADD https://github.com/openresty/echo-nginx-module/archive/v0.62.tar.gz  /tmp/
#WORKDIR  /tmp
#RUN tar -xzvf v0.62.tar.gz
#RUN cp -r echo-nginx-module-0.62/ /usr/local/src/


#nginx check模組
#ADD https://github.com/yaoweibin/nginx_upstream_check_moduLe/archive/master.zip  /tmp/
#WORKDIR  /tmp
#RUN unzip master.zip
#RUN cp -r nginx_upstream_check_module-master/ /usr/local/src/

#nginx安裝
WORKDIR  /tmp
ADD http://nginx.org/download/nginx-1.19.4.tar.gz /tmp/
RUN useradd -M -s /sbin/nologin nginx
RUN tar -zxvf nginx-1.19.4.tar.gz
RUN mkdir -p /usr/local/nginx
RUN mkdir -p /var/cache/nginx
RUN cd /tmp/nginx-1.19.4 \
    && ./configure \
    --prefix=/etc/nginx \
       --sbin-path=/usr/sbin/nginx\
       --modules-path=/usr/lib/nginx/modules \
       --conf-path=/etc/nginx/nginx.conf \
       --error-log-path=/log/error.log \
       --http-log-path=/log/access.log \
       --pid-path=/var/run/nginx.pid \
       --lock-path=/var/run/nginx.lock \
       --http-client-body-temp-path=/var/cache/nginx/client_temp \
       --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
       --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
       --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
       --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
       --user=nginx \
       --group=nginx \
       --with-compat \
       --with-file-aio \
       --with-threads \
       --with-http_addition_module \
       --with-http_auth_request_module \
       --with-http_dav_module \
       --with-http_flv_module \
       --with-http_gunzip_module \
       --with-http_gzip_static_module \
       --with-http_mp4_module \
       --with-http_random_index_module \
       --with-http_realip_module \
       --with-http_secure_link_module \
       --with-http_slice_module \
       --with-http_ssl_module \
       --with-http_stub_status_module \
       --with-http_sub_module \
       --with-http_v2_module \
       --with-mail \
       --with-mail_ssl_module \
       --with-stream \
       --with-stream_realip_module \
       --with-stream_ssl_module \
       --with-stream_ssl_preread_module \
       #--add-module=/usr/local/src/lua-nginx-module-0.10.13 \
        #--add-module=/usr/local/src/ngx_devel_kit-0.3.0 \
        #--add-module=/usr/local/src/ngx_cache_purge-2.3 \
        #--add-module=/usr/local/src/echo-nginx-module-0.62 \
        #--add-module=/usr/local/src/nginx_upstream_check_module-master \
       --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.4/debian/debuild-base/nginx-1.19.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \
    && make && make install


#對映埠
EXPOSE 80
ADD ./cp/nginx.conf /etc/nginx/nginx.conf
RUN mkdir /etc/nginx/conf.d && mkdir /www
ADD ./nginx/conf/default.conf /etc/nginx/conf.d


#CMD 執行以下命令
CMD ["nginx","-g","daemon off;"]
mysql Dockerfile
# mysql Dockerfile
# Version 1.0
# author fendo

# Base images 基礎映象
FROM centos:centos7

#安裝相關依賴

#yum
RUN yum -y install  libaio-devel.x86_64 numactl.x86_64
RUN yum -y install openssl openssl-devel



#pt工具安裝
#WORKDIR  /usr/local/src
#RUN yum install -y gcc
#RUN yum install -y perl-CPAN perl-Time-HiRes perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker cpan perl-Digest-MD5 perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL
#ADD http://www.percona.com/redir/downloads/percona-toolkit/2.1.1/percona-toolkit-2.1.1.tar.gz /usr/local/src
#RUN tar zxvf percona-toolkit-2.1.1.tar.gz
#WORKDIR /usr/local/src/percona-toolkit-2.1.1
#RUN perl Makefile.PL PREFIX=/usr/local/percona-toolkit
#RUN make && make install




RUN useradd mysql
WORKDIR  /usr/local/src
#ADD https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz /usr/local/src
ADD mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz /usr/local/src
#RUN tar zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
RUN mv /usr/local/src/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql

RUN cd /usr/local/mysql
RUN mkdir -p /usr/local/mysql/data
RUN mkdir -p /var/lib/mysql
RUN echo "">/etc/my.cnf
#初始化資料庫
RUN /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2>/tmp/pass
RUN /usr/local/mysql/bin/mysql_ssl_rsa_setup  --datadir=/usr/local/mysql/data

#安裝pbk
#RUN yum -y install numactl
#ADD https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/tarball/percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz /usr/local/src
#ADD percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz /usr/local/src
#RUN tar zxvf percona-xtrabackup-2.4.9-Linux-x86_64.tar.gz
#RUN mv percona-xtrabackup-2.4.9-Linux-x86_64 /usr/local/xtrabackup
#RUN ln -s /usr/local/xtrabackup/bin/* /usr/bin/

MAINTAINER wjj wjj@qq.com
RUN chown -R mysql.mysql /usr/local/mysql/data
ENV PATH $PATH:/usr/local/mysql/bin
RUN  echo "passwd=\"`cat /tmp/pass |grep root@localhost:|awk '{print $11}'`\"" >>/usr/local/src/init.sh

RUN  echo "mysql -uroot -p\$passwd --connect-expired-password  -e \"alter user user() identified by 'root'\";" >>/usr/local/src/init.sh
RUN  echo "mysql -uroot -proot -e \"update mysql.user set Host='%' where User='root' limit 1;\";" >>/usr/local/src/init.sh
RUN  echo "mysql -uroot -proot -e \"flush privileges\";" >>/usr/local/src/init.sh

#對映埠
EXPOSE 3306

#CMD 執行以下命令
CMD /usr/local/mysql/bin/mysqld_safe

#執行命令
#docker build -f dockerfile  -t mysql:1  ./
#docker run --name mysql1 --hostname mysql1 --ip 172.17.0.2 -p 3306:3306   -v mysql_conf:/etc  -v mysql_data:/usr/local/mysql  -itd mysql:1
redis Dockerfile
FROM centos:centos7

#安裝依賴
RUN yum -y install tcl gcc automake autoconf libtool make 
#RUN yum -y install centos-release-sc1 
RUN rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
RUN yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
RUN scl enable devtoolset-9 bash
#RUN "source /opt/rh/devtoolset-9/enable" >> /etc/profile && gcc -v

WORKDIR  /tmp
#ADD http://download.redis.io/releases/redis-5.0.9.tar.gz /tmp/
ADD redis-5.0.9.tar.gz /tmp/
# PREFIX=/usr/local/redis/ 
# WORKDIR /tmp/redis-5.0.9 #&& make && make install PREFIX=/usr/local/redis
RUN cd /tmp/redis-5.0.9 && make && make install PREFIX=/usr/local/redis
ADD ./redis.conf  /usr/local/redis/
RUN cp /usr/local/redis/bin/* /usr/bin/
WORKDIR /usr/local/src



#對映埠
EXPOSE 6379

#CMD 執行以下命令
CMD redis-server /usr/local/redis/redis.conf

構建映象

# 當前目錄建立映象
$ docker build -t centos/php:7.2 -f .\docker-php .
$ docker build -t centos/nginx:latest -f .\docker-nginx .
$ docker build -t centos/mysql:5.7 -f .\docker-mysql .
$ docker build -t centos/redis:5.0 -f .\docker-redis .

# 檢視映象
❯ docker images
REPOSITORY            TAG              IMAGE ID       CREATED        SIZE
centos/mysql          5.7              4401a0a40b1d   3 days ago     3.74GB
centos/php            7.2              cdb3f044dabf   3 days ago     1.72GB
centos/nginx          latest           c25a3e671855   3 days ago     765MB
redis                 5.0              9b58edd0f903   6 weeks ago    1.17GB

建立容器

首先需要docker-compose命令, 自己安裝

下面是docker-compose.yaml檔案

version : "3" #docker-compose的版本
services: #容器的集合
  mysql: #專案名稱
    #build:
    #  dockerfile: docker-mysql
    #  context: ./
    image: centos/mysql:5.7 #映象名稱,如果是通過dockerfile建立的可以使用build屬性
    container_name: mysql  #容器名稱,如果沒有這個屬性的話,docker-compose會隨機分配一個名字給容器
    privileged: true    #允許操作的表示,如果不加的話會出現類似,permission deny的錯誤
    ports:
      - 3306:3306  #開放宿主機和容器的對映埠
    environment:
      MYSQL_ROOT_PASSWORD: root  #mysql映象中的環境變數
    volumes:
      - mysql_data:/usr/local/mysql/data
  php:
    #build:
    #  dockerfile: docker-php
    #  context: ./
    image: centos/php:7.2
    container_name: php
    privileged: true
    ports:
      - 9000:9000
    volumes:
      - C:/web:/www  #掛載卷,需要注意的是,php在以模組的形式載入到nginx的時候,需要他們兩個的目錄結構一致,否則nginx無法載入php,但是 html等靜態問價可以正常訪問。
    environment:
      APP_ENV: dev
  #php74:
  #  image: php:7.4
  #  container_name: php74
  #  privileged: true
  #  ports:
  #    - 9074:9000
  #  volumes:
  #    - ./nginx/www:/www
  #  environment:
  #    APP_ENV: test
  #php7325:
  #  image: php:7.3.25
  #  container_name: php7325
  #  privileged: true
  #  ports:
  #    - 9075:9000
  #  volumes:
  #    - ./nginx/www:/www  
  #  environment:
  #    APP_ENV: test
  nginx:
    #build:
    #  dockerfile: docker-nginx
    #  context: ./
    image:  nginx:latest
    container_name: nginx
    privileged: true
    restart: always
    ports:
      - 80:80
      - 81:81
      - 82:82
      - 83:83
      - 84:84
      - 85:85
      - 86:86
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d  # 當前目錄影射到容器中
      - ./nginx/log:/log
      - C:/web:/www
  redis:
    image: redis:5.0
    container_name: redis
    privileged: true
    ports:
      - 6379:6379
volumes:
    mysql_data: {}

執行命令

docker-compose up -d

# 檢視容器
❯ docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED      STATUS       PORTS                                           NAMES
b4790c8c7068   nginx:latest          "/docker-entrypoint.…"   2 days ago   Up 9 hours   0.0.0.0:80-86->80-86/tcp, :::80-86->80-86/tcp   nginx
75224b173b86   centos/php:7.2        "/bin/sh -c php-fpm"     2 days ago   Up 9 hours   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp       php
6e499aadff0a   centos/mysql:5.7      "/bin/sh -c /usr/loc…"   2 days ago   Up 9 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp       mysql
a401b297440e   redis:5.0             "/bin/sh -c 'redis-s…"   2 days ago   Up 9 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp       redis

進入容器配置

# 進入容器
> docker exec -it php bash
# 安裝redis擴充套件
[root@75224b173b86 php]# cd /usr/local/src/
[root@75224b173b86 src]# ls
package.xml  redis-5.3.2  redis-5.3.2.tgz
[root@75224b173b86 src]# cd redis-5.3.2
[root@75224b173b86 src]# phpize
[root@75224b173b86 src]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@75224b173b86 src]# make && make install
[root@75224b173b86 src]# php -i |grep redis


# 配置redis密碼,監聽地址
> docker exec -it redis bash
vim redis.conf
requirepass root
bind 0.0.0.0

# 初始化mysql,設定密碼
建立nginx配置

當前目錄./nginx/conf/default.conf, 此目錄就是nginx dockerfile 對映的目錄

server {
    listen       80;
    server_name www.test1.com;
    root /www/test;
    location / {
        index index.php  index.html index.htm;
    }
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

測試

在目錄c:/web/建立一個index.php

<?php
phpinfo();

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章