windows 10 配置 docker lnmp 开发环境

Docker/容器
427
0
0
2022-05-29
标签   Docker

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();