How to use docker with drupal and drush?

docker drupal
docker drupal composer
docker-compose drush
wodby drupal docker
install drush
docker drupal production
php drupal docker
drupal containerization

I'd like to use drush. It needs to run in the drupal container. There's also a drush docker repo. But I have no clue how to make it available whithin the drupal container. It's my first docker and drupal project, so maybe I'm getting things completely wrong.

How can I use drush with this drupal docker image? https://hub.docker.com/_/drupal/ Is it possible to manage it with docker-compose? Maybe extending the drupal container?

This is my docker-compose.yml:

mysql:
  image: mysql:5.5
  ports:
    - "3306:3306"
  environment:
    - MYSQL_USER=xxxxx
    - MYSQL_PASSWORD=xxxxxx
    - MYSQL_ROOT_PASSWORD=xxxxxx
    - MYSQL_DATABASE=xxxxxx

drupal:
  image: drupal:8.0.4
  links:
    - mysql
  ports:
    - "8080:80"

I have spent way too much time getting this to work. Here are my findings.

Like OP I never got the drush image to work on the local docker network so it seemed simpler to me to bundle drush via composer with the drupal image (see Dockerfile below).

That works somewhat, if you exec into the container you can run drush status, but it won't connect to the mysql server. There are two reasons for this:

  1. The package mysql-client is needed to connect remotely to the database (since we are running this on a local docker network).

  2. The mysql hostname needs to be explicitly set in the docker-compose file (or docker run command).


This is my Dockerfile:

FROM drupal:8.3.7-apache

# Install packages
RUN rm /bin/sh && ln -s /bin/bash /bin/sh && \
    apt-get update && apt-get install --no-install-recommends -y \
    curl \
    wget \
    vim \
    git \
    unzip \
    mysql-client

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php && \
    mv composer.phar /usr/local/bin/composer && \
    ln -s /root/.composer/vendor/bin/drush /usr/local/bin/drush

# Install Drush
RUN composer global require drush/drush && \
    composer global update

# Clean repository
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

The important packages are curl (obviously) and mysql-client.

And this is the relevant parts of the docker-compose.yml:

version: '3.3'

services:

  drupal:
    image: drupal
    build: ./docker/drupal
    env_file:
      - ./docker/environment.env
    ports:
      - "8080:80"
    depends_on:
      - mysql
    restart: always

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    volumes:
      - ./docker/phpmyadmin/config.user.inc.php:/etc/phpmyadmin/config.user.inc.php
    env_file:
      - ./docker/intervention/environment.env
    ports:
      - "8080:80"
    depends_on:
      - mysql
    restart: always

  mysql:
    image: mysql
    build: ./docker/mysql
    env_file:
      - ./docker/environment.env
    hostname: mysql
    ports:
      - 3306:3306
    volumes:
      - mysql-data-d8:/var/lib/mysql
    restart: always

volumes:
  mysql-data-d8:
Why explicitly setting the hostname works

The second problem above is particularly devilish since drush use the configuration from settings.php to connect to mysql. But the 'host' key in the databases array is interpreted differently bu drupal and drush apparently. Here is the relevant section from settings.php:

$databases = array (
  'default' => array (
    'default' => array (
      'database' => $envs['MYSQL_DATABASE'] ?? '',
      'username' => $envs['MYSQL_USER'] ?? '',
      'password' => $envs['MYSQL_PASSWORD'] ?? '',
      'host' => 'mysql',//php_sapi_name() === 'cli' ? 'a8597b38be21' : 'mysql',
      'port' => '3306',
      'driver' => 'mysql',
      'prefix' => 'drupal_',
      'charset' => 'utf8mb4',
      'collation' => 'utf8mb4_general_ci',
      'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
    ),
  ),
);

The out commented line after 'host' => 'mysql' is a previous attempt taken from another SO answer and illustrates the problem, drush use the command line server API which is different from whatever drupal uses. The alternate hostname is the hash id of the running container, which can be found by running the sql (from phpmyadmin for example):

SHOW VARIABLES WHERE Variable_name = 'hostname' (taken form this)

