LESSON 6 OF 14

6. Setting Up and Securely Configuring the SQL Server Container

In this lesson, you will run a SQL Server Linux container, configure the license agreement, SA password, edition, port mapping, and restart behavior, then verify connectivity with sqlcmd.

50 dk 67

Introduction

In the previous lessons, we covered Docker images, containers, port mapping, and the first containerization of an ASP.NET Core application. In this lesson, we will prepare the SQL Server environment that the application will use. Instead of installing SQL Server directly on the development machine, we will run the SQL Server Linux image in Docker and create a repeatable database environment.

Running a SQL Server container involves more than starting an image. The license agreement must be accepted, a strong sa password must be supplied, an edition must be selected, and the SQL Server port must be published appropriately. The container's status, logs, restart policy, and removal behavior should also be managed deliberately.

This lesson uses the SQL Server 2022 Linux image with the Developer edition. Developer edition is appropriate for development and testing; it does not replace production licensing decisions.

1. Understanding SQL Server Container Settings

The SQL Server image requires several environment variables during its first startup. The most important ones are:

VariableDescription
ACCEPT_EULAConfirms acceptance of the SQL Server license agreement. It is required for the image to start.
MSSQL_SA_PASSWORDSets the initial password for the sa account. Use this instead of the deprecated SA_PASSWORD name.
MSSQL_PIDSpecifies the SQL Server edition or product key. Developer is suitable for development.
MSSQL_TCP_PORTSpecifies the TCP port on which SQL Server listens inside the container. The usual default is 1433.

These variables are used during the initial container configuration. In particular, if database files are later reused through a volume, changing MSSQL_SA_PASSWORD may not automatically change the existing sa password. Therefore, changing a password and creating a new container are separate operations.

2. Running the SQL Server Image

First, verify that Docker is available:

docker version

Start SQL Server with the following command:

docker run -d \  --name sqlserver-dev \  --hostname sqlserver-dev \  --restart unless-stopped \  -e ACCEPT_EULA=Y \  -e MSSQL_SA_PASSWORD='YourStrong!Passw0rd' \  -e MSSQL_PID=Developer \  -p 127.0.0.1:1433:1433 \  mcr.microsoft.com/mssql/server:2022-latest

If you are using PowerShell, line continuation syntax is different. Running the command as a single line is the simplest option:

docker run -d --name sqlserver-dev --hostname sqlserver-dev --restart unless-stopped -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=YourStrong!Passw0rd" -e "MSSQL_PID=Developer" -p 127.0.0.1:1433:1433 mcr.microsoft.com/mssql/server:2022-latest

The -d option runs the container in the background. --name assigns a meaningful name so that you do not need to use a container ID in every command. --hostname sets the hostname inside the container. --restart unless-stopped allows the container to start again when Docker restarts, but it does not automatically restart a container that you intentionally stopped.

In -p 127.0.0.1:1433:1433, the first port is the host port and the second is the container port. Binding the host port only to the local loopback address prevents direct access from other machines on the network. This is a safer starting point for a development environment than using -p 1433:1433.

3. SA Password and Secure Configuration

SQL Server applies its default password policy. The sample password should be at least eight characters long and include characters from at least three of the following groups: uppercase letters, lowercase letters, digits, and symbols. Never use the sample password unchanged in a real project.

Although putting a password directly in a command is convenient during development, it may appear in shell history or process information. Safer approaches include:

  • Reading the password from a secrets manager or a protected CI/CD variable.
  • Avoiding the sa account for normal application connections in production.
  • Creating a separate SQL Server login with only the permissions the application needs.
  • Not publishing the SQL Server port to every network interface unless it is required.
  • Never placing passwords in a Dockerfile, where they could become part of image layers.

For this lesson, we will use sa only to verify connectivity. In later lessons, we will manage the ASP.NET Core connection string through environment variables and use a more controlled database configuration.

4. Checking Container Status and Logs

Check whether the container was created and is running:

docker ps

To include stopped containers in the output, use:

docker ps -a

Follow the SQL Server startup logs with:

docker logs -f sqlserver-dev

When SQL Server is ready, the logs will show messages indicating that the service is accepting connections. A connection attempt immediately after the image is downloaded may fail because the container being in an Up state does not necessarily mean that SQL Server is already ready to process queries. Inspect the logs or wait briefly before testing.

