How to Run Microsoft SQL in minutes using Docker Desktop
Published: 2023-02-11 07:55:25

Docker Desktop for Windows is a Windows-based application that provides a user-friendly interface for developers to build, package, and ship applications as containers. It includes all the necessary tools and dependencies to get started with container-based development, including the Docker Engine, Docker CLI, and Docker Compose.

Docker Desktop for Windows integrates with the Windows operating system, allowing developers to build and run containers directly on the Windows host. It also includes a virtualization layer that allows containers to run in a lightweight Linux environment on Windows, providing a consistent development experience across different operating systems.

## Why to use Docker Desktop for Windows?

One of the key benefits of Docker Desktop for Windows is that it simplifies the process of developing and testing applications in containers, making it easier for developers to build, test, and deploy their applications. With Docker Desktop for Windows, developers can work with their applications and dependencies in isolated containers, easily moving between different environments, such as development, testing, and production.

In addition, Docker Desktop for Windows provides support for multi-stage builds, allowing developers to create small, optimized images for production, and manage large, complex applications in development. It also integrates with other development tools and platforms, such as Visual Studio, to provide a seamless and integrated development experience.

## Running Microsoft SQL Server using Docker Desktop

Here's a step-by-step guide to run Microsoft SQL Server using Docker Desktop:

## Install Docker Desktop

You can download Docker Desktop from the official Docker website. Once installed, make sure the Docker daemon is running.

## Pull the Microsoft SQL Server image

Use the following command in a terminal or command prompt to pull the latest version of the Microsoft SQL Server image:

```
docker pull mcr.microsoft.com/mssql/server:latest
```

The output will look something like this:

```
latest: Pulling from mssql/server
6ecb52f16b1e: Pull complete
faa26b6a67c8: Pull complete
b9cd86b9ac9d: Pull complete
84a30c44e708: Pull complete
b66d8b7a4072: Pull complete
...
Digest: sha256:3ba5cad69bdd5f4ab6c3c3d3b1e24e6fdbb6e06f0ed2ea6a5468e2234b058c84
Status: Downloaded newer image for mcr.microsoft.com/mssql/server:latest
```

This output shows that the latest version of the Microsoft SQL Server image has been pulled and is now available locally on the Docker host.

## Start a new container

Use the following command to start a new container from the Microsoft SQL Server image:

```
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrongPassword' -p 1433:1433 -d mcr.microsoft.com/mssql/server:latest
```

This will start a new container with the environment variable ACCEPT_EULA set to Y to accept the End-User License Agreement and the environment variable SA_PASSWORD set to yourStrongPassword to set the password for the SA (system administrator) account. The container will map port 1433 on the host to port 1433 in the container.

## Connect to the SQL Server instance

You can connect to the SQL Server instance using a SQL client, such as SQL Server Management Studio, and use the host IP address (localhost) and the port number (1433) to connect to the SQL Server instance. The username is SA and the password is yourStrongPassword.



For example, to connect to SQL Server using SSMS, you can enter the following information:

```
Server type: Database Engine
Server name: <Docker host IP>,1433
Authentication: SQL Server Authentication
Login: sa
Password: yourStrongPassword
```

Or, to connect using the sqlcmd utility, you can run the following command in a terminal or command prompt:

```
sqlcmd -S <Docker host IP>,1433 -U sa -P yourStrongPassword
```

Replace <Docker host IP> with the IP address of the Docker host where the container is running. The sqlcmd utility allows you to run Transact-SQL commands and scripts against SQL Server.


## Using Docker Compose

Here is a sample docker-compose.yml file that starts a Microsoft SQL Server container and maps port 1433 on the host to port 1433 in the container:

```
version: '3'
services:
  sql-server:
    image: mcr.microsoft.com/mssql/server:latest
    environment:
      ACCEPT_EULA: 'Y'
      SA_PASSWORD: 'yourStrongPassword'
    ports:
      - 1433:1433
```

You can use the docker-compose up command to start the container defined in this file. The docker-compose up command reads the docker-compose.yml file and starts the specified services. The first time you run docker-compose up, Docker Compose will download the specified image and start the container.

Note that the docker-compose down command can be used to stop and remove the container and any associated resources created by docker-compose up.