如何在Drupal和Drush中使用Docker? [英] How to use docker with drupal and drush?
问题描述
我想使用 drush .它需要在drupal容器中运行.还有一个提取docker repo .但是我不知道如何在drupal容器中使用它.这是我的第一个docker and drupal项目,所以也许我弄错了.
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.
如何将drush与该drupal docker映像一起使用? https://hub.docker.com/_/drupal/ 是否可以使用docker-compose进行管理?也许扩展Drupal容器?
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?
这是我的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.
像OP一样,我从来没有在本地docker网络上使用drush映像,因此对我来说,通过作曲家将drush与drupal映像捆绑在一起似乎更简单(请参阅下面的Dockerfile).
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).
这有点奏效,如果您执行到容器中,则可以运行drush status,但它不会连接到mysql服务器.这样做的原因有两个:
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:
-
需要mysql-client软件包才能远程连接到数据库(因为我们正在本地docker网络上运行它).
The package mysql-client is needed to connect remotely to the database (since we are running this on a local docker network).
需要在docker-compose文件(或docker run命令)中显式设置mysql主机名.
The mysql hostname needs to be explicitly set in the docker-compose file (or docker run command).
这是我的Dockerfile:
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/*
重要的软件包是curl
(显然)和mysql-client
.
The important packages are curl
(obviously) and mysql-client
.
这些是docker-compose.yml的相关部分:
And these are 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:
为什么显式设置主机名有效
上面的第二个问题特别令人讨厌,因为drush使用settings.php中的配置连接到mysql.但是,drupal和drush显然对数据库阵列中的主机"键有不同的解释.这是settings.php
中的相关部分:
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 by 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',
),
),
);
'host' => 'mysql'
之后的带注释的行是从另一个SO答案中进行的先前尝试,并说明了问题,请使用命令行服务器API(与drupal所使用的有所不同).备用主机名是正在运行的容器的哈希ID,可以通过运行sql(例如,从phpmyadmin)找到它:
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'
(采用此值在每次更新容器时都会更改,因此要使其持久存在,主机名在上面的docker-compose.yml
中明确说明.
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.
我已经基于此创建了一个小项目来托管drupal + drush + phpmyadmin开发环境: https://github.com/glaux/drupal8docker
I've made a small project to host a drupal + drush + phpmyadmin development environment based on this: https://github.com/glaux/drupal8docker
这篇关于如何在Drupal和Drush中使用Docker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!