A Handy Script for .NET Developer Containers on Docker

July 21, 2021
Cover Image

I'm a .NET Core developer and I mostly work on a Mac. Before docker and containers, this would have been hard to do. But now there are containers for SQL Server, the Azure Storage emulator, and the CosmosDb emulator. Developing on a Mac is easy now. And developing on a Mac makes it easy to deploy to Linux-based servers and Linux-based Azure resources.

When I'm doing development, I almost always want all three of these containers running. I got tired of manually issuing the docker commands to start and stop and update these containers so I created a bash script to control the containers.

On my machine, I've got this script saved as 'devcontainers.sh'. It takes a bunch of command line parameters:

  • -a controls the action (start, stop)
  • -k to kill and delete the containers
  • -p to pull for updates to the containers

By default if I call "./devcontainers.sh", it'll start all three of these containers. "./devcontainers -a stop" will stop all the containers. "./devcontainers.sh -k 1 -p 1" will kill any existing instance of the container, check for updates to the container images, and start the containers.

Here's the contents of the script. Here's a link to download the script as a text file.

#!/bin/bash

ipaddr="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"

killAndResetContainers="0";
pullContainers="0";
action="start";

containerNameSqlServer="sqlserver";
containerNameAzureStorage="azurestorage";
containerNameCosmos="cosmosdb";

while getopts a:k:p: flag
do
    case "${flag}" in
        a) action=${OPTARG};;
        k) killAndResetContainers=${OPTARG};;
        p) pullContainers=${OPTARG};;
    esac
done

echo "  "
echo "FYI, the available script options are: "
echo "  a - container action                     --> start|stop|none"
echo "  k - kill and reset containers            --> 0|1"
echo "  p - pull containers for updated versions --> 0|1"
echo "  "

if [ "$killAndResetContainers" != "1" ]; then
    echo "***** NOT KILLING AND RESETTING CONTAINERS *****"
else
    echo "***** KILLING AND RESETTING CONTAINERS *****"
    
    docker stop $containerNameAzureStorage || true && docker rm $containerNameAzureStorage || true
    docker stop $containerNameSqlServer || true && docker rm $containerNameSqlServer || true
    docker stop $containerNameCosmos || true && docker rm $containerNameCosmos || true
fi

if [ "$pullContainers" != "1" ]; then
    echo "***** NOT PULLING CONTAINERS *****"
else
    echo "***** PULLING CONTAINERS *****"
    
    docker pull mcr.microsoft.com/azure-storage/azurite
    docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    docker pull mcr.microsoft.com/mssql/server:2019-latest
fi

if [ "$action" == "start" ]; then
    echo "***** STARTING CONTAINERS *****"
    
    if [ "$(docker ps -q -f name=$containerNameAzureStorage -f status=exited)" ]; then
        echo "***** resuming $containerNameAzureStorage *****"
        docker start $containerNameAzureStorage
    elif [ "$(docker ps -q -f name=$containerNameAzureStorage -f status=running)" ]; then
        echo "***** container for $containerNameAzureStorage already running *****"
    else
        echo "***** starting new instance of $containerNameAzureStorage *****"
        docker run --name $containerNameAzureStorage -p 10000:10000 -p 10001:10001 -d mcr.microsoft.com/azure-storage/azurite
    fi

    if [ "$(docker ps -q -f name=$containerNameCosmos -f status=exited)" ]; then
        echo "***** resuming $containerNameCosmos *****"
        docker start $containerNameCosmos
    elif [ "$(docker ps -q -f name=$containerNameCosmos -f status=running)" ]; then
        echo "***** container for $containerNameCosmos already running *****"
    else
        echo "***** starting new instance of $containerNameCosmos *****"
        docker run -p 8081:8081 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254  -m 3g --cpus=2.0 --name $containerNameCosmos -e AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10 -e AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true -e AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr -d mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    fi

    if [ "$(docker ps -q -f name=$containerNameSqlServer -f status=exited)" ]; then
        echo "***** resuming $containerNameSqlServer *****"
        docker start $containerNameSqlServer
    elif [ "$(docker ps -q -f name=$containerNameSqlServer -f status=running)" ]; then
        echo "***** container for $containerNameSqlServer already running *****"
    else
        echo "***** starting new instance of $containerNameSqlServer *****"
        docker run --name $containerNameSqlServer -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Pa$$word' -p 1433:1433 -e 'MSSQL_PID=Standard' -d mcr.microsoft.com/mssql/server:2019-latest
    fi
elif [ "$action" == "stop" ]; then
    echo "***** STOPPING CONTAINERS *****"
    
    docker stop $containerNameAzureStorage || true
    docker stop $containerNameSqlServer || true
    docker stop $containerNameCosmos || true
else
    echo "***** NOT RUNNING START OR STOP CONTAINER ACTIONS *****"
fi

I hope this is helpful.

-Ben