用dockerfile搭建php+mysql+redis+swoole環境,使用docker-compose up啟動

meigo發表於2022-06-27

一、編寫dockerfile檔案

編寫dockerfile構建映象,主要是一些php的擴充套件

FROM php:7.4-fpm


ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone


RUN apt-get update && apt-get install -y \
        --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev curl \
        && rm -r /var/lib/apt/lists/* \
        && docker-php-ext-configure gd \
        && docker-php-ext-install -j$(nproc) gd opcache pdo_mysql gettext sockets


RUN pecl install redis \
    && pecl install swoole \
    && docker-php-ext-enable redis swoole


ENV COMPOSER_HOME /root/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH

WORKDIR /data

構建映象:

docker build -t malina-php-project .

執行之後:
file
說明映象構建成功

二、編寫docker-compose.yaml

一定要注意層次與空格,嚴格按照yaml格式進行編寫
在project資料夾中建立project1
在project1中建立docker-compose.yaml

version: '3.0'
services:
  nginx:
    image: "nginx:latest"
    ports:
      - "80:80"
    volumes:
      - /home/malina/project/project1:/usr/share/nginx/html
  php-fpm:
    image: "malina-php-project"
    volumes:
      - /home/malina/project/project1:/usr/share/nginx/html
  mysql:
    image: "mysql:latest"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
  redis:
    image: "redis:4.0"

執行:docker-compose up -d

到此,docker-compose 就已經啟動好了
檢視執行的映象:docker ps

三、檢視容器的docker ip 地址

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

file

四、對nginx不能正常訪問

把nginx.conf複製出來進行修改

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        root      /usr/share/nginx/html;
        fastcgi_pass  172.20.0.2:9000;#此處需要填寫你的php容器的docker內部通訊ip
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
        include    fastcgi_params;
      }
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

修改完配置之後再匯入docker裡面

docker cp nginx.conf project_nginx_1:/etc/nginx/conf.d/default.conf

對nginx容器進行重啟

docker container stop project_nginx_1 
docker container start project_nginx_1 #重啟nginx容器使配置檔案生效

容器內安裝vim

apt update
apt install vim

localhost/

此時可以訪問到/usr/share/nginx/html/index.html的內容

如果遇到可以訪問html  不可以訪問php
需要進入容器,檢視/etc/nginx/conf.d/default.conf配置是否正確

訪問檔案時,html可以正常訪問,php檔案訪問不了,不防換個瀏覽器試一下

fastcgi_pass  172.20.0.2:9000可更改為fastcgi_pass  php-fpm:9000
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章