本文共 25982 字,大约阅读时间需要 86 分钟。
docker本地开发和测试
The author selected the to receive a donation as part of the program.
作者选择了来接受捐赠,这是计划的一部分。
is an open-source tool that uses Docker to build local development environments for many different PHP frameworks. Using the power of containerization, DDEV can greatly simplify how you work on multiple projects that use multiple tech stacks and multiple cloud servers. DDEV includes templates for , , , , , and more.
是一种开源工具,使用Docker为许多不同PHP框架构建本地开发环境。 使用容器化功能,DDEV可以大大简化您使用多个技术堆栈和多个云服务器的多个项目的工作方式。 DDEV包括 , , , , 模板。
was released on June 3, 2020 for the Drupal CMS. Known for its ease of use and a massive library of modules and themes, Drupal is a popular PHP framework for building and maintaining various websites and applications of all sizes.
于2020年6月3日发布,用于Drupal CMS。 Drupal以其易用性和庞大的模块和主题库而闻名,它是一个流行PHP框架,用于构建和维护各种规模的各种网站和应用程序。
In this tutorial, you will begin developing a Drupal 9 website on your local machine using DDEV. This will allow you to build your website first, and then later, when you are ready, deploy your project to a production server.
在本教程中,您将开始使用DDEV在本地计算机上开发Drupal 9网站。 这将允许您先构建网站,然后在准备就绪后将项目部署到生产服务器。
To complete this tutorial, you will need:
要完成本教程,您将需要:
One local machine running Linux or macOS
一台运行Linux或macOS的本地计算机
For macOS: , which you will use to install DDEV. To install Homebrew on your local machine, follow .
对于macOS : ,您将使用它来安装DDEV。 要在本地计算机上安装Homebrew,请遵循 。
and installed on your local machine.
在本地计算机上安装了和 。
For Linux: You can install Docker and Docker Compose following these two tutorials: and . Choose your Linux distribution from the list and follow the included instructions.
对于Linux :您可以按照以下两个教程安装Docker和Docker Compose: 以及 。 从列表中选择Linux发行版,然后按照随附的说明进行操作。
For macOS: Docker Compose was formerly available as part of Docker Toolbox, but . Today, Docker officially recommends that you install Docker Desktop, which includes , , and more. . For more information . If you previously used Docker Toolbox to install various Docker tools, .
对于macOS :Docker Compose以前是Docker Toolbox的一部分,但是 。 今天,Docker正式建议您安装Docker Desktop,其中包括 , 等。 。 有关更多信息, 。 如果您以前使用Docker Toolbox安装各种Docker工具,则 。
Note: It is possible to develop Drupal 9 using DDEV on a remote server, but you will need a solution to access localhost
in a web browser. works with , which creates a secure tunnel into your server for you and other stakeholders to view your development site. For personal use, you could also install a GUI on your remote server and access your development site through a web browser inside that interface. To do this, you could follow our guide on . For an even quicker GUI solution you can follow .
注意:可以在远程服务器上使用DDEV开发Drupal 9,但是您需要一种解决方案才能在Web浏览器中访问localhost
。 与一起 ,后者可为您和其他利益相关者创建进入您服务器的安全隧道,以供您查看开发站点。 对于个人用途,您还可以在远程服务器上安装GUI并通过该界面内的Web浏览器访问开发站点。 为此,您可以按照有关指南进行操作。 对于更快的GUI解决方案,您可以遵循 。
In this step you will install DDEV on your local machine. Option 1 includes instructions for macOS while Option 2 provides instructions for Linux. This tutorial was tested on DDEV version 1.15.0.
在此步骤中,您将在本地计算机上安装DDEV。 选项1包括针对macOS的说明,而选项2提供针对Linux的说明。 本教程已在DDEV 1.15.0版上进行了测试。
DDEV advises that macOS users install their tool using the . Use the following brew
command to install the newest stable release:
DDEV建议macOS用户使用安装其工具。 使用以下brew
命令安装最新的稳定发行版:
If you prefer the absolute newest version, you can use brew
to install ddev-edge
:
如果您喜欢绝对最新的版本,则可以使用brew
安装ddev-edge
:
If you already have a version of DDEV installed, or if you ever wish to update your version, shut down DDEV and use brew
to update your installation:
如果已经安装了DDEV的版本,或者如果您希望更新版本,请关闭DDEV并使用brew
更新安装:
Once you have installed or updated DDEV, run ddev version
to verify your software:
安装或更新DDEV之后,请运行ddev version
以验证软件:
You will see an output like this:
您将看到类似以下的输出:
Output DDEV-Local version v1.15.0commit v1.15.0db drud/ddev-dbserver-mariadb-10.2:v1.15.0dba phpmyadmin/phpmyadmin:5ddev-ssh-agent drud/ddev-ssh-agent:v1.15.0docker 19.03.8docker-compose 1.25.5os darwinrouter drud/ddev-router:v1.15.0web drud/ddev-webserver:v1.15.0
DDEV includes a powerful CLI, or command line interface. Run ddev
to learn about some common commands:
DDEV包含功能强大的CLI或命令行界面。 运行ddev
以了解一些常用命令:
You will see the following output:
您将看到以下输出:
Output Create and maintain a local web development environment.Docs: https://ddev.readthedocs.ioSupport: https://ddev.readthedocs.io/en/stable/#supportUsage: ddev [command]Available Commands: auth A collection of authentication commands composer Executes a composer command within the web container config Create or modify a ddev project configuration in the current directory debug A collection of debugging commands delete Remove all project information (including database) for an existing project describe Get a detailed description of a running ddev project. exec Execute a shell command in the container for a service. Uses the web service by default. export-db Dump a database to a file or to stdout help Help about any command hostname Manage your hostfile entries. import-db Import a sql file into the project. import-files Pull the uploaded files directory of an existing project to the default public upload directory of your project. list List projects logs Get the logs from your running services. pause uses 'docker stop' to pause/stop the containers belonging to a project. poweroff Completely stop all projects and containers pull Pull files and database using a configured provider plugin. restart Restart a project or several projects. restore-snapshot Restore a project's database to the provided snapshot version. sequelpro This command is not available since sequel pro.app is not installed share Share project on the internet via ngrok. snapshot Create a database snapshot for one or more projects. ssh Starts a shell session in the container for a service. Uses web service by default. start Start a ddev project. stop Stop and remove the containers of a project. Does not lose or harm anything unless you add --remove-data. version print ddev version and component versionsFlags: -h, --help help for ddev -j, --json-output If true, user-oriented output will be in JSON format. -v, --version version for ddevUse "ddev [command] --help" for more information about a command.
For more information about using the DDEV CLI, .
有关使用DDEV CLI的更多信息, 。
With DDEV installed on your local machine, you are now ready to install Drupal 9 and begin developing a website.
在本地计算机上安装了DDEV之后,现在就可以安装Drupal 9并开始开发网站了。
On a Linux operating system, you can install DDEV using Homebrew for Linux or using the official installation script. On Ubuntu, begin by updating your list of packages in the apt
package manager (you can use apt
in Debian, otherwise use the equivalent package manager associated with your Linux distribution):
在Linux操作系统上,可以使用Homebrew for Linux或使用官方安装脚本来安装DDEV。 在Ubuntu上,首先更新apt
软件包管理器中的软件包列表(您可以在Debian中使用apt
,否则请使用与Linux发行版关联的等效软件包管理器):
Now install some prerequisite packages from Ubuntu’s official repository:
现在,从Ubuntu的官方存储库安装一些必备软件包:
These packages will allow you to download the DDEV installation script from their official GitHub repository.
这些软件包将允许您从其官方GitHub存储库下载DDEV安装脚本。
Now download the script:
现在下载脚本:
Before running the script, open it in nano
or your preferred text editor and inspect its contents:
在运行脚本之前,请在nano
或您喜欢的文本编辑器中将其打开并检查其内容:
nano install_ddev.sh
Once you have reviewed the script’s contents and you are satisfied, save and close the file. Now you are ready to run the installation script.
查看脚本的内容并感到满意后,保存并关闭文件。 现在您可以运行安装脚本了。
Use the chmod
command to make the script executable:
使用chmod
命令使脚本可执行:
Now run the script:
现在运行脚本:
The installation process might prompt you to confirm some settings or to enter your sudo
password. Once the installation completes, you will have DDEV available on your Linux operating system.
安装过程可能会提示您确认某些设置或输入您的sudo
密码。 安装完成后,您将在Linux操作系统上使用DDEV。
Run ddev version
to verify your software:
运行ddev version
以验证您的软件:
You will see an output like this:
您将看到类似以下的输出:
Output DDEV-Local version v1.15.0commit v1.15.0db drud/ddev-dbserver-mariadb-10.2:v1.15.0dba phpmyadmin/phpmyadmin:5ddev-ssh-agent drud/ddev-ssh-agent:v1.15.0docker 19.03.8docker-compose 1.25.5os linuxrouter drud/ddev-router:v1.15.0web drud/ddev-webserver:v1.15.0
DDEV is a powerful CLI, or command line interface. Run ddev
without anything else to learn about some common commands:
DDEV是功能强大的CLI或命令行界面。 无需其他任何ddev
即可运行ddev
,以了解一些常用命令:
You will see the following output:
您将看到以下输出:
Output Create and maintain a local web development environment.Docs: https://ddev.readthedocs.ioSupport: https://ddev.readthedocs.io/en/stable/#supportUsage: ddev [command]Available Commands: auth A collection of authentication commands composer Executes a composer command within the web container config Create or modify a ddev project configuration in the current directory debug A collection of debugging commands delete Remove all project information (including database) for an existing project describe Get a detailed description of a running ddev project. exec Execute a shell command in the container for a service. Uses the web service by default. export-db Dump a database to a file or to stdout help Help about any command hostname Manage your hostfile entries. import-db Import a sql file into the project. import-files Pull the uploaded files directory of an existing project to the default public upload directory of your project. list List projects logs Get the logs from your running services. pause uses 'docker stop' to pause/stop the containers belonging to a project. poweroff Completely stop all projects and containers pull Pull files and database using a configured provider plugin. restart Restart a project or several projects. restore-snapshot Restore a project's database to the provided snapshot version. sequelpro This command is not available since sequel pro.app is not installed share Share project on the internet via ngrok. snapshot Create a database snapshot for one or more projects. ssh Starts a shell session in the container for a service. Uses web service by default. start Start a ddev project. stop Stop and remove the containers of a project. Does not lose or harm anything unless you add --remove-data. version print ddev version and component versionsFlags: -h, --help help for ddev -j, --json-output If true, user-oriented output will be in JSON format. -v, --version version for ddevUse "ddev [command] --help" for more information about a command.
For more information about using the DDEV CLI, .
有关使用DDEV CLI的更多信息, 。
With DDEV installed on your local machine, you are now ready to deploy Drupal 9 and begin developing a website.
在本地计算机上安装DDEV之后,您现在就可以部署Drupal 9并开始开发网站。
With DDEV running, you will now use it to create a Drupal-specific filesystem, install Drupal 9, and then initiate a standard website project.
在运行DDEV的情况下,您现在将使用它来创建特定于Drupal的文件系统,安装Drupal 9,然后启动一个标准的网站项目。
First, you will create a project root directory and then move inside it. You will run all remaining commands from this location. This tutorial will use d9test
, but you are free to name your directory something else. Note, however, that DDEV doesn’t handle hyphenated names well. It is considered a best practice to avoid directory names like my-project
or drupal-site-1
.
首先,您将创建一个项目根目录,然后在其中移动。 您将从该位置运行所有剩余命令。 本教程将使用d9test
,但是您可以随意命名目录。 但是请注意,DDEV不能很好地处理带连字符的名称。 最好避免使用目录名,例如my-project
或drupal-site-1
。
Create your project root directory and navigate inside:
创建项目根目录并在其中导航:
mkdir d9test
mkdir d9test
cd d9test
cd d9test
. Use the ddev config
command to create a directory structure specific to Drupal 9:
。 使用ddev config
命令创建特定于Drupal 9的目录结构:
You will see an output like this:
您将看到类似以下的输出:
Output Creating a new ddev project config in the current directory (/Users/sammy/d9test)Once completed, your configuration will be written to /Users/sammy/d9test/.ddev/config.yamlCreated docroot at /Users/sammy/d9test/webYou have specified a project type of drupal9 but no project of that type is found in /Users/sammy/d9test/webEnsuring write permissions for d9newNo settings.php file exists, creating oneExisting settings.php file includes settings.ddev.phpConfiguration complete. You may now run 'ddev start'.
Because you passed --project-type=drupal9
to your ddev config
command, DDEV created several subdirectories and files that represent the default organization for a Drupal website. Your project directory tree will now look like this:
因为您在ddev config
命令中传递了--project-type= drupal9
,所以DDEV创建了几个子目录和文件,这些子目录和文件代表了Drupal网站的默认组织。 您的项目目录树现在将如下所示:
.├── .ddev│ ├── .gitignore│ ├── config.yaml│ ├── db-build│ │ └── Dockerfile.example│ └── web-build│ └── Dockerfile.example└── web └── sites └── default ├── .gitignore ├── settings.ddev.php └── settings.php6 directories, 7 files
.ddev/
will be the main folder for the ddev configuration. web/
will be the docroot for your new project; it will contain several specific settings.
files. You now have the initial scaffolding for your new Drupal project.
.ddev/
将是ddev配置的主文件夹。 web/
将是您新项目的文档根目录; 它将包含几个特定的settings.
文件。 现在,您有了新的Drupal项目的初始脚手架。
Your next step is to initialize your platform, which will build the necessary containers and networking configurations. DDEV binds to ports 80
and 443
, so if you are running a web server like Apache on your machine, or anything else that uses those ports, stop those services before continuing.
下一步是初始化平台,这将构建必要的容器和网络配置。 DDEV绑定到端口80
和443
,因此,如果您在计算机上运行Apache之类的Web服务器,或使用这些端口的任何其他服务器,请先停止这些服务,然后再继续。
Use the ddev start
command to initialize your platform:
使用ddev start
命令初始化平台:
This will build all the Docker-based containers for your project, which include a web container, a database container, and . When the initialization completes you will see an output like this (your port number might differ):
这将为您的项目构建所有基于Docker的容器,其中包括Web容器,数据库容器和 。 初始化完成后,您将看到以下输出(您的端口号可能有所不同):
Output ...Successfully started d9testProject can be reached at http://d9test.ddev.site http://127.0.0.1:32773
Note: Remember that DDEV is starting Docker containers behind the scenes here. If you want to view those containers or verify that they are running, you can always use the docker ps
command:
注意:请记住,DDEV在这里是在后台启动Docker容器。 如果要查看那些容器或确认它们正在运行,则可以始终使用docker ps
命令:
Alongside any other containers that you are currently running, you will find four new containers, each running a different image: php-myadmin
, ddev-webserver
, ddev-router
, and ddev-dbserver-mariadb
.
除了当前正在运行的任何其他容器之外,您还将找到四个新容器,每个容器运行不同的映像: php-myadmin
, ddev-webserver
, ddev-router
和ddev-dbserver-mariadb
。
ddev start
has successfully built your containers and given you an output with two URLs. While this output says that your project “can be reached at http://d9test.ddev.site
and http://127.0.0.1:32773
,” visiting these URLs right now will throw an error. Starting with Drupal 8, the Drupal core and the contrib modules function like dependencies. Therefore, you’ll first need to finish installing Drupal using , the package manager for PHP projects, before anything loads in your web browser.
ddev start
已成功构建了容器,并为您提供了两个URL的输出。 虽然这种输出说,你的项目“可以达到http:// d9test .ddev.site
和http://127.0.0.1: 32773
,”访问这些网址,马上就会抛出一个错误。 从Drupal 8开始,Drupal核心和contrib模块的功能类似于依赖项。 因此,在将任何内容加载到Web浏览器之前,您首先需要使用 (PHP项目的软件包管理器)完成Drupal的安装。
One of the most useful and elegant features of DDEV is that you can pass Composer commands through the DDEV CLI and into your containerized environment. This means that you can separate your machine’s specific configuration from your development environment. You no longer have to manage the various file path, dependency, and version issues that generally accompany local PHP development. Moreover, you can quickly context-switch between multiple projects using different frameworks and tech stacks with minimal effort.
DDEV的最有用和最优雅的功能之一是,您可以通过DDEV CLI将Composer命令传递到您的容器化环境中。 这意味着您可以将计算机的特定配置与开发环境分开。 您不再需要管理通常伴随本地PHP开发的各种文件路径,依赖关系和版本问题。 此外,您可以轻松地使用不同的框架和技术堆栈在多个项目之间快速进行上下文切换。
Use the ddev composer
command to download drupal/recommended-project
. This will download Drupal core, its libraries, and other related resources and then create a default project:
使用ddev composer
命令下载drupal/recommended-project
。 这将下载Drupal核心,其库和其他相关资源,然后创建一个默认项目:
Now download one final component called , or Drupal Shell. This tutorial will only use one drush
command, and this tutorial provides an alternative, but drush
is a powerful CLI for Drupal development that can improve your efficiency.
现在下载一个名为或Drupal Shell的最终组件。 本教程仅使用一个drush
命令,并且本教程提供了另一种方法,但是drush
是用于Drupal开发的功能强大的CLI,可以提高效率。
Use ddev composer
to install drush
:
使用ddev composer
安装drush
:
You have now built a default Drupal 9 project and installed drush
. Now you will view your project in a browser and configure your website’s settings.
现在,您已经构建了一个默认的Drupal 9项目并安装了drush
。 现在,您将在浏览器中查看项目并配置网站的设置。
Now that you have installed Drupal 9 you can visit your new project in your browser. To do this, you can rerun ddev start
and copy one of the two URLs that it outputs, or you can use the following command, which will automatically launch your site in a new browser window:
既然已经安装了Drupal 9,就可以在浏览器中访问新项目。 为此,您可以重新运行ddev start
并复制其输出的两个URL之一,或者可以使用以下命令,它将在新的浏览器窗口中自动启动您的站点:
You will encounter the standard Drupal installation wizard.
您将遇到标准的Drupal安装向导。
Here you have two options. You can use this UI and follow the wizard through installation, or you can return to your terminal and pass a drush
command through ddev
. The latter option will automate the installation process and set admin
as both your username and password.
在这里,您有两个选择。 您可以使用此UI,然后按照向导完成安装,也可以返回到你的终端,并通过一个drush
通过命令ddev
。 后一个选项将自动执行安装过程,并将admin
设置为用户名和密码。
Return to the wizard in your browser. Under Choose language select a language from the drop-down menu and click Save and continue. Now select an installation profile. You can choose between Standard, Minimal, and Demo. Make your choice and then click Save and continue. Drupal will automatically verify your requirements, set up a database, and install your site. Your last step is to customize a few configurations. Add a site name and a site email address that ends in your domain. Then choose a username and password. Choose a strong password and keep your credentials somewhere safe. Lastly, add a private email address that you regularly check, fill in the regional settings, and press Save and continue.
返回浏览器中的向导。 在“选择语言”下,从下拉菜单中选择一种语言,然后单击“ 保存并继续” 。 现在选择一个安装配置文件。 您可以在Standard , Minimal和Demo之间进行选择。 做出选择,然后单击“ 保存并继续” 。 Drupal将自动验证您的要求,建立数据库并安装您的站点。 最后一步是自定义一些配置。 添加以您的域结尾的网站名称和网站电子邮件地址。 然后选择一个用户名和密码。 选择一个安全的密码,并将您的凭据保存在安全的地方。 最后,添加您要定期检查的私人电子邮件地址,填写区域设置,然后按保存并继续 。
Your new site will load with a welcome message.
您的新站点将加载欢迎消息。
From your project’s root directory, run this ddev exec
command to install a default Drupal site using drush
:
在项目的根目录中,运行以下ddev exec
命令以使用drush
安装默认的Drupal站点:
This will create your site just like the wizard will but with some boilerplate configurations. Your username and password will be admin
.
这将像向导一样创建您的站点,但是具有一些样板配置。 您的用户名和密码将为admin
。
Now launch the site to view it in your browser:
现在启动网站以在浏览器中查看它:
You are now ready to begin building your website, but it is considered best practice to check that your permissions are correct for the /sites/web/default
directory. While you are working locally, this is not a significant concern, but if you transfer these permissions to a production server, they will pose a security risk.
现在,您可以开始构建网站了,但是最好的做法是检查/sites/web/default
目录的权限是否正确。 当您在本地工作时,这不是一个大问题,但是,如果将这些权限转移到生产服务器,则将带来安全风险。
During the wizard installation, or when your welcome page first loads, you might see a warning about the permissions settings on your /sites/web/default
directory and one file inside that directory: settings.php
.
在向导的安装,或者当你的欢迎页面第一次加载时,你可能会看到关于您的权限设置警告/sites/web/default
目录和一个文件是目录中: settings.php
。
After the installation script runs, : this is a 555
permissions setting. It will also attempt to set permissions for default/settings.php
to read-only
, or 444
. If you encounter this warning, run these two chmod
commands from your project’s root directory. Failure to do so poses a security risk:
安装脚本运行后, :这是555
权限设置。 它还将尝试将default/settings.php
权限default/settings.php
为read-only
或444
。 如果遇到此警告,请从项目的根目录运行这两个chmod
命令。 否则会带来安全风险:
To verify that you have the correct permissions, run this ls
command with the a
, l
, h
, and d
switches:
要验证您是否具有正确的权限,请使用a
, l
, h
和d
开关运行以下ls
命令:
Check that your permissions match the following output:
检查您的权限是否与以下输出匹配:
Output dr-xr-xr-x 8 sammy staff 256 Jul 21 12:56 web/sites/default-r--r--r-- 1 sammy staff 249 Jul 21 12:12 web/sites/default/settings.php
You are now ready to develop a Drupal 9 website on your local machine.
现在您可以在本地计算机上开发Drupal 9网站了。
To test some of Drupal’s functionality, you will now create a post using the web UI.
为了测试Drupal的某些功能,您现在将使用Web UI创建一个帖子。
From your site’s initial page, click the Content button on the upper menu’s left-hand edge. Now click the blue add content button. A new page will appear. Click Article, and another page will appear.
在您网站的初始页面上,单击上方菜单左边缘的“ 内容”按钮。 现在,单击蓝色的添加内容按钮。 将会出现一个新页面。 点击文章 ,将出现另一个页面。
Add whatever title and content you like. You can add an image, too, like . When ready, click the blue save button.
添加您喜欢的任何标题和内容。 您也可以添加图像,例如 。 准备就绪后,单击蓝色的保存按钮。
Your first post will appear on your website.
您的第一篇文章将出现在您的网站上。
You are now developing a Drupal 9 website on your local machine without ever interacting with a server, thanks to Docker and DDEV. In the following step, you will manage the DDEV container to accomodate your workflow.
现在,借助Docker和DDEV,您无需与服务器进行交互即可在本地计算机上开发Drupal 9网站。 在接下来的步骤中,您将管理DDEV容器以适应您的工作流程。
When you have finished developing your project, or when you want to take a break, you can stop your DDEV container without worrying about data loss. DDEV can manage rapid context-switching among many projects; this is one of its most useful features. Your code and data are always preserved in your project directory, even after you stop or delete the DDEV container.
完成项目开发后,或想休息一下时,可以停止DDEV容器而不必担心数据丢失。 DDEV可以管理许多项目之间的快速上下文切换; 这是它最有用的功能之一。 即使停止或删除DDEV容器,代码和数据也始终保留在项目目录中。
To free up resources, you can stop DDEV at any time. From your project’s root directory, run the following command:
要释放资源,您可以随时停止DDEV。 在项目的根目录中,运行以下命令:
DDEV is available globally, so you can run ddev
commands from anywhere, as long as you specify the DDEV project:
DDEV在全球范围内可用,因此只要指定DDEV项目,就可以从任何地方运行ddev
命令:
ddev stop d9test
ddev停止d9test
You can also view all your projects at once using ddev list
:
您还可以使用ddev list
一次查看所有项目:
.
。
You can restart DDEV and continue developing locally at any time.
您可以随时重新启动DDEV并继续在本地进行开发。
In this tutorial, you used Docker and the power of containerization to develop a Drupal site locally, with the help of DDEV. DDEV also integrates well with numerous IDEs, and it provides built-in PHP debugging for , , and . From here, you can also learn more about or .
在本教程中,您使用Docker和容器化的功能在DDEV的帮助下在本地开发了Drupal站点。 DDEV还与许多IDE很好地集成在一起,并且为 , 和提供了内置PHP调试。 从这里,您还可以了解有关或 。
翻译自:
docker本地开发和测试
转载地址:http://wtegb.baihongyu.com/