Laravel docker cron nginx 502错误的网关问题(111:连接被拒绝)连接到上游时)

时间:2020-07-06 08:37:27

标签: laravel docker nginx cron

我是docker容器中的laravel应用。在我向dockerfile添加cron之前,一切工作正常。我需要安排工作,所以我需要一名工作人员。 我的撰写文件看起来像这样

version: '3'

networks:
  laravel:
    driver: bridge

services:
  nginx:
    image: nginx:stable
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

nginx配置文件如下

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我的docker文件看起来像这样

FROM php:7.3-fpm

WORKDIR /var/www/html

# RUN docker-php-ext-install pdo pdo_mysql mysqli
# RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable mysqli

# Install cron
RUN apt-get update && apt-get install -y cron

# Add crontab file in the cron directory
ADD src/app/schedule/crontab /etc/cron.d/cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD printenv > /etc/environment && echo "cron starting..." && (cron) && : > /var/log/cron.log && tail -f /var/log/cron.log

nginx日志中的错误是

2020/07/06 08:27:06 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.19.0.2:9000", host: "localhost:8080"

nginx运行于

/nginx - 172.19.0.7

这是怎么回事?

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方法

我们需要替换docker文件的最后一行。

请使用此

CMD cron && docker-php-entrypoint php-fpm

代替

CMD printenv > /etc/environment && echo "cron starting..." && (cron) && : > /var/log/cron.log && tail -f /var/log/cron.log