This value changes every time the container is updated, so to make it persist, the hostname is explicitely stated in docker-compose.yml, se above.


Edit: I've made a small project to host a drupal + drush + phpmyadmin development environment based on this: https://github.com/glaux/drupal8docker

Continuous Drupal: Maintaining a Drupal Website With Docker, Git , Makes using Drush on the command-line easier now that it's no longer recommended to install Drush globally. RUN rm -rf /var/www/html/*. This  A Drupal docker can also have services like SSH (for drush alias to work) and some important utilities like vim SQL Server : Choose your favourite SQL Service (MySQL or PostGRESQL or SQLite). I'm going to be using a MySQL docker.

As always with Docker there are many options to use drush. But notice that in order drush to have access to the database (eg mysql) there should be volumes from mysql container on Drupal such as:

 version: '2'
 services:
   drupal:
     image: drupal:8.0.4-apache
     volumes_from:
       - mysql
   mysql:
     image: mysql
     volumes:
       - /var/lib/mysql
       - /usr/bin
...

Here are the most common.

1) Mount your local drush to the Drupal container:

version: '2'
services:
  drupal:
    image: drupal:8.0.4-apache
    volumes:
      # Mount local drush to your container.
      - /usr/local/bin/drush:/drush/drush

2) Create a data only docker image for drush and volume the executable to your Drupal container. See an example at https://github.com/RobLoach/drush-docker/issues/32#issuecomment-222321265.

3) Install Drush into Drupal container after it has run. Eg:

$ docker exec -ti <MY_DRUPAL_CONTAINER> \ 
bash -c 'php -r "readfile('https://s3.amazonaws.com/files.drush.org/drush.phar');" \
> drush  && chmod +x drush && mv drush /usr/local/bin'

4) Install Drush on Drupal by creating a Docker image (using a Dockerfile). Example:

FROM drupal:8.0.4-apache

MAINTAINER ...

RUN php -r "readfile('https://s3.amazonaws.com/files.drush.org/drush.phar');" > drush \
&& chmod +x drush \
&& mv drush /usr/local/bin

5) Use another container with drush and volume it to the Drupal container using the docker-compose.yml. Example:

version: '2'
services:
  drupal:
    image: drupal:8.0.4-apache
    volumes_from:
      - drush
  drush:
    image: drush/drush
    volumes:
      - /usr/local/bin/drush
      - /usr/local/bin/composer

