github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/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-pull`, `post-pull`, `pre-push`, `post-push`, `pre-snapshot`, `post-snapshot`, `pre-delete-snapshot`, `post-delete-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 rebuild all caches and get a user login link after database import_. 47 48 ```yaml 49 hooks: 50 post-import-db: 51 - exec: drush cache:rebuild 52 - exec: drush user:login 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: _Use Drush to sanitize a database by removing or obfuscating user data_. 64 65 ```yaml 66 hooks: 67 post-import-db: 68 - exec: drush sql:sanitize 69 ``` 70 71 Example: _Add an extra database before `import-db`, executing in `db` container_. 72 73 ```yaml 74 hooks: 75 pre-import-db: 76 - exec: mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS some_new_database;" 77 service: db 78 79 ``` 80 81 Example: _Add the common `ll` alias into the `web` container’s `.bashrc` file_. 82 83 ```yaml 84 hooks: 85 post-start: 86 - exec: sudo echo alias ll=\"ls -lhA\" >> ~/.bashrc 87 ``` 88 89 !!!tip 90 This could be done more efficiently via `.ddev/web-build/Dockerfile` as explained in [Customizing Images](../extend/customizing-images.md). 91 92 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.). 93 94 ```yaml 95 hooks: 96 post-start: 97 - exec: 98 exec_raw: [ls, -lR, /var/www/html] 99 ``` 100 101 ### `exec-host`: Execute a shell command on the host system 102 103 Value: string providing the command to run. Commands requiring user interaction are not supported. 104 105 ```yaml 106 hooks: 107 pre-start: 108 - exec-host: "command to run" 109 ``` 110 111 ### `composer`: Execute a Composer command in the web container 112 113 Value: string providing the Composer command to run. 114 115 Example: 116 117 ```yaml 118 hooks: 119 post-start: 120 - composer: config discard-changes true 121 ``` 122 123 ## WordPress Example 124 125 ```yaml 126 hooks: 127 post-start: 128 # Install WordPress after start 129 - exec: "wp config create --dbname=db --dbuser=db --dbpass=db --dbhost=db" 130 - exec: "wp core install --url=http://mysite.ddev.site --title=MySite --admin_user=admin --admin_email=admin@mail.test" 131 post-import-db: 132 # Update the URL of your project throughout your database after import 133 - exec: "wp search-replace https://www.myproductionsite.com http://mydevsite.ddev.site" 134 ``` 135 136 ## Drupal 7 Example 137 138 ```yaml 139 hooks: 140 post-start: 141 # Install Drupal after start if not installed already 142 - exec: "(drush status bootstrap | grep -q Successful) || drush site-install -y --db-url=db:db@db/db" 143 # Generate a one-time login link for the admin account 144 - exec: "drush uli" 145 post-import-db: 146 # Set the project name 147 - exec: "drush vset site_name MyDevSite" 148 # Enable the environment indicator module 149 - exec: "drush en -y environment_indicator" 150 # Clear the cache 151 - exec: "drush cc all" 152 ``` 153 154 ## Drupal 10 Example 155 156 ```yaml 157 hooks: 158 post-start: 159 # Install Composer dependencies from the web container 160 - composer: install 161 # Generate a one-time login link for the admin account 162 - exec: "drush user:login" 163 post-import-db: 164 # Sanitize the database 165 - exec: "drush sql:sanitize" 166 # Apply any database updates 167 - exec: "drush updatedb" 168 # Rebuild all caches 169 - exec: "drush cache:rebuild" 170 ``` 171 172 ## TYPO3 Example 173 174 ```yaml 175 hooks: 176 post-start: 177 - composer: install 178 ``` 179 180 ## Adding Additional Debian Packages (PHP Modules) Example 181 182 ```yaml 183 webimage_extra_packages: ["php-bcmath", "php7.4-tidy"] 184 dbimage_extra_packages: ["vim"] 185 ```