nano mssql_docker.sh
Script:
#!/bin/bash
# =========================
# Configuration
# =========================
CONTAINER_NAME="mssql2019"
MSSQL_IMAGE="mcr.microsoft.com/mssql/server:2019-latest"
VOLUME_NAME="mssql2019_data"
# =========================
# Check Docker installation
# =========================
if ! command -v docker &> /dev/null; then
echo "đ Docker not found. Installing..."
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable --now docker
sudo usermod -aG docker $USER
echo "â
Docker installed successfully. Please log out and log back in if you want to use docker without sudo."
else
echo "â
Docker is already installed."
fi
# =========================
# Check Docker volume
# =========================
if ! docker volume ls | grep -q "$VOLUME_NAME"; then
echo "đĻ Creating Docker volume: $VOLUME_NAME"
docker volume create "$VOLUME_NAME"
else
echo "đĻ Docker volume '$VOLUME_NAME' already exists."
fi
# =========================
# Check container
# =========================
if docker container inspect "$CONTAINER_NAME" > /dev/null 2>&1; then
RUNNING=$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME")
if [ "$RUNNING" = "true" ]; then
echo "â
Container '$CONTAINER_NAME' is already running."
else
echo "âļī¸ Starting existing container '$CONTAINER_NAME'..."
docker start "$CONTAINER_NAME"
echo "â
Container '$CONTAINER_NAME' started."
fi
else
echo "đ§ Creating MSSQL container '$CONTAINER_NAME'..."
echo "â ī¸ Please set SA_PASSWORD environment variable before running this script."
echo "Example: export SA_PASSWORD='YourStrong!Passw0rd'"
if [ -z "$SA_PASSWORD" ]; then
echo "â SA_PASSWORD not set. Aborting."
exit 1
fi
docker run -e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=$SA_PASSWORD" \
-e "TZ=$(cat /etc/timezone)" \
-p 11433:1433 \
--name "$CONTAINER_NAME" \
--volume "$VOLUME_NAME:/var/opt/mssql" \
--restart always \
-d "$MSSQL_IMAGE"
echo "â
Container '$CONTAINER_NAME' created and running."
fi
# =========================
# Check/install sqlcmd on host
# =========================
if ! command -v sqlcmd &> /dev/null; then
echo "đ Installing sqlcmd tools on host..."
curl <https://packages.microsoft.com/keys/microsoft.asc> | sudo apt-key add -
curl <https://packages.microsoft.com/config/ubuntu/22.04/prod.list> | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt update
sudo ACCEPT_EULA=Y apt install -y mssql-tools unixodbc-dev
echo 'export PATH="$PATH:/opt/mssql-tools/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
echo "â
sqlcmd installed successfully."
else
echo "â
sqlcmd is already installed."
fi
# =========================
# Prompt to connect (manual)
# =========================
echo "âšī¸ To connect to SQL Server, run:"
echo "sqlcmd -S localhost,11433 -U SA -P \"<YourPassword>\""
chmod +x mssql_docker.sh
export SA_PASSWORD='YourStrong!Passw0rd'
./mssql_docker.sh
Output:

You can see :
The script first checks if Docker is installed on the system and installs it if necessary. It then ensures that a Docker volume is available to persist the SQL Server data. Next, it checks whether the container already exists; if not, it prompts the user to set the SA_PASSWORD environment variable before proceeding. The script then pulls the SQL Server image from Docker Hub (if not already available) and creates a new container using that image. After the container is running, it installs sqlcmd on the host, which is an essential tool for interacting with SQL Server. Finally, the script provides clear instructions on how to connect to the SQL Server instance using sqlcmd
