dockerfile搭建php+mysql+redis+swoole环境,使用docker-compose up启动

3 篇文章 0 订阅
1 篇文章 0 订阅

一、编写 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 .

运行之后:
在这里插入图片描述
说明镜像构建成功

二、编写 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)

在这里插入图片描述

四、对 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文件访问不了,不防换个浏览器试一下

————————————————
原文作者:meigo
转自链接:https://learnku.com/articles/69079
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值