在 docker 容器中设置 laravel 任务调度程序
Setting laravel task scheduler in the docker container
任何人都可以帮助在 docker 容器中设置 laravel 调度程序吗?我已经使用 docker 安装并设置了服务器。但是要在 docker 容器中设置一个 cron 作业(laravel 任务调度程序),我遇到了问题。
这是我的 Dockerfile
FROM php:7.2-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www/
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
default-mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl\
cron
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www/
# Copy existing application directory permissions
COPY --chown=www:www . /var/www/
# Change current user to www
USER www
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.sh
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
我的 Entrypoint.sh 文件代码是这样的
#!/bin/bash
# Start the run once job. echo "Docker container has been started"
# Setup a cron schedule
* * * * * php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1
# This extra line makes it a valid cron" > scheduler.txt
crontab scheduler.txt
cron -f
我在制作容器时总是遇到这个错误。
cron can't open or create /var/run/crond.pid permission denied
创建一个 bin 文件夹并在该文件夹中添加 运行-scheduler.sh 文件,其中声明了 laravel 计划命令,最终会命中此命令。
#!/usr/bin/env bash
while [ true ]
do
php /var/www/artisan schedule:run --verbose --no-interaction &
sleep 60
done
在此之后声明一个 entrypoint.sh 文件,其中声明了开始 docker 命令,该命令最终每 60 秒被 运行-scheduer.sh 命中一次。
#!/bin/bash
./bin/run-scheduler.sh &
docker-php-entrypoint php-fpm
任何人都可以帮助在 docker 容器中设置 laravel 调度程序吗?我已经使用 docker 安装并设置了服务器。但是要在 docker 容器中设置一个 cron 作业(laravel 任务调度程序),我遇到了问题。
这是我的 Dockerfile
FROM php:7.2-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www/
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
default-mysql-client \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl\
cron
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www/
# Copy existing application directory permissions
COPY --chown=www:www . /var/www/
# Change current user to www
USER www
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.sh
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
我的 Entrypoint.sh 文件代码是这样的
#!/bin/bash
# Start the run once job. echo "Docker container has been started"
# Setup a cron schedule
* * * * * php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1
# This extra line makes it a valid cron" > scheduler.txt
crontab scheduler.txt
cron -f
我在制作容器时总是遇到这个错误。
cron can't open or create /var/run/crond.pid permission denied
创建一个 bin 文件夹并在该文件夹中添加 运行-scheduler.sh 文件,其中声明了 laravel 计划命令,最终会命中此命令。
#!/usr/bin/env bash
while [ true ]
do
php /var/www/artisan schedule:run --verbose --no-interaction &
sleep 60
done
在此之后声明一个 entrypoint.sh 文件,其中声明了开始 docker 命令,该命令最终每 60 秒被 运行-scheduer.sh 命中一次。 #!/bin/bash
./bin/run-scheduler.sh &
docker-php-entrypoint php-fpm