Docker based development environment, If you do a site-install with drush, this will become the site name. SUBNET is the private subnet of the network, the containers will run in. It must be unique within all  In this first post of a three-part series, we’re going to cover how we can use Docker, Git, Composer, and Drush to maintain a Drupal 8 website intelligently and efficiently. Software Used. The following software and tools are needed: Apache (available in image) Composer (available in image) Docker (v1.13.0 or newer) Docker Compose (v1.10.0 or

In order to make Drush work effectively - you're better off running it from within the container that is running Drupal. For that, add a one liner in your Dockerfile.

# Set base Image/tag here. 
FROM drupal:8-fpm 
# Get a global installation of Drush on the web / drupal container 
RUN php -r "readfile('http://files.drush.org/drush.phar');" > drush && chmod +x drush && mv drush /usr/bin/

For actual usage, you're better off shipping separate Drush aliases for usage - both from the container and the host (via public key SSH). As an example, look at these aliases and consider the entire setup as a reference implementation: https://github.com/srijanaravali/docker-blueprint/blob/master/docker-utils/drush/local.aliases.drushrc.php

You can transport these aliases into the container by having something like this in your Dockerfile:

# Push our aliases into the container ~/.drush 
ADD /path/to/your/drush-aliases/* ~/.drush/

or - for shared control with the host, you can consider having it mounted as a volume in your docker-compose.yml.

volumes:
  - ~/.drush/:/root/.drush/

You can now run Drush commands on the container like this: docker exec drush @alias <command>. You can bash alias that to be something less keyboard intensive, ofcourse.

Or, Use drush from your host by invoking the SSH alias you shipped instead - drush @alias-ssh <command>

Drush with a dockerized 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. If we need to reinitialize the install profile volume (e.g. after pushing a change to the drupal-org.make file), we can explicitly remove that specific volume with docker volume rm drupal-docker-drupal_drupal_core (where drupal-docker-drupal is the name of the folder containing the docker-compose.yml file).

One of the solutions for Drupal 8 - Docker - Drush combo is:

  1. Step: Installing the Drush in the image where you have the Apache server, let's call this docker image 'php5'.
  2. Step: make an alias in your ~/.bashrc file like this:

    alias dr5='docker-compose run php5 /root/.composer/vendor/bin/drush --root=/var/www/html/docroot';
    

In this alias i use the Docker composer but it is the same with simple Docker, you runt the Drush command on the php5 image specifying where your drush is in Docker and where your codebase is on Docker. In this way you just use it like this:

dr5 cr

For the cache rebuild command, in you local code directory.

Drupal in Docker - how do you run drush etc? : drupal, Been looking into using Docker for development, but one thing I've still not seen a good solution for is how you run commands like drush and composer in your  I don't use drush, but as for composer and Drupal Console, I run the former outside the container (it only depends on files) and the latter inside. For DC, it's as easy as docker exec <container> drupal (which I tend to alias to a shortcut), and I imagine that it should be similar with drush.

Try something like https://github.com/wodby/docker4drupal. For me is very useful and flexible thing to develop Drupal with Docker.

How to use docker drush with docker-compose · Issue #32 , If you do so, you could also install Drush in your drupal container. It shares the PHP and all other stuff. That's not, what is wanted when you use  A Docker image is a static snapshot of a container which is used to create new container instances. Docker Hub is an official repository where individuals and organizations can upload Docker images for public consumption. Why Use Docker to Run Drupal? Using the Drupal and PostgreSQL images from Docker Hub offers the following benefits:

Drupal Docker Containers · docs.amazee.io, Drush from your host machine. To use drush, you can either connect to the container as above, or add a bash  2. There are 2 things you can do here: 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.

drush/drush, A Docker container to run Drush, Drupal's command line tool. Usage. This covers how to run the Drush container through the Docker CLI. Pull. Pull drush/drush  Using Drush, the Drupal console, or Composer in PhpStorm . To start drush, the drupal console, or Composer open a terminal in PhpStorm. From the Project directory source load-env (by issuing ". load-env" or "source load-env" in the command line (without the quotation marks). This will set up aliases for drush and drupal to work with the

How to use Docker for Drupal Development, I'm going to demonstrate building the Docker using Apache with PHP. A Drupal docker can also have services like SSH (for drush alias to work)  I have a droplet setup on Digital Ocean with two docker containers, one for Drupal (using modified alpine-drupal8 image) and another running a simple mysql server.. I am able to run drush commands on this container by connecting over ssh into the droplet and running docker exec -it <container_name> /bin/sh I have also set up an ssh server (listening on port 2222 on the same ip address) on the

Comments
  • I don't understand the question, could you provide more information about what you're trying to do?
  • I'd like to use drush (github.com/drush-ops/drush). It needs to run in the drupal container. There's also a drush docker repo (hub.docker.com/r/drush/drush). But I have no clue how to make it available whithin the drupal container. It's my first docker and drupal project, so maybe I'm getting things completely wrong.
  • I'm in the same situation as @citizen404.
  • Why is it the first thing you do in your Dockerfile is remove /bin/sh? It's such a tiny binary. Seems odd.
  • @Jack_Hu Honestly I have no idea. That bit is copied from an example I found long ago and I haven’t given it a second thought.
  • thank you! I have been trying to figure this out for a while now
  • I tried approach 5, but it failed with this error: ERROR: for drush Cannot create container for service drush: b'cannot mount volume over existing file, file exists /var/lib/docker/overlay2/37b3bf71181740b62dcd1e0ca812caf1aab398228cd2a22fc7803954d8765b70/merge d/usr/local/bin/drush'
  • #4 worked like a charm. Already had the Dockerfile, so it was a no brainer. Thanks!