2

I'm trying to setup a development environment for Drupal using docker.

I've already succeeded on creating a PostgreSQL container and a Drupal container using official images for both, and I can replicate the production environment.

How do I get drush to detect my Drupal install?

I've tried both https://hub.docker.com/r/drush/drush/ and https://github.com/DockerDemos/appstack-drush (had to build that last one) but when I run drush inside the containers they don't see the Drupal install.

I run "docker run --volumes-from <my-drupal-container> --link <my-db-container>:postgres -it drush/drush dd" and it shows nothing, "drush cc" finds no caches.

  • You have to specify a working directory -w. See Docker docs. – leymannx Feb 6 '18 at 18:44
2

For the --uri parameter, pass in whatever URI you use to access your site from your local web browser. In addition to --uri, you must also specify --root, which should contain the full path to the Drupal root inside your container.

2

There are 2 things you can do here:

  1. Use Docker as external container like you are doing. You need to pass the --net parameter too to make that work, see example below. You will always use this drush container as a running command, not a container that is up and running with your stack all the time.

docker run --net={your_container_network_name} --rm -it -v $(pwd):/app drush/drush sqlc

  1. Simply add drush to your Drupal container and connect to that container to run drush commands:

Dockerfile:

ENV DRUSH_VERSION 8.1.2
RUN curl -L --silent https://github.com/drush-ops/drush/releases/download/${DRUSH_VERSION}/drush.phar \
  > /usr/local/bin/drush && chmod +x /usr/local/bin/drush

Connect to your container and then run any drush command inside it:

docker exec -it {container_id} bash

root@{container_id}:/var/www/html# drush status
  • Option 2 worked well for me. – Johnathan Elmore Feb 4 '17 at 20:37
  • docker exec -it -w /var/www/html {container_id} drush status in one line. docker exec -it -w /var/www/html {container_id} bash -c "drush status" probably works, too. – leymannx Feb 6 '18 at 18:47
0

To have a very convenient way to use drush / drupal console / composer in Docker, I use zsh with autoenv and a little function for each command.

In each drupal project folders, create a .autoenv.zsh file the following settings:

autostash DRUSH_IN_DOCKER=1
autostash DOCKER_PHP_CONTAINER=myproject_fpm_1
autostash PROJECT_URL=http://www.mydomain

Then creates a small zsh function:

drush() {
    if [ "$DRUSH_IN_DOCKER" == "1" ]; then
        docker exec -it ${DOCKER_PHP_CONTAINER} /var/www/vendor/bin/drush --root=/var/www/web --uri=${PROJECT_URL} "$@"
    else
        drush "$@"
    fi
}

Be sure to replace the provided paths with the one of your drupal project in docker.

-1

Run:

docker-compose exec php sh
drush cc all
  • 3
    Hi and welcome. Rather than just giving 2 commands, could you explain what these commands do? – Neograph734 Jul 3 '17 at 9:36
  • The exec command executes a program on the passed container (php). 'sh' is just a basic (non-bash) shell, which is available on most containers. So this comment is just telling you to connect to the container, which is no different than Karel's comment, except with less explanation. – Atomox Aug 21 '17 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.