So we have been able to containerize a Laravel app in this tutorial 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.
2.Fill in your desired name and the Review and Create
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.
4.Build the application locally like before. There is a slight difference though. We would append the name of the docker build command thus:
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:
docker run -p 8000:80 adura.azurecr.io.azurecr.io/docker-repo
We will now push the code to the Azure registry:
docker push adura.azurecr.io/docker-repo
Verify that it exists as a repository in the registry:
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"
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
Navigate to the URL generated to see your application:
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.
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:
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:
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.
Top comments (2)
Great article. I noticed that in the docker login, you have the repository and tag, is that a typo?
Yes, the login allows you to sign in to your registry while the tag directly shifts context to the container you wish to work on