Press Ctrl+C to stop following the logs. This exits the log command but does not stop the container.

5. Connecting with sqlcmd Inside the Container

The SQL Server image generally includes command-line tools at /opt/mssql-tools18/bin/sqlcmd. After the server is ready, run the following query:

docker exec -it sqlserver-dev \  /opt/mssql-tools18/bin/sqlcmd \  -S localhost \  -U sa \  -P 'YourStrong!Passw0rd' \  -No \  -Q "SELECT @@VERSION AS SqlServerVersion;"

docker exec runs a new command inside a running container. -S localhost connects to SQL Server from inside the container. -U specifies the login name and -P supplies the password. Newer sqlcmd versions may require encryption by default; in this local development example, -No indicates that encryption is not mandatory.

To open an interactive sqlcmd session, omit the -Q option:

docker exec -it sqlserver-dev \  /opt/mssql-tools18/bin/sqlcmd \  -S localhost -U sa -P 'YourStrong!Passw0rd' -No

Then try these queries:

SELECT DB_NAME() AS CurrentDatabase; GO SELECT name FROM sys.databases; GO QUIT

The GO command separates batches. Type QUIT to leave the sqlcmd session.

6. Verifying Connectivity from the Host

If sqlcmd is installed on the host operating system, you can also test the connection from outside the container:

sqlcmd -S 127.0.0.1,1433 -U sa -P 'YourStrong!Passw0rd' -No -Q "SELECT DB_NAME();"

You can map a different host port to the container's SQL Server port. For example, use host port 11433 and container port 1433 as follows:

docker run -d --name sqlserver-dev --hostname sqlserver-dev --restart unless-stopped -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD='YourStrong!Passw0rd' -e MSSQL_PID=Developer -p 127.0.0.1:11433:1433 mcr.microsoft.com/mssql/server:2022-latest

In that case, the client connects to 127.0.0.1,11433. Connections made from inside the container still use the SQL Server port 1433. Keeping the host port and container port distinct is especially important when you later write the ASP.NET Core connection string.

7. Managing the Container Lifecycle

Stop and start the SQL Server container with:

docker stop sqlserver-dev docker start sqlserver-dev

You can change the restart policy without recreating the container:

docker update --restart unless-stopped sqlserver-dev

To remove the container, stop it first and then delete it:

docker stop sqlserver-dev docker rm sqlserver-dev

These commands remove the container. Because we have not attached a persistent volume yet, data created inside the container can also be lost when the container is removed. In a later lesson, we will mount a named volume at /var/opt/mssql. That will preserve database files even if the container itself is recreated.

Common Mistakes

Port already in use

If you see Bind for 127.0.0.1:1433 failed, another SQL Server instance or container may already be using the host port. Inspect the source with docker ps and operating-system port tools, or use -p 127.0.0.1:11433:1433 instead.

Password policy failure

If the container exits immediately, inspect its logs. SQL Server will not start if the password is too short or not complex enough. Use a strong password containing uppercase letters, lowercase letters, digits, and symbols.

Incorrect environment variable

You may find SA_PASSWORD in older examples. Current configurations should use MSSQL_SA_PASSWORD. Typographical errors in variable names can also prevent successful startup.

Testing too soon

A running container does not necessarily mean SQL Server is ready. Check docker logs sqlserver-dev and wait for the ready message before connecting.

Practice Exercise

  1. Create a new SQL Server container named sqlserver-lab using the Developer edition.
  2. Map host port 11433 to container port 1433, binding the host port only to 127.0.0.1.
  3. Follow the container logs and wait until SQL Server is ready.
  4. Connect with sqlcmd and execute SELECT @@VERSION; and SELECT name FROM sys.databases;.
  5. Stop and start the container, then verify that you can connect again.

After completing the exercise, inspect the name, status, and port mapping values in the output of docker ps -a.

Conclusion

In this lesson, we created a working SQL Server Linux container. We accepted the license agreement with ACCEPT_EULA, supplied a strong initial password with MSSQL_SA_PASSWORD, selected the edition with MSSQL_PID=Developer, and bound the host port securely to localhost.

We also learned how to inspect logs, connect with sqlcmd from inside and outside the container, use a different host port, and manage the basic container lifecycle. Next, we will make SQL Server data persistent with a volume and connect the ASP.NET Core application and database through a shared Docker network.

Sources