We received a lot of great entries in our recent competition to find the best tip for making the most out of Alibaba Cloud services. It was a fun but challenging task for our judges to pick the winners amongst so many helpful and interesting entries. But alas after fiery deliberations and heated debates they’ve decided that the first prize of the competition goes to Magyar András, who submitted this tip on deploying a Laravel app to Alibaba Cloud Container Service using Docker. It is a comprehensive guide that demonstrates usage of several relevant technologies — Docker, Laravel, Apache, Redis, Git, and connects Alibaba Cloud Container Service, Container Registry, Alibaba’s Virtual Private Cloud, and Resource Access Management.
在最近的竞赛中,我们收到了很多精彩的作品,以找到充分利用阿里云服务的最佳秘诀。 对于我们的评委来说,从众多有用和有趣的参赛作品中选出获奖者是一项有趣而艰巨的任务。 但是,经过激烈的讨论和激烈的辩论,他们决定将比赛的头奖授予MagyarAndrás,后者提交了有关使用Docker将Laravel应用部署到阿里云容器服务的提示。 它是一本综合性指南,演示了几种相关技术的用法-Docker,Laravel,Apache,Redis,Git,并连接了阿里云容器服务, 容器注册表 ,阿里巴巴的虚拟私有云和资源访问管理 。
In this tutorial, we will deploy a Laravel application using Docker and Alibaba Cloud Container Service.
在本教程中,我们将使用Docker和Alibaba Cloud Container Service部署Laravel应用程序。
Before you begin this guide you’ll need the following:
在开始本指南之前,您需要满足以下条件:
Docker installed on your local machine (if you can’t install the latest version you can use Docker Toolbox)
在本地计算机上安装了Docker (如果无法安装最新版本,则可以使用Docker Toolbox )
Composer installed on your computer
安装在计算机上的Composer
First of all, you need a Laravel application that you can Dockerize. You can just copy my example from GitHub and push to your own git repository or you can create a new Laravel app using Composer with this command: composer create-project --prefer-dist laravel/laravel appname
首先,您需要一个可以Dockerize的Laravel应用程序。 您可以从GitHub复制示例并推送到自己的git存储库,也可以使用Composer通过以下命令创建新的Laravel应用: composer create-project --prefer-dist laravel/laravel appname
We need to add some files to the root of the Laravel app.
我们需要向Laravel应用的根目录添加一些文件。
You have to create an environment configuration file for the production environment, let’s call it .env.prod. You can copy your existing .env file, but don’t forget to modify the values(for example, set APP_ENV to production).
您必须为生产环境创建一个环境配置文件,我们将其称为.env.prod。 您可以复制现有的.env文件,但不要忘记修改值(例如,将APP_ENV设置为生产)。
We will need a configuration file for the web server as well(we will use Apache), create a vhost.conf file for our virtual host.
我们还将需要用于Web服务器的配置文件(我们将使用Apache),为我们的虚拟主机创建一个vhost.conf文件。
<VirtualHost *:80> DocumentRoot /app/public <Directory "/app/public"> AllowOverride all Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>To build our container we need a Dockerfile, we will use multi-stage build:
为了构建我们的容器,我们需要一个Dockerfile,我们将使用多阶段构建:
#Install the dependencies using composer FROM composer:1.7 as build WORKDIR /app COPY . /app RUN composer install COPY .env.prod .env #Application FROM php:7.2-apache RUN docker-php-ext-install mysqli pdo pdo_mysql EXPOSE 80 COPY --from=build /app /app COPY vhost.conf /etc/apache2/sites-available/000-default.conf RUN chown -R www-data:www-data /app \ && a2enmod rewriteWe also need to exclude some files and folders from the container, so you should create a .dockerignore file (you can extend this list if you want):
我们还需要从容器中排除一些文件和文件夹,因此您应该创建一个.dockerignore文件(可以根据需要扩展此列表):
.git/ vendor/ node_modules/ yarn-error.logOn the Alibaba Cloud Console, go to Products > Elastic Computing > Container Registry.
在阿里云控制台上,转到产品>弹性计算>容器注册表 。
First, you need to set the registry login password.
首先,您需要设置注册表登录密码。
We have to create a namespace, then we can create a repository for the application.
我们必须创建一个名称空间,然后才能为该应用程序创建一个存储库。
Make sure that you set the repository type to Private, otherwise the repository will be accessible without the password. You can select the region of your repository as well.
确保将存储库类型设置为“专用”,否则无需密码即可访问该存储库。 您也可以选择存储库的区域。
The Container Registry supports GitHub, GitLab and Bitbucket as a code source which is really useful. If you use one of them you can choose that, but for the simplicity, we will use the Local repository option in this tutorial.
容器注册表支持GitHub,GitLab和Bitbucket作为非常有用的代码源。 如果使用其中之一,则可以选择该选项,但为简单起见,我们将在本教程中使用“本地存储库”选项。
You need to build the container on your local computer and push it to the registry (if you choose a Git hosting service Container Registry will build the containers automatically, so you can skip these steps.)
您需要在本地计算机上构建容器并将其推送到注册表(如果您选择Git托管服务,Container Registry将自动构建容器,因此您可以跳过这些步骤。)
Run the following command in the root of your Laravel app to build the container (you can replace test/laravel:1.0 tag with your own).
在Laravel应用的根目录中运行以下命令来构建容器(您可以使用自己的容器替换test / laravel:1.0标记)。
docker build -t test/laravel:1.0 .
docker build -t test/laravel:1.0 .
If you click on manage at the right of your repository in the Container Registry, you can find the address of your repository and a guide about how to log in to the registry and push an image to the repository.
如果在Container Registry中单击存储库右侧的管理,则可以找到存储库的地址以及有关如何登录到注册表并将映像推送到存储库的指南。
So you have to run the following commands, but with your own region, namespace and repository:
因此,您必须运行以下命令,但要使用自己的区域,名称空间和存储库:
docker login --username=user@example.com registry-intl.eu-central-1.aliyuncs.com docker tag test/laravel:1.0 registry-intl.eu-central-1.aliyuncs.com/ma_test/laravel1:1.0 docker push registry-intl.eu-central-1.aliyuncs.com/ma_test/laravel1:1.0When you successfully pushed the image, you will see it under the Tags tab.
成功推送图像后,您将在“标签”标签下看到它。
On the Alibaba Cloud Console go to Products > Networking > Virtual Private Cloud and activate VPC.
在阿里云控制台上,转到产品>网络>虚拟私有云,然后激活VPC。
Choose your region from the top menu and create a VPC and a VSwitch.
从顶部菜单中选择您的区域,然后创建一个VPC和一个VSwitch。
First you need to enable RAM (Products > Monitor and Management > Resource Access Management), then you can go to Products > Elastic Computing > Container Service.
首先,您需要启用RAM( 产品>监视和管理>资源访问管理 ),然后可以转到产品>弹性计算>容器服务 。
Container Service supports both Swarm and Kubernetes. Now we will use Swarm, so you should select Swarm from the left menu.
容器服务同时支持Swarm和Kubernetes。 现在我们将使用Swarm,因此您应该从左侧菜单中选择Swarm。
Click on the Create Cluster button and configure your cluster (don’t forget to select the same region that you selected for your VPC).
单击“创建群集”按钮并配置您的群集(不要忘记选择您为VPC选择的相同区域)。
I chose 2x (2 nodes) 1 Core 1GB ECS instances for the demo, but you can choose a different configuration if you want.
我为演示选择了2个(2个节点)1个Core 1GB ECS实例,但是如果需要,可以选择其他配置。
In the Login section, you need to create SSH keys or set a password. I highly recommend SSH keys, but for the simplicity, you can use passwords for now.
在“登录”部分,您需要创建SSH密钥或设置密码。 我强烈建议使用SSH密钥,但为简单起见,您现在可以使用密码。
When you have finished with the configuration you can click on the Create button (a confirm dialog will show up with pricing information).
完成配置后,可以单击“创建”按钮(将显示一个包含价格信息的确认对话框)。
When the cluster creation is finished and you can see your cluster in the cluster list, click on Manage.
集群创建完成后,您可以在集群列表中看到您的集群,单击“管理”。
You need to log in to your private repository to access the images, so click on the Log on to Hub button. If you don’t know what the repository’s domain name is, you should go to the Container Registry control panel and click on Manage at the right of your repository. Copy the VPC address (for example: registry-intl-vpc.eu-central-1.aliyuncs.com/ma_test/laravel1) — that is your Repository Domain Name. Your username and password is the username and password of your registry.
您需要登录到专用存储库才能访问图像,因此请单击“登录到中心”按钮。 如果您不知道存储库的域名,则应转到Container Registry控制面板,然后单击存储库右侧的“管理”。 复制VPC地址(例如: registry-intl-vpc.eu-central-1.aliyuncs.com/ma_test/laravel1 ),即您的存储库域名。 您的用户名和密码是注册表的用户名和密码。
Now the cluster nodes can pull your image from the private repository.
现在,群集节点可以从专用存储库中提取映像。
On the Container Service control panel click on Applications from the left menu, then click on Create Application.
在“容器服务”控制面板上,单击左侧菜单中的“应用程序”,然后单击“创建应用程序”。
Set the name and the version, check Pull Docker Image (this will ensure that you definitely end up with the latest version), then click on Create with Image button.
设置名称和版本,选中Pull Docker Image(这将确保您最终获得最新版本),然后单击Create with Image按钮。
Select your image (in the popup you can find your images under the User tab) and a version. In the Network section, under Web Routing, click on the blue plus symbol. For the container port, type 80. For the Domain type what you would it to be called. You can set the number of instances(Scale) as well.
选择图像(在弹出窗口中,可以在“用户”选项卡下找到图像)和版本。 在“网络”部分的“ Web路由”下,单击蓝色加号。 对于容器端口,键入80。对于“域”,键入将要调用的名称。 您还可以设置实例数(比例)。
Click create, then click on View Application List. Under Applications, you should see your new container. Click on the container, in the services tab click on the name of the container again. You will now see some information about your container. Click on the Access Endpoint URL and you will see your Laravel app’s homepage.
单击创建,然后单击查看应用程序列表。 在“应用程序”下,您应该看到新的容器。 单击容器,在服务选项卡中再次单击容器的名称。 现在,您将看到有关容器的一些信息。 单击Access Endpoint URL,您将看到Laravel应用程序的主页。
We will need an SQL database and a session database (we will use Redis), so we have to set up these services and configure the connections.
我们将需要一个SQL数据库和一个会话数据库(我们将使用Redis),因此我们必须设置这些服务并配置连接。
Go to Products > ApsaraDB > ApsaraDB for Redis. From the top menu select the region where your VPC is. Create an instance that fit your needs (you can use a 4G Master-Slave instance free for one month). After your instance is created and you can see it in the instance list, click on manage. From the left menu select Whitelist settings and modify the default value to 0.0.0.0/0 to allow connections from all machines in the VPC, or you can add the private IP/IP range of your ECS instances.
转到产品> ApsaraDB> 适用于Redis的ApsaraDB 。 从顶部菜单中选择VPC所在的区域。 创建一个满足您需要的实例(您可以免费使用一个4G Master-Slave实例一个月)。 创建实例后,您可以在实例列表中看到它,单击“管理”。 从左侧菜单中选择“白名单设置”,然后将默认值修改为0.0.0.0/0,以允许来自VPC中所有计算机的连接,或者您可以添加ECS实例的私有IP / IP范围。
From the left menu select Instance information and copy Connection Address. On your local machine open the .env.prod file and set the host and the password for Redis.
从左侧菜单中选择实例信息,然后复制连接地址。 在本地计算机上,打开.env.prod文件,然后设置Redis的主机和密码。
Similarly to this:
与此类似:
REDIS_HOST=r-4xoba5b097200b94.redis.germany.rds.aliyuncs.com REDIS_PASSWORD=my-password REDIS_PORT=6379 You should set the cache and session drivers to redis: CACHE_DRIVER=redis SESSION_DRIVER=redisLaravel needs a package named predis to interact with Redis.
Laravel需要一个名为predis的软件包才能与Redis进行交互。
On your local machine run the following command, this will install predis and add it to the dependency list:
在本地计算机上,运行以下命令,这将安装predis并将其添加到依赖项列表中:
composer require predis/predis
composer require predis/predis
We configured the Redis connection, but before we deploy a new container we will configure the MySQL connection as well.
我们配置了Redis连接,但是在部署新容器之前,我们还将配置MySQL连接。
Go to Products > ApsraraDB > ApsaraDB for RDS. From the top menu select the region where your VPC is. Create an instance that fits your needs (you can use a free instance for one month with 1 Core CPU, 1 GB Memory, 20GB storage).
转到产品> ApsraraDB> ApsaraDB for RDS 。 从顶部菜单中选择VPC所在的区域。 创建一个满足您需要的实例(您可以使用一个免费实例一个月使用1个核心CPU,1 GB内存和20GB存储空间)。
After your instance created and you can see it in the instance list click on manage. From the left menu choose Security and modify the whitelist similarly as we did it with Redis. You have to create a database account and a database, grant read-write access to the user.
创建实例后,您可以在实例列表中看到它,单击“管理”。 从左侧菜单中选择“安全性”,然后像使用Redis一样修改白名单。 您必须创建一个数据库帐户和一个数据库,向用户授予读写访问权限。
From the left menu select Connection Options and copy the intranet address(host).
从左侧菜单中选择“连接选项”,然后复制Intranet地址(主机)。
On your local machine, you should edit the database connection settings in your .env.prod file. Similarly to this:
在本地计算机上,应该在.env.prod文件中编辑数据库连接设置。 与此类似:
DB_CONNECTION=mysql DB_HOST=rm-4xo3wjj1wh2nwi7yn.mysql.germany.rds.aliyuncs.com DB_PORT=3306 DB_DATABASE=laravel1 DB_USERNAME=laravel1 DB_PASSWORD=my-passwordBuild the container and push it to the registry:
构建容器并将其推送到注册表:
You should run commands similar to these (replace the region, etc):
您应该运行与以下命令类似的命令(替换区域等):
docker build -t test/laravel:1.1 . docker tag test/laravel:1.1 registry-intl.eu-central-1.aliyuncs.com/ma_test/laravel1:1.1 docker push registry-intl.eu-central-1.aliyuncs.com/ma_test/laravel1:1.1Go to Container Service > Applications, you should see your application in the list, click on Update. Change the value of the version field and in the yaml file (template) change the image.
转到容器服务>应用程序 ,您应该在列表中看到您的应用程序,单击更新。 更改版本字段的值,并在yaml文件(模板)中更改图像。
For example, replace registry-intl-vpc.eu-central-1.aliyuncs.com/ma_test/laravel1:1.0 with registry-intl-vpc.eu-central-1.aliyuncs.com/ma_test/laravel1:1.1. Click on OK and the container will be replaced with the new one. You can use blue-green release policy as well if you want.
例如,用registry-intl-vpc.eu-central-1.aliyuncs.com/ma_test/laravel1:1.0替换registry-intl-vpc.eu-central-1.aliyuncs.com/ma_test/laravel1:1.1 。 单击确定,该容器将被替换为新的容器。 如果需要,您也可以使用蓝绿色发布策略 。
Now your application can connect to Redis and MySQL. If you want to run a database migration you can click on the application, in the services tab click on the name of the application again, select a container and click on Web Terminal. You should run the following commands:
现在,您的应用程序可以连接到Redis和MySQL。 如果要运行数据库迁移,可以单击该应用程序,在“服务”选项卡中再次单击该应用程序的名称,选择一个容器,然后单击“ Web终端”。 您应该运行以下命令:
cd /app php artisan migrateNote: If you receive a Specified key was too long error when you trying to run the migration, you can find a solution here.
注意:如果在尝试运行迁移时收到指定密钥太长的错误,则可以在此处找到解决方案。
Go to Applications in the Container Service control panel, and click on update at the right of your application. In the yaml file, add your domain to aliyun.routing.port_80. You can separate values with semicolons, for example: aliyun.routing.port_80: laravel1;yourdomain.com
转到Container Service控制面板中的“应用程序”,然后单击应用程序右侧的“更新”。 在yaml文件中,将您的域添加到aliyun.routing.port_80 。 您可以使用分号分隔值,例如: aliyun.routing.port_80: laravel1;yourdomain.com
Go to the cluster list in the Container Service control panel. Click on manage at the right of the cluster. Click on Load Balancer Settings, now you can see the id of the load balancer. Go to Products > Networking > Server Load Balancer, copy the IP address of your load balancer and add to your DNS records.
转到“容器服务”控制面板中的群集列表。 单击集群右侧的管理。 单击负载均衡器设置,现在您可以看到负载均衡器的ID。 转到产品>网络> 服务器负载均衡器 ,复制负载均衡器的IP地址,然后添加到您的DNS记录中。
You can add a TLS certificate to the load balancer as well if you want.
您也可以根据需要向负载均衡器添加TLS证书。
You probably want to store files, so you need Alibaba Cloud Object Storage Service. You can create a bucket and connect your app to the service using these official libraries:
您可能想存储文件,因此需要阿里云对象存储服务。 您可以使用以下官方库创建存储桶并将应用程序连接到服务:
aliyun/aliyun-oss-php-sdk-laravel
aliyun / aliyun-oss-php-sdk-laravel
aliyun/aliyun-oss-php-sdk-flysystem
aliyun / aliyun-oss-php-sdk-flysystem
You have successfully containerized and deployed a Laravel application to Container Service and configured several Alibaba Cloud services to work together to provide a scalable and reliable infrastructure for your application. I hope this tutorial was useful for you!
您已成功将Laravel应用程序容器化并部署到Container Service,并配置了多个阿里云服务以协同工作,从而为您的应用程序提供可扩展且可靠的基础架构。 我希望本教程对您有用!
翻译自: https://www.sitepoint.com/deploy-a-laravel-app-to-alibaba-cloud-using-docker/
相关资源:jdk-8u281-windows-x64.exe