In this tutorial, we will deploy a Laravel application using Docker and Alibaba Cloud Container Service.
Prerequisites
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)
- Composer installed on your computer
Preparing the Application for Deployment
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
We need to add some files to the root of the Laravel app.
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).
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.
<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:
#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 rewrite
We 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):
.git/
vendor/
node_modules/
yarn-error.log
Creating a Repository in Alibaba Cloud Container Registry
On 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.
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.)
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).
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.
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.0
When you successfully pushed the image, you will see it under the Tags tab.
Creating a VPC
On the Alibaba Cloud Console go to Products > Networking > Virtual Private Cloud and activate VPC.
Choose your region from the top menu and create a VPC and a VSwitch.
Creating a Cluster
First you need to enable RAM (Products > Monitor and Management > Resource Access Management), then you can go to Products > Elastic Computing > Container Service.
Container Service supports both Swarm and Kubernetes. Now we will use Swarm, so you should select Swarm from the left menu.
Click on the Create Cluster button and configure your cluster (don’t forget to select the same region that you selected for your VPC).
I chose 2x (2 nodes) 1 Core 1GB ECS instances for the demo, but you can choose a different configuration if you want.
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.
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.
Now the cluster nodes can pull your image from the private repository.
Deploying the Application
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.
The post Deploy a Laravel App to Alibaba Cloud Using Docker appeared first on SitePoint.
No comments:
Post a Comment