polkovnik-z / docker-jobs-bundle
Requires
- polkovnik-z/php-docker-client: 1.41.*
- symfony/asset: ~5.0
- symfony/form: ~5.0
- symfony/translation: ~5.0
This package is auto-updated.
Last update: 2022-07-23 10:32:10 UTC
README
提供带有 批量处理系统 的 Docker 的 Symfony 扩展包。
使用 Docker 容器来运行和处理你的工作。
需要了解
此扩展包启动的所有容器默认都在主机的网络中。
因此,如果您需要连接到本地数据库,可以使用常规的 localhost|127.0.0.1
。
安装
composer require polkovnik-z/docker-jobs-bundle
配置
配置过程相对较长,请按照以下所有步骤进行。
工作实体
您必须创建您的工作实体并扩展 BaseJob 类。
<?php namespace App\Entity; use Polkovnik\DockerJobsBundle\Entity\BaseJob; class Job extends BaseJob { }
只要您扩展了 BaseJob
,扩展包就能正常工作。
Docker 配置
为此扩展包能正常工作,您必须执行两个操作。
如果未完成 Docker 套接字权限 步骤,
扩展包将抛出 异常,因为它无法连接到 Docker。
Docker 套接字权限
默认情况下,在大多数情况下,PHP 没有权限访问默认位于 Linux 上的 docker 套接字 /var/run/docker.sock
。
为了让 PHP 能够绑定到此套接字并执行 http 请求,您必须授予必要的权限。
最简单的方法
sudo chmod 777 /var/run/docker.sock
这样做后,您将向 所有人 授予对此文件的读写执行权限。
困难的方法
如果您对安全性有顾虑,则可以仅将此权限授予 PHP 用户。
Docker 镜像
如果您尚未创建,您必须创建一个 Docker 镜像,该镜像将用于执行您的工作。
此扩展包允许为每个工作使用不同的镜像,但要求指定默认镜像以备启动时未指定 Docker 镜像时使用。
扩展包配置
一旦您有了工作实体和 Docker 镜像,最后一步是配置扩展包。
如果您尚未完成,请将扩展包包含到您的项目中
<?php # Symfony 4 # ./project_dir/config/bundles.php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], ... Polkovnik\DockerJobsBundle\DockerJobsBundle::class => ['all' => true], ]; # Symfony 3 # ./project_dir/app/AppKernel.php $bundles = array( new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), new Symfony\Bundle\SecurityBundle\SecurityBundle(), ... new Polkovnik\DockerJobsBundle\DockerJobsBundle(), )
Yaml 配置
# Symfony 4 # ./project_dir/config/packages/docker_jobs.yaml # Symfony 2 - 3 # ./project_dir/app/config/config.yml docker_jobs: class: job: App\Entity\Job # Required docker: # This is the docker daemon socket # If you haven't touched the Docker config, it should be "/var/run/docker.sock" by default unix_socket_path: '/var/run/docker.sock' # Required # Default docker image ID for job execution # You can specify docker image when creating a job, this image will be used if no image is specified at creation. default_image_id: '6c39ebee77c9' # Required # This parameter is required to avoid eventual errors # When starting a container for your job, the user will cd into this directory. container_working_dir: '/opt/symfony-project' # Required # This parameter is used to convert docker container dates to my timezone. # Docker by default uses UTC+00:00 time, me who's residing in France, i need to add 1 hour to it for it to be correct (UTC+01:00). # You can use this parameter to add or remove hours to adapt it to your timezone. time_difference: '+1 hour' # Optional (default: +1 hour) # time_difference: '+3 hours' # time_difference: '-2 hours' runtime: # Optional # This is a default concurrency limit value # This value can be overridden in the job orchestrating command. concurrency_limit: 4 # Optional (default: 4)
至此,您应该已经导入了扩展包并使其工作。
接下来,您需要配置 工作编排命令 以持续运行。
查看下面的步骤。