首先安装compose

 

#下载并移动到/usr/local/bin目录下
curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
#赋予可执行权限
chmod +x /usr/local/bin/docker-compose

 

 然后执行docker-compose --version 如果能看显示对应的版本号说明安装成功

 

再一个空目录下创建nginx 和php两个目录,首先进入nginx目录

cd nginx
vi Dockerfile

 

将代码打包移动到nginx目录下,如:

tar czvf docker.tar docker #打包
mv docker.tar /web/nginx/ #移动文件到nginx文件下

 

创建Dockerfile文件且写入如下指令

FROM nginx:latest
COPY default.conf /etc/nginx/conf.d/default.conf
COPY fastcgi_params /etc/nginx/conf.d/fastcgi
ADD  docker.tar /usr/share/nginx/  #打包代码的文件

保存退出创建default.conf写入以下指令

server {
    listen       80;
    server_name  zhuang.top; #自定义域名
    root   /usr/share/nginx/html/docker/public/;
    index  index.html index.htm index.php;
    location ~ \.php$ {
        fastcgi_pass   php-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/docker/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

保存退出再创建fastcgi_params 写入以下指令

#fastcgi相关参数开始
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param  QUERY_STRING       $query_string;
            fastcgi_param  REQUEST_METHOD     $request_method;
            fastcgi_param  CONTENT_TYPE       $content_type;
            fastcgi_param  CONTENT_LENGTH     $content_length;

            fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            fastcgi_param  REQUEST_URI        $request_uri;
            fastcgi_param  DOCUMENT_URI       $document_uri;
            fastcgi_param  DOCUMENT_ROOT      $document_root;
            fastcgi_param  SERVER_PROTOCOL    $server_protocol;
            fastcgi_param  REQUEST_SCHEME     $scheme;
            fastcgi_param  HTTPS              $https if_not_empty;

            fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

            fastcgi_param  REMOTE_ADDR        $remote_addr;
            fastcgi_param  REMOTE_PORT        $remote_port;
            fastcgi_param  SERVER_ADDR        $server_addr;
            fastcgi_param  SERVER_PORT        $server_port;
            fastcgi_param  SERVER_NAME        $server_name;
            fastcgi_param  REDIRECT_STATUS    200;
            fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/../:/tmp/:/proc/";
            #fastcgi相关参数结束

            #pathinfo开始
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO       $path_info;
            try_files $fastcgi_script_name =404;
            #pathinfo结束

保存退出 进入php目录

cd ../php

再次创建dockerfile

vi dockerfile

并键入以下指令

FROM php:7.3-fpm
RUN docker-php-ext-install pdo pdo_mysql mysqli \
&& pecl install redis-4.2.0 && docker-php-ext-enable redis

 

保存退出回到外层目录

cd ..

 

创建docker-compose.yml且写入以下

version: '3'
networks:
  dev-net:
    driver: bridge
services:
 mysql:
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
    networks:
     - dev-net
 nginx:
    build: nginx
    restart: always
    ports:
      - 80:80
    volumes:
      - web_data:/usr/share/nginx/html
    command:
      - /bin/bash
      - -c
      - |
       mv /usr/share/nginx/docker/ /usr/share/nginx/html/docker
       nginx -g 'daemon off;'
    depends_on:
      - php-fpm
    networks:
     - dev-net
 php-fpm:
    build: php
    volumes:
    - web_data:/usr/share/nginx/html
    networks:
     - dev-net
 redis:
    image: redis:latest
    networks:
     - dev-net
volumes:
   web_data:

保存退出后执行

docker-compose up -d

等待完成自动构建即可

 

如果中途报这样的错:

ERROR: Service 'php-fpm' failed to build: The command '/bin/sh -c docker-php-ext-install pdo pdo_mysql mysqli && pecl install redis-4.2.0 && docker-php-ext-enable redis' returned a non-zero code: 1

进入php目录下的dockerfile文件

cd /php/dockerfile/

将以下代码删除

&& pecl install redis-4.2.0 && docker-php-ext-enable redis

保存退出,到外层目录再次执行

docker-compose up -d

等待完成即可,如

Creating web_mysql_1_6205b2c7719e   ... done
Creating web_php-fpm_1_936cde3b56d7 ... done
Creating web_redis_1_ab5d18cc6999   ... done
Creating web_nginx_1_ea5d74e5fa72   ... done

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