Deploying Dockerised Laravel Applications on Azure
Tags: devops, azure, laravel, backenddevelopment
Published: 2023-10-23 11:57:39




![laravel az](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fsfkjb61plzpj97kj186.png)

So we have been able to containerize a Laravel app [in this tutorial](https://dev.to/dreywandowski/how-to-dockerize-laravel-applications-48m7) and everything is fine and dandy. How do we go about actually deploying it though, and making the image get live updates and essentially "_rebuild_" itself? 

We need 3 things: a Github account which would serve as source control, a Circle CI account to act as middleware to help with our workflows and finally, a Microsoft Azure account with an active subscription. We would be deploying the app on the infrastructure provided.

1.On your Azure portal, click on **Create a resource** and search for "azure registry". Click on **Create**.

![azure registry create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imiuz0ct3nsxhltkpofc.png)

2.Fill in your desired name and the **Review and Create**


![createee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ardvjfj7u3um1u7f2ai4.png)


3.Once done, navigate to "Access Keys" , make sure "Admin User" is checked on _Enabled_. Copy your username and password as well. We will be needing it.


![admin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7mucagzqd1l9b6kk96w.png)

4.Build the application locally like before. There is a slight difference though. We would append the name of the docker build command thus: 

```php
docker login -u {user} -p {password} adura.azurecr.io

docker build -t adura.azurecr.io.azurecr.io/docker-repo .
```
Here, we login from our code editor to the Azure registry. We then build the Docker image, using the registry name as the leading tag together with the desired image name. This would allow the Docker daemon to tag it to the appropriate Azure registry.

5.You can also run it locally to confirm its running:
```php
docker run -p 8000:80 adura.azurecr.io.azurecr.io/docker-repo
```
We will now push the code to the Azure registry:
```php
docker push adura.azurecr.io/docker-repo
```

Verify that it exists as a repository in the registry:

![repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4zwagprn0w8mxcoi4j8.png)

6.Next, we will need to create an Azure Web app for the container. Go to **Create Resource** and search for "web app for containers"

![container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdpx5oh1hy7yix86ro99.png) 
Give it a relevant name and navigate to the "Docker" tab. Select "Azure Container Registry" as your image source and wait for the dropdown to populate. Select the repo we pushed and **Review + Create**

![docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hny6fn1t2c0e60cqht8z.png)

Navigate to the URL generated to see your application: 
![laravel app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07ltww6xxcljuefwurab.png)


7.We will now look towards continious intergration, i.e. making changes to the app and refreshing the docker build on Azure automatically. Navigate to *Deployment Center* , turn "Continous Deployment" to ON. Any changes to the docker repo will trigger a fresh build of the image for our application.

![ON](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4npnquywr86jv7wbsyrc.png)

8.Next, push your code to Github, sign-in to Circle CI and then link your Github's repo to identify your latest project. Click on "Set Up Project". Click on the prompt that says you will use the configuration in your project:

![circle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3768hms0g8au95pqaje.png)
Next, click on **Project Settings** > **Environment Variables**, fill in the user credentials of your Azure registry so as to hook into the Azure registry, DOCKER_USERNAME and DOCKER_PASS.

9.We now need to write the circle CI config file in our project.
Create a folder with the name .circleci in the root of your project, create a file config.yml and paste the following sample workflow:
```php
version: 2.1
orbs:
  docker: circleci/docker@2.1.2
jobs:
  build-and-test:
    description: Setup laravel application
    docker:
      # Specify the version desired
      - image: cimg/php:8.0-browsers

    steps:
      - checkout

      - run:
          name: "Prepare environment"
          command: |
            sudo apt update
      # Download and cache dependencies
      - restore_cache:
          keys:
            # "composer.lock" can be used if it is committed to the repo
            - v1-dependencies-{{ checksum "composer.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run:
          name: "Install dependencies"
          command: composer install -n --prefer-dist

      - save_cache:
          key: v1-dependencies-{{ checksum "composer.json" }}
          paths:
            - ./vendor

      - run:
          name: "Create .env file and generate app key"
          command: |
            mv .env.example .env
            php artisan key:generate

      - run:
          name: "Run tests"
          command: php vendor/bin/phpunit

  build-docker-image:
    executor:
      name: docker/docker
      tag: "3.6"
    steps:
      - checkout
      - docker/install-docker-tools
      - setup_remote_docker:
          version: 20.10.14
          docker_layer_caching: true
      - run:
          name: "Build and push Docker image"
          command: |
            docker build -t adura.azurecr.io/docker-repo:latest .
            docker login -u $DOCKER_USER -p $DOCKER_PASS adura.azurecr.io/docker-repo:latest
            docker push adura.azurecr.io/docker-repo:latest

workflows:
  test-and-deploy:
    jobs:
      - build-and-test
      - build-docker-image:
          requires:
            - build-and-test
```

10.Finally we push to Github and check our Circle CI dashboard. The progress will show and the setup will be completed.


![deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32rsxv6rumcpddzlzueo.png)