Deploy a Laravel App to Alibaba Cloud Using Docker

    Magyar András
    Magyar András

    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.

    In this tutorial, we will deploy a Laravel application using Docker and Alibaba Cloud Container Service.


    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 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
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

    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
    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):


    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
    docker tag test/laravel:1.0
    docker push

    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: — 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.


    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.


    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.


    Connecting the Application to MySQL and Redis

    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.

    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 to allow connections from all machines in the VPC, or you can add the private IP/IP range of your ECS instances.

    From the left menu select Instance information and copy Connection Address. On your local machine open the file and set the host and the password for Redis.

    Similarly to this:
    You should set the cache and session drivers to redis:

    Laravel needs a package named predis to interact with Redis.

    On your local machine run the following command, this will install predis and add it to the dependency list:

    composer require predis/predis

    We configured the Redis connection, but before we deploy a new container we will configure the MySQL connection as well.

    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).

    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.


    From the left menu select Connection Options and copy the intranet address(host).


    On your local machine, you should edit the database connection settings in your file. Similarly to this:


    Deploying the New Version of the Application

    Build 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
    docker push

    Go 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.

    For example, replace with 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.


    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:

    cd /app
    php artisan migrate

    Note: If you receive a Specified key was too long error when you trying to run the migration, you can find a solution here.

    Add Your Domain Name to the Cluster

    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;


    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.

    You can add a TLS certificate to the load balancer as well if you want.

    Use Object Storage Service

    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:

    1. aliyun/aliyun-oss-php-sdk-laravel
    2. 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!

    Frequently Asked Questions on Deploying a Laravel App to Alibaba Cloud Using Docker

    How can I ensure the security of my Laravel app when deploying it to Alibaba Cloud using Docker?

    Security is a crucial aspect when deploying any application. When deploying your Laravel app to Alibaba Cloud using Docker, you can ensure its security by implementing several measures. Firstly, always use the latest version of Docker and Laravel, as they come with the latest security patches. Secondly, use Docker secrets to manage sensitive data like database credentials, API keys, etc. Docker secrets provide a secure way to store and manage sensitive information. Lastly, configure your Docker containers to run with a non-root user. This can prevent unauthorized access to your application.

    What are the benefits of using Docker for Laravel app deployment on Alibaba Cloud?

    Docker provides several benefits for Laravel app deployment on Alibaba Cloud. Firstly, Docker ensures consistency across multiple development environments. It means your application will behave the same way, regardless of where it is run. Secondly, Docker containers are isolated from each other, which increases the security of your application. Thirdly, Docker allows you to version control your application’s environment, which makes debugging easier. Lastly, Docker’s lightweight nature means it uses fewer resources than traditional virtual machines, which can reduce your cloud hosting costs.

    How can I troubleshoot common Docker errors when deploying my Laravel app to Alibaba Cloud?

    Troubleshooting Docker errors requires a good understanding of Docker and its commands. Docker provides several commands that can help you diagnose and fix common issues. For instance, the docker logs command allows you to view the logs of a Docker container, which can be useful for identifying errors. The docker ps command can help you check the status of your Docker containers. If a container is not running as expected, you can use the docker restart command to restart it. Additionally, understanding Dockerfile and Docker Compose file syntax can help you identify configuration errors.

    How can I optimize the performance of my Laravel app on Alibaba Cloud using Docker?

    Optimizing the performance of your Laravel app on Alibaba Cloud using Docker involves several strategies. Firstly, use multi-stage builds in your Dockerfile to reduce the size of your Docker images. Smaller images take less time to download and start up. Secondly, use Docker’s built-in health checks to monitor the status of your application and restart it if necessary. Thirdly, use Docker Compose to manage your application’s services, as it allows you to start, stop, and rebuild services with a single command. Lastly, regularly update your Docker and Laravel versions to benefit from performance improvements.

    Can I use Docker to deploy a Laravel app to other cloud platforms apart from Alibaba Cloud?

    Yes, Docker is a platform-independent tool, which means you can use it to deploy your Laravel app to any cloud platform that supports Docker, such as AWS, Google Cloud, Azure, etc. The process of deploying a Laravel app using Docker is similar across different cloud platforms. However, you may need to adjust some settings based on the specific requirements of each cloud platform. For instance, you may need to configure the security settings differently on AWS compared to Alibaba Cloud.