Skip to content

Instantly share code, notes, and snippets.

@marshyon
Created August 26, 2019 10:00
Show Gist options
  • Save marshyon/211cba078d01ec1e8045d0579a4ef5a7 to your computer and use it in GitHub Desktop.
Save marshyon/211cba078d01ec1e8045d0579a4ef5a7 to your computer and use it in GitHub Desktop.
Deploy a Go web service as an Azure Web Application using Docker - deploy.sh
#!/usr/bin/bash
# replace 'uniquename' with your own unique name
# define variables for repeated use in the current script
export RESOURCE_GROUP="uniquenameResourceGroup"
export AZURE_REGISTRY_NAME="uniquenameRegisry"
export DOCKER_IMAGE_NAME="uniquename-custom-docker-image"
export DOCKER_IMAGE_TAG="v1.0.0"
export APP_SERVICE_PLAN_NAME="uniquenameAppServicePlan"
export APP_NAME="uniquenameApplicaton" # this will need to be unique
export APP_PORT="8080" # this is the port number our docker image is configured to listen on
# build our docker image
docker build -t ${DOCKER_IMAGE_NAME} .
# create a resource group into which we can store resources pertinent to this activity
az group create --name ${RESOURCE_GROUP} --location "UK South"
# create a private registry in order to store our docker image
az acr create --name ${AZURE_REGISTRY_NAME} \
--resource-group ${RESOURCE_GROUP} --sku Basic \
--admin-enabled true
# in order to authenticate against our newly created registry, get user name and password for this
CREDENTIAL_JSON=$(az acr credential show --name ${AZURE_REGISTRY_NAME})
# the output of the credential show command will be in json format and we need to extract
# - passwords .. value ( the first or second it doesn't matter which
# - username ( this will be the registry name )
# the following jq commands will do this for us
export REGISTRY_USERNAME=$(echo ${CREDENTIAL_JSON} | jq -r '.username')
export REGISTRY_PASSWORD=$(echo ${CREDENTIAL_JSON} | jq -r "[.passwords][0][0]|.value")
# now we authenticate against our newly created docker registry
docker login ${AZURE_REGISTRY_NAME}.azurecr.io --username ${REGISTRY_USERNAME} --password ${REGISTRY_PASSWORD}
# tag our recently created custom docker image
docker tag ${DOCKER_IMAGE_NAME} ${AZURE_REGISTRY_NAME}.azurecr.io/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
# push our tagged image to our private registry
docker push ${AZURE_REGISTRY_NAME}.azurecr.io/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
# list our docker images
az acr repository list -n ${AZURE_REGISTRY_NAME}
# create an Azure App Service Plan
az appservice plan create --name ${APP_SERVICE_PLAN_NAME} \
--resource-group ${RESOURCE_GROUP} --sku B1 --is-linux
# create the web app into which we will deploy our container
az webapp create --resource-group ${RESOURCE_GROUP} \
--plan ${APP_SERVICE_PLAN_NAME} --name ${APP_NAME} \
--deployment-container-image-name ${AZURE_REGISTRY_NAME}.azurecr.io/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}
# give the application credentials which it will need to log in to our private registry
az webapp config container set --name ${APP_NAME} \
--resource-group ${RESOURCE_GROUP} \
--docker-custom-image-name ${AZURE_REGISTRY_NAME}.azurecr.io/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} \
--docker-registry-server-url https://${AZURE_REGISTRY_NAME}.azurecr.io \
--docker-registry-server-user ${REGISTRY_USERNAME} \
--docker-registry-server-password ${REGISTRY_PASSWORD}
# configure environment variables for the docker instance
az webapp config appsettings set --resource-group ${RESOURCE_GROUP} --name ${APP_NAME} --settings WEBSITES_PORT=${APP_PORT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment