Docker compose mssql server linux

Quickstart: Compose and ASP.NET Core with SQL Server

Estimated reading time: 6 minutes

This quick-start guide demonstrates how to use Docker Engine on Linux and Docker Compose to set up and run the sample ASP.NET Core application using the .NET Core SDK image with the SQL Server on Linux image. You just need to have Docker Engine and Docker Compose installed on your platform of choice: Linux, Mac or Windows.

For this sample, we create a sample .NET Core Web Application using the microsoft/dotnet:2.1-sdk Docker image. After that, we create a Dockerfile , configure this app to use our SQL Server database, and then create a docker-compose.yml that defines the behavior of all of these components.

Note: This sample is made for Docker Engine on Linux. For Windows Containers, visit Docker Labs for Windows Containers.

Create a new directory for your application.

This directory is the context of your docker-compose project. For Docker Desktop for Windows and Docker Desktop for Mac, you need to set up file sharing for the volume that you need to map.

Within your directory, use the dotnet:2.1-sdk Docker image to generate a sample web application within the container under the /app directory and into your host machine in the working directory:

Note: If running in Docker Desktop for Windows, make sure to use Powershell or specify the absolute path of your app directory.

Create a Dockerfile within your app directory and add the following content:

This file defines how to build the web app image. It uses the .NET Core SDK image, maps the volume with the generated code, restores the dependencies, builds the project and exposes port 80. After that, it calls an entrypoint script that we create in the next step.

The Dockerfile makes use of an entrypoint to your webapp Docker image. Create this script in a file called entrypoint.sh and paste the contents below.

Note: Make sure to use UNIX line delimiters. The script doesn’t work if you use Windows-based delimiters (Carriage return and line feed).

This script restores the database after it starts up, and then runs the application. This allows some time for the SQL Server database image to start up.

Create a docker-compose.yml file. Write the following in the file, and make sure to replace the password in the SA_PASSWORD environment variable under db below. This file defines the way the images interact as independent services.

Note: The SQL Server container requires a secure password to startup: Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols.

This file defines the web and db micro-services, their relationship, the ports they are using, and their specific environment variables.

Note: You may receive an error if you choose the wrong Compose file version. Be sure to choose a version that is compatible with your system.

Go to Startup.cs and locate the function called ConfigureServices (Hint: it should be under line 42). Replace the entire function to use the following code (watch out for the brackets!).

Note: Make sure to update the Password field in the connection variable below to the one you defined in the docker-compose.yml file.

Go to app.csproj . You see a line like:

Читайте также:  Как ставить линукс минт

The generated project uses sqlite by default. To use SQL Server, add this line to app.csproj :

The Sqlite dependency was at version 1.1.2 at the time of this writing. Use the same version for the SQL Server dependency.

Ready! You can now run the docker-compose build command.

Make sure you allocate at least 2GB of memory to Docker Engine. Here is how to do it on Docker Desktop for Mac and Docker Desktop for Windows. This is necessary to run the SQL Server on Linux container.

Run the docker-compose up command. After a few seconds, you should be able to open localhost:8000 and see the ASP.NET core sample website. The application is listening on port 80 by default, but we mapped it to port 8000 in the docker-compose.yml .

Go ahead and try out the website! This sample uses the SQL Server database image in the back-end for authentication.

Источник

Deploy and connect to SQL Server Docker containers

Applies to: SQL Server (all supported versions) — Linux

This article explains how to deploy and connect to SQL server docker containers.

For other deployment scenarios, see:

This article specifically focuses on using the mssql-server-linux image. The Windows image is not covered, but you can learn more about it on the mssql-server-windows Docker Hub page.

Before choosing to run a SQL Server container for production use cases, please review our support policy for SQL Server Containers to ensure that you are running on a supported configuration.

This 6-minute video provides an introduction into running SQL Server on containers:

Pull and run the container image

To pull and run the Docker container images for SQL Server 2017 and SQL Server 2019, follow the prerequisites and steps in the following quickstart:

This configuration article provides additional usage scenarios in the following sections.

Connect and query

You can connect and query SQL Server in a container from either outside the container or from within the container. The following sections explain both scenarios.

Tools outside the container

You can connect to the SQL Server instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections. Some common tools include:

The following example uses sqlcmd to connect to SQL Server running in a Docker container. The IP address in the connection string is the IP address of the host machine that is running the container.

If you mapped a host port that was not the default 1433, add that port to the connection string. For example, if you specified -p 1400:1433 in your docker run command, then connect by explicitly specify port 1400.

Tools inside the container

Starting with SQL Server 2017, the SQL Server command-line tools are included in the container image. If you attach to the image with an interactive command-prompt, you can run the tools locally.

Use the docker exec -it command to start an interactive bash shell inside your running container. In the following example e69e056c702d is the container ID.

You don’t always have to specify the entire container ID. You only have to specify enough characters to uniquely identify it. So in this example, it might be enough to use e6 or e69 rather than the full ID. To find out the container ID, run the command docker ps -a .

Once inside the container, connect locally with sqlcmd. Sqlcmd is not in the path by default, so you have to specify the full path.

Читайте также:  Windows error green screen

When finished with sqlcmd, type exit .

When finished with the interactive command-prompt, type exit . Your container continues to run after you exit the interactive bash shell.

Check the container version

If you want to know the version of SQL Server in a running docker container, run the following command to display it. Replace with the target container ID or name. Replace with the SQL Server password for the SA login.

You can also identify the SQL Server version and build number for a target docker container image. The following command displays the SQL Server version and build information for the mcr.microsoft.com/mssql/server:2017-latest image. It does this by running a new container with an environment variable PAL_PROGRAM_INFO=1. The resulting container instantly exits, and the docker rm command removes it.

