How do I use a drush alias to connect to a docker container? 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 &lt;container_name&gt; /bin/sh
I have also set up an ssh server (listening on port 2222 on the same ip address) on the container itself with a little port forwarding:

ssh root@&lt;droplet_ip&gt; -p 2222 # Connects from remote dev machine to drupal container using ssh key


My sites/default/drush/aliases.drushrc.php file:

&lt;?php

$aliases['container'] = array(
  'root' =&gt; '/usr/share/nginx/html',
  'uri' =&gt; 'http://default',
  'path-aliases' =&gt;
  array (
    '%drush' =&gt; '/usr/local/bin/drush',
    '%site' =&gt; 'sites/default/',
    '%files' =&gt;'sites/default/files/',
    '%modules' =&gt;'modules/'
  ),

  'remote-host' =&gt; '&lt;droplet_ip&gt;',
  'remote-user' =&gt; 'root',
  'ssh-options' =&gt; '-p 2222',
);

?&gt;


Starting an ssh session with the drush alias is working:

$ drush @container ssh
186203cca:/usr/share/nginx/html# ### Connected to ssh session in droplet
186203cca:/usr/share/nginx/html# which drush
/usr/local/bin/drush
186203cca:/usr/share/nginx/html# drush status
 Drupal version                  :  8.1.8
 Site URI                        :  http://default
 Database driver                 :  mysql
 Database hostname               :  &lt;removed&gt;
 Database port                   :  &lt;removed&gt;
 Database username               :  &lt;removed&gt;
 Database name                   :  &lt;removed&gt;
 Drupal bootstrap                :  Successful
 Drupal user                     :
 Default theme                   :  &lt;removed&gt;
 Administration theme            :  seven
 PHP configuration               :  /etc/php7/php.ini
 PHP OS                          :  Linux
 Drush script                    :  /usr/local/bin/drush
 Drush version                   :  8.1.3
 Drush temp directory            :  /tmp
 Drush configuration             :
 Drush alias files               :
 Install profile                 :  standard
 Drupal root                     :  /usr/share/nginx/html
 Drupal Settings File            :  sites/default/settings.php
 Site path                       :  sites/default
 File directory path             :  sites/default/files
 Temporary file directory path   :  /tmp
 Sync config path                :  sites/default/files/config_vxs7qxuAqrfCtOXdbGYLvLAIKl1_bN4J_BvmrEn1l1EUIsn4Du_CKck2QWYSmkNDgUVfdZUZVQ/sync


However, I still can't seem to run actual drush commands:

$ drush @container status
env: can't execute 'drush': No such file or directory


My theory is that the port forwarding is not working correctly, and drush is trying to run on the container, not the droplet

This may be related to my issue: Why does drush say &quot;command not found&quot; when I try to execute remote commands?



Update:

A little more testing and I found out I can use drupal console without issues, so it seems this is isolated to drush only:

$ drupal @container cr all

 Rebuilding cache(s), wait a moment please.


 [OK] Done clearing cache(s).
So this one threw me for a while this evening as well. 

Here's what ended working:

$aliases['docker'] = array(
  'uri' =&gt; 'http://localhost:8080',
  'php' =&gt; '/usr/bin/php',
  'root' =&gt; '/var/www',
  'remote-user' =&gt; 'root',
  'remote-host' =&gt; '0.0.0.0',
  'ssh-options' =&gt; '-p 8022 -o PasswordAuthentication=no -i ~/.ssh/id_rsa',
  'path-aliases' =&gt;
  array (
    '%drush' =&gt; '/root/.composer/vendor/drush/drush/drush'
  )
);


The remote host of 0.0.0.0 ended up doing the trick. Seems that if you use localhost or 127.0.0.1 drush doesn't actually try to ssh in.