How to run MS SQL database server in Docker

In this article, we will show how to run a database server in Docker. A running database is necessary for any ORIGAM development. We will use both commercial MS SQL Server and open source PostgreSQL relational database system.

In case you don’t have Docker installed on your computer yet, please do it now. Here are general instructions.
If you are completely new to Docker, you may find useful this and also this video.

MS SQL Server

Image download

First, you need to download an image from which you can run your container.

:information_source: If you have an M1 or newer Mac, check this guide.

In your command line (CMD or PowerShell in MS Windows or Terminal on MacOS), run the Docker pull command to download the image with the database server. You can use your own image, find any available in DockerHub or the following one:

docker pull mcr.microsoft.com/mssql/server:2022-latest

Container creation

Once the image is downloaded on your machine, you can create the container. Use your command line again, and this time run the following command. But before you do so, please replace the string yourStrong(!)Password with any password of your choice.

docker run -d -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=yourStrong(!)Password" --name mssql2022 -p 1433:1433 mcr.microsoft.com/mssql/server:2022-latest
IMPORTANT INFORMATION ON DATA SECURITY !

It is important to know that all the data stored in the database running in the Docker container is stored only in that container, and if you lose that container (for example, if you accidentally delete the container), you will lose all your data as well.

The solution is to use a Docker volume (save to an isolated Docker repository) or a bind mount (the database and data are saved to a local file on your machine).

Example of using Docker volume:

-v mssql_data:/var/opt/mssql \

Using bind mount:

-v C:\sql-backups:/var/opt/mssql/backup (for MS Windows)
-v $HOME/sql-data:/var/opt/mssql/backup (for MacOS)

So if you want to keep your databases and data on your local disk and you are using MS Windows, create a container using this command:

docker run -d -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=yourStrong(!)Password" --name mssql2022 -p 1433:1433 -v C:\sql-backups:/var/opt/mssql/backup mcr.microsoft.com/mssql/server:2022-latest

The backup will then be in your local folder C:\sql-backups. You can, of course, customize the name of the local folder in the script.

Once you start the container with the mount, all changes in the database are immediately written to local files in your folder.

Docker mount works in real time - you don’t have to do anything manually. Every INSERT, UPDATE, DELETE is immediately reflected in those local files.

So when you shut down the container and restart it, all the data will be there because it’s physically stored on your machine in that folder.

Now there is a Docker container with the MS SQL database server running in your local Docker.

If you plan to use this database server not just for training purposes but in production, consider also disabling the SA account as best security practice as described here.

In case you are having any trouble, you can check full instructions from Microsoft.

PostgreSQL

Running the PosgreSQL database system in a Docker container is easy. Below are scripts for both MacOS and Windows that create and run the database on your local machine. In both cases we create the user “postgres” with “MyPassword123” for later use.

MacOS

Here is a comprehensive script generated by Claude AI LLM for MacOS Terminal that creates a and starts a Alpine Linux based container with the PostgreSQL database system.

All you need to do is edit some of the information in the introductory Configuration section, such as the name of the container in Docker or the user password, and run it in Terminal.

#!/bin/bash

# Configuration
CONTAINER_NAME="PostgreSQL"
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="MyPassword123"
PORT="5432"
VOLUME_NAME="postgres_data"

echo "🚀 Starting PostgreSQL in Docker..."

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker is not running. Start Docker Desktop."
    exit 1
fi

# Stop (or remove) old container (if exists)
if [ "$(docker ps -aq -f name=$CONTAINER_NAME)" ]; then
    
    echo "❌ Docker container with that name already exists."
    exit 1

    # Or you can delete it if you prefer that
    # echo "🔄 Removing old container..."
    # docker rm -f $CONTAINER_NAME
fi

# Start new container
docker run -d \
    --name $CONTAINER_NAME \
    --restart unless-stopped \
    -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
    -p $PORT:5432 \
    -v $VOLUME_NAME:/var/lib/postgresql/data \
    postgres:17-alpine

# Wait for database to start
echo "⏳ Waiting for database to start..."
sleep 5

# Health check
if docker exec $CONTAINER_NAME pg_isready -U $POSTGRES_USER > /dev/null 2>&1; then
    echo "✅ PostgreSQL is running!"
    echo ""
    echo "📝 Connection details:"
    echo "   Host: localhost"
    echo "   Port: $PORT"
    echo "   User: $POSTGRES_USER"
    echo "   Password: $POSTGRES_PASSWORD"
    echo ""
    echo "🔌 Connect: docker exec -it $CONTAINER_NAME psql -U $POSTGRES_USER"
else
    echo "❌ Something went wrong. Check logs: docker logs $CONTAINER_NAME"
    exit 1
fi

Windows

Simplified script for Windows CMD:

docker run -d ^
    --name PostgreSQL ^
    --restart unless-stopped ^
    -e POSTGRES_PASSWORD=MyPassword123 ^
    -p 5432:5432 ^
    -v postgres_data:/var/lib/postgresql/data ^
    postgres:17-alpine

General information about PostgreSQL you can find on their website.