github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/docs/content/users/configuration/hooks.md (about)

     1  # Hooks
     2  
     3  Most DDEV commands provide hooks to run tasks before or after the main command executes. To automate setup tasks specific to your project, define them in the project’s `config.yaml` file.
     4  
     5  To define command tasks in your configuration, specify the desired command hook as a subfield to `hooks`, then a list of tasks to run:
     6  
     7  ```yaml
     8  hooks:
     9    post-start:
    10      - exec: "simple command expression"
    11      - exec: "ls >/dev/null && touch /var/www/html/somefile.txt"
    12      - exec-host: "simple command expression"
    13    post-import-db:
    14      - exec: "drush uli"
    15  ```
    16  
    17  ## Supported Command Hooks
    18  
    19  * `pre-start`: Hooks into [`ddev start`](../usage/commands.md#start). Execute tasks before the project environment starts.
    20  
    21      !!!tip
    22          Only `exec-host` tasks can run during `pre-start` because the containers are not yet running. See [Supported Tasks](#supported-tasks) below.
    23  
    24  * `post-start`: Execute tasks after the project environment has started.
    25  * `pre-import-db` and `post-import-db`: Execute tasks before or after database import.
    26  * `pre-import-files` and `post-import-files`: Execute tasks before or after files are imported.
    27  * `pre-composer` and `post-composer`: Execute tasks before or after the `composer` command.
    28  * `pre-stop`, `pre-config`, `post-config`, `pre-exec`, `post-exec`, `pre-pause`, `post-pause`, `pre-pull`, `post-pull`, `pre-push`, `post-push`, `pre-snapshot`, `post-snapshot`, `pre-restore-snapshot`, `post-restore-snapshot`: Execute as the name suggests.
    29  * `post-stop`: Hooks into [`ddev stop`](../usage/commands.md#stop). Execute tasks after the project environment stopped.
    30  
    31      !!!tip
    32          Only `exec-host` tasks can run during `post-stop`. See [Supported Tasks](#supported-tasks) below.
    33  
    34  ## Supported Tasks
    35  
    36  DDEV currently supports these tasks:
    37  
    38  * `exec` to execute a command in any service/container.
    39  * `exec-host` to execute a command on the host.
    40  * `composer` to execute a Composer command in the web container.
    41  
    42  ### `exec`: Execute a shell command in a container (defaults to web container)
    43  
    44  Value: string providing the command to run. Commands requiring user interaction are not supported. You can also add a “service” key to the command, specifying to run it on the `db` container or any other container you use.
    45  
    46  Example: _Use Drush to clear the Drupal cache and get a user login link after database import_.
    47  
    48  ```yaml
    49  hooks:
    50    post-import-db:
    51      - exec: drush cr
    52      - exec: drush uli
    53  ```
    54  
    55  Example: _Use wp-cli to replace the production URL with development URL in a WordPress project’s database_.
    56  
    57  ```yaml
    58  hooks:
    59    post-import-db:
    60      - exec: wp search-replace https://www.myproductionsite.com http://mydevsite.ddev.site
    61  ```
    62  
    63  Example: _Add an extra database before `import-db`, executing in `db` container_.
    64  
    65  ```yaml
    66  hooks:
    67    pre-import-db:
    68      - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS some_new_database;"
    69        service: db
    70  
    71  ```
    72  
    73  Example: _Add the common `ll` alias into the `web` container’s `.bashrc` file_.
    74  
    75  ```yaml
    76  hooks:
    77    post-start:
    78    - exec: sudo echo alias ll=\"ls -lhA\" >> ~/.bashrc
    79  ```
    80  
    81  !!!tip
    82      This could be done more efficiently via `.ddev/web-build/Dockerfile` as explained in [Customizing Images](../extend/customizing-images.md).
    83  
    84  Advanced usages may require running commands directly with explicit arguments. This approach is useful when Bash interpretation is not required (no environment variables, no redirection, etc.).
    85  
    86  ```yaml
    87  hooks:
    88    post-start:
    89    - exec: 
    90      exec_raw: [ls, -lR, /var/www/html]
    91  ```
    92  
    93  ### `exec-host`: Execute a shell command on the host system
    94  
    95  Value: string providing the command to run. Commands requiring user interaction are not supported.
    96  
    97  ```yaml
    98  hooks:
    99    pre-start:
   100      - exec-host: "command to run"
   101  ```
   102  
   103  ### `composer`: Execute a Composer command in the web container
   104  
   105  Value: string providing the Composer command to run.
   106  
   107  Example:
   108  
   109  ```yaml
   110  hooks:
   111    post-start:
   112      - composer: config discard-changes true
   113  ```
   114  
   115  ## WordPress Example
   116  
   117  ```yaml
   118  hooks:
   119    post-start:
   120      # Install WordPress after start
   121      - exec: "wp config create --dbname=db --dbuser=db --dbpass=db --dbhost=db"
   122      - exec: "wp core install --url=http://mysite.ddev.site --title=MySite --admin_user=admin --admin_email=admin@mail.test"
   123    post-import-db:
   124      # Update the URL of your project throughout your database after import
   125      - exec: "wp search-replace https://www.myproductionsite.com http://mydevsite.ddev.site"
   126  ```
   127  
   128  ## Drupal 7 Example
   129  
   130  ```yaml
   131  hooks:
   132    post-start:
   133      # Install Drupal after start if not installed already
   134      - exec: "(drush status bootstrap | grep -q Successful) || drush site-install -y --db-url=db:db@db/db"
   135      # Generate a one-time login link for the admin account
   136      - exec: "drush uli"
   137    post-import-db:
   138      # Set the project name
   139      - exec: "drush vset site_name MyDevSite"
   140      # Enable the environment indicator module
   141      - exec: "drush en -y environment_indicator"
   142      # Clear the cache
   143      - exec: "drush cc all"
   144  ```
   145  
   146  ## Drupal 8 Example
   147  
   148  ```yaml
   149  hooks:
   150    post-start:
   151      # Install Composer dependencies from the web container
   152      - composer: install
   153      # Install Drupal after start if not installed already
   154      - exec: "(drush status bootstrap | grep -q Successful) || drush site-install -y --db-url=mysql://db:db@db/db"
   155      # Generate a one-time login link for the admin account
   156      - exec: "drush uli 1"
   157    post-import-db:
   158      # Set the site name
   159      - exec: "drush config-set system.site name MyDevSite"
   160      # Enable the environment indicator module
   161      - exec: "drush en -y environment_indicator"
   162      # Clear the cache
   163      - exec: "drush cr"
   164  ```
   165  
   166  ## TYPO3 Example
   167  
   168  ```yaml
   169  hooks:
   170      post-start:
   171        - composer: install
   172  ```
   173  
   174  ## Adding Additional Debian Packages (PHP Modules) Example
   175  
   176  ```yaml
   177  webimage_extra_packages: ["php-bcmath", "php7.4-tidy"]
   178  dbimage_extra_packages: ["vim"]
   179  ```