The previous commands display version information similar to the following output:

Run a specific SQL Server container image

  • Starting with SQL Server 2019 CU3, Ubuntu 18.04 is supported.
  • Starting with SQL Server 2019 CU10, Ubuntu 20.04 is supported.
  • You can retrieve a list of all available tags for mssql/server at https://mcr.microsoft.com/v2/mssql/server/tags/list.

There are scenarios where you might not want to use the latest SQL Server container image. To run a specific SQL Server container image, use the following steps:

Identify the Docker tag for the release you want to use. To view the available tags, see the mssql-server-linux Docker hub page.

Pull the SQL Server container image with the tag. For example, to pull the 2019-CU7-ubuntu-18.04 image, replace in the following command with 2019-CU7-ubuntu-18.04 .

To run a new container with that image, specify the tag name in the docker run command. In the following command, replace with the version you want to run.

These steps can also be used to downgrade an existing container. For example, you might want to rollback or downgrade a running container for troubleshooting or testing. To downgrade a running container, you must be using a persistence technique for the data folder. Follow the same steps outlined in the upgrade section, but specify the tag name of the older version when you run the new container.

Run RHEL-based container images

The documentation for SQL Server Linux container images points to Ubuntu-based containers. Beginning with SQL Server 2019, you can use containers based on Red Hat Enterprise Linux (RHEL). An example of the image for RHEL will look like mcr.microsoft.com/mssql/rhel/server:2019-CU1-rhel-8.

For example, the following command pulls the Cumulative Update 1 for SQL Server 2019 container that uses RHEL 8:

Run production container images

The quickstart in the previous section runs the free Developer edition of SQL Server from Docker Hub. Most of the information still applies if you want to run production container images, such as Enterprise, Standard, or Web editions. However, there are a few differences that are outlined here.

You can only use SQL Server in a production environment if you have a valid license. You can obtain a free SQL Server Express production license here. SQL Server Standard and Enterprise Edition licenses are available through Microsoft Volume Licensing.

The Developer container image can be configured to run the production editions as well. Use the following steps to run production editions:

Review the requirements and run procedures in the quickstart. You must specify your production edition with the MSSQL_PID environment variable. The following example shows how to run the latest SQL Server 2017 container image for the Enterprise Edition:

By passing the value Y to the environment variable ACCEPT_EULA and an edition value to MSSQL_PID, you are expressing that you have a valid and existing license for the edition and version of SQL Server that you intend to use. You also agree that your use of SQL Server software running in a Docker container image will be governed by the terms of your SQL Server license.

Читайте также:  Linux для сервера nginx

For a full list of possible values for MSSQL_PID, see Configure SQL Server settings with environment variables on Linux.

Run multiple SQL Server containers

Docker provides a way to run multiple SQL Server containers on the same host machine. Use this approach for scenarios that require multiple instances of SQL Server on the same host. Each container must expose itself on a different port.

The following example creates two SQL Server 2017 containers and maps them to ports 1401 and 1402 on the host machine.

The following example creates two SQL Server 2019 containers and maps them to ports 1401 and 1402 on the host machine.

Now there are two instances of SQL Server running in separate containers. Clients can connect to each SQL Server instance by using the IP address of the Docker host and the port number for the container.

Upgrade SQL Server in containers

To upgrade the container image with Docker, first identify the tag for the release for your upgrade. Pull this version from the registry with the docker pull command:

This updates the SQL Server image for any new containers you create, but it does not update SQL Server in any running containers. To do this, you must create a new container with the latest SQL Server container image and migrate your data to that new container.

Make sure you are using one of the data persistence techniques for your existing SQL Server container. This enables you to start a new container with the same data.

Stop the SQL Server container with the docker stop command.

Create a new SQL Server container with docker run and specify either a mapped host directory or a data volume container. Make sure to use the specific tag for your SQL Server upgrade. The new container now uses a new version of SQL Server with your existing SQL Server data.

Upgrade is only supported between RC1, RC2, and GA at this time.

Verify your databases and data in the new container.

Optionally, remove the old container with docker rm .

Next steps

  • Get started with SQL Server 2017 container images on Docker by going through the quickstart
  • Get started with SQL Server 2019 container images on Docker by going through the quickstart

See the mssql-docker GitHub repository for resources, feedback, and known issues

Источник

Запуск ms-sql server в docker контейнере

Понадобилось на работе для тестов поюзать ms-sql server, благо он есть и для linux, иначе бы мне пришлось предать свою сущность… Итак приступим.

Требования:

– docker;
– docker-compose;
– linux.

Клонируем из git проект

Собираем образ

Далее правим docker-compose.yml

Приводим к необходимому виду:
– правим пароль – он должен соответствовать требованиям надежности мелкомягких – иначе не заведется.

Запускаем

Заходим внутрь докер контейнерв

Восстановим дамп из бэкапа

Иы пробросили каталог ./backups внутрь докер контейнера в каталог /backups.

Восстановим дамп из файла(например) GRN_MAIN.bak

Для этого выполним sql – просмотрим инфу о файле.

Нас интересует имя GRN_MAIN.mdf и его расположение, а также имя GRN_MAIN_1.ldf и его расположение.

Теперь произведем восстановление из дампа.

Ждем окончания. Всё ок.

Или из GUI для ms-sql – например – DBEAVER.
Сначала просмотрим инфу о файле.


Теперь восстановим


Сделать бэкап

Другие команды

Показать версию

Добавить комментарий Отменить ответ

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.

Источник

Оцените статью