1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 创建自己SQL Server Docker映像

创建自己SQL Server Docker映像

时间:2021-12-05 03:20:21

相关推荐

创建自己SQL Server Docker映像

In this article, we will review how to create custom SQL Server docker images and run the containers from the custom images, upload the custom images to the docker hub.

在本文中,我们将介绍如何创建自定义SQL Server docker映像,以及如何从自定义映像运行容器,以及将自定义映像上传到docker hub。

Deploying SQL Server on docker really is quick and easy. Deploying SQL Server on docker includes pulling the SQL Server image from the repository and creating the container from the image. But the base SQL Server image comes with only the system databases and default configuration settings. In some cases, you may want to run a SQL Server container with your databases in place that are required for your application or you may want to run SQL Server containers with your own configuration already in place. To achieve this, we must build your own SQL Server image on top of the base image from Microsoft.

在docker上部署SQL Server确实非常快捷。 在docker上部署SQL Server包括从存储库中提取SQL Server映像并从该映像创建容器。 但是基本SQL Server映像仅附带系统数据库和默认配置设置。 在某些情况下,您可能希望在应用程序所需的数据库就位的情况下运行SQL Server容器,或者可能在已经拥有自己的配置的情况下运行SQL Server容器。 为此,我们必须在Microsoft的基础映像之上构建自己SQL Server映像。

Let us go through the few examples of building custom SQL Server docker images. In this demo, I used Docker on windows.

让我们来看一些构建自定义SQL Server docker映像的示例。 在此演示中,我在Windows上使用了Docker。

Download docker on Windows from /v17.09/docker-for-windows/install/

从/v17.09/docker-for-windows/install/在Windows上下载docker

download the package from the stable channel and install it.

从稳定频道下载软件包并安装。

Once the docker is installed, open the command prompt and verify the installation by running docker –version command. Please refer to the below image.

docker安装完成后,打开命令提示符并通过运行docker –version命令验证安装。 请参考下图。

Pull SQL Server Linux container image from the Microsoft container registry using the below command. In this case, I used SQL Server version developer edition.

使用以下命令从Microsoft容器注册表中提取SQL Server Linux容器映像。 在这种情况下,我使用了SQL Server 版开发人员版。

docker pull /mssql/server:-latest

Execute the following command to see the downloaded image.

执行以下命令以查看下载的图像。

docker images

Execute the following command to run the SQL Server container from the image /mssql/server:-latest.

执行以下命令以从映像/mssql/server:-latest运行SQL Server容器。

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Password" -p 1433:1433 --name RBC-d /mssql/server:-latest

Execute the below command to see if the container created from the SQL Server docker image is up and running.

执行以下命令以查看从SQL Server docker映像创建的容器是否已启动并正在运行。

docker ps

Now login to the SQL Server using SQL Server Management studio with the IP address of the host.

现在,使用带有主机IP地址SQL Server Management Studio登录到SQL Server。

By default, The Agent XP is disabled so I am enabling it by using sp_configure.

默认情况下,Agent XP被禁用,因此我使用sp_configure启用它。

For demo purposes, I am making a few configuration changes using sp_configure. In this case, I am enabling Agent XP and the configuration option called “remote admin connections”.

出于演示目的,我使用sp_configure进行了一些配置更改。 在这种情况下,我将启用Agent XP和称为“远程管理员连接”的配置选项。

exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Agent XPs',1reconfigureexec sp_configure 'remote admin connections',1reconfigure

If you do not have SQL Server management studio use SQLCMD to modify the configuration. To run commands against the container, execute the following command. Replace the name of the container with yours.

如果您没有SQL Server Management Studio,请使用SQLCMD修改配置。 要对容器运行命令,请执行以下命令。 用您的名称替换容器的名称。

docker exec -it RBC "bash"

Connect to the SQL Server using SQLCMD.

使用SQLCMD连接到SQL Server。

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA

Enter the password of “sa” login.

输入“sa”登录名的密码。

Once your login is successful, execute sp_configure to change the configuration options.

成功登录后,执行sp_configure更改配置选项。

Now we have changed the configuration to required values. Exit the bash.

现在,我们已将配置更改为所需的值。 退出重击。

Now stop the container and build the custom SQL Server docker image from the container. Execute the following command to stop the container.

现在,停止容器并从该容器构建自定义SQL Server docker映像。 执行以下命令以停止容器。

docker stop RBC

Check the status of the container using the below command.

使用以下命令检查容器的状态。

docker ps -a

Once the container is stopped we must build an image of the container so that we can run a container based on this image.

停止容器后,我们必须构建容器的映像,以便我们可以基于该映像运行容器。

docker commit RBC

Once you commit the container, a new image is created. We can check the newly created image using the following command.

提交容器后,将创建一个新映像。 我们可以使用以下命令检查新创建的图像。

docker images

The newly created image does not have a repository and tag. Execute the following command to tag the image.

新创建的映像没有存储库和标签。 执行以下命令以标记图像。

docker tag a82e969d1395 rangach99/my-own98:sqlcust

Breakout of the above command.

突破上述命令。

Image ID: a82e969d1395图片ID:a82e969d1395Docker hub username: rangach99Docker中心用户名:rangach99Docker hub repository: my-own98Docker中心存储库:my-own98TAG name: sqlcust标记名称:sqlcust

Now I am removing the previously created container by executing docker rm command. Please refer to the below image.

现在,我通过执行docker rm命令删除先前创建的容器。 请参考下图。

Now let us create the container from the new custom SQL Server docker image we built earlier. Execute the following command to create a container from the new image.

现在,让我们根据之前构建的新的自定义SQL Server docker映像创建容器。 执行以下命令从新映像创建容器。

docker run -p 1433:1433 --name rbccustom -d rangach99/my-own98:sqlcust

Login to the SQL Server using SQL Server management studio or using SQLCMD and check for the configuration changes we made. Please refer to the below images which shows the new container built from the custom image has Agent XP enabled and the configuration option “remote admin connections” enabled.

使用SQL Server Management Studio或SQLCMD登录到SQL Server,并检查我们所做的配置更改。 请参考下图,该图显示了从自定义映像构建的新容器已启用Agent XP并启用了配置选项“远程管理连接”。

We can create multiple instances with the same image. All the instances are identical with the same password for “sa” login.

我们可以使用同一张图片创建多个实例。 所有实例均使用相同的密码登录“sa”。

将自定义映像上传到Docker Hub (Upload the custom image to docker hub)

We can upload the custom SQL Server docker image we built to the docker hub so that we can download it from anywhere and run containers from the image.

我们可以将构建的自定义SQL Server Docker映像上载到Docker中心,以便我们可以从任何地方下载它并从该映像运行容器。

Docker push command is used to push the local image to the docker hub. Before pushing the image to docker hub we must have a docker hub account and a repository created.

Docker push命令用于将本地映像推送到docker hub。 在将映像推送到Docker Hub之前,我们必须创建一个Docker Hub帐户和一个存储库。

In my case, rangach99 is the docker hub account and my-own98 is the repository. the image should be properly tagged as shown above.

就我而言,rangach99是docker hub帐户,my-own98是存储库。 图像应正确标记,如上所示。

We need to login to the docker hub from the command prompt using docker command. Execute the below command.

我们需要使用docker命令从命令提示符登录docker hub。 执行以下命令。

docker login --username=rangach99

Enter the password of your login.

输入您的登录密码。

Execute the following command to push the custom SQL Server docker image to the docker hub.

执行以下命令以将自定义SQL Server docker映像推送到docker hub。

docker push rangach99/my-own98:sqlcust

Go to/and login to the docker hub using your docker hub user id and password. We can see the image which we pushed to the docker hub.

转到/并使用您的Docker Hub用户ID和密码登录Docker Hub。 我们可以看到我们推送到docker hub的图像。

We can now pull the image from the docker hub from anywhere and run the container from the downloaded image. Make sure you log in to the docker hub with an account that has privileges to pull the image.

现在,我们可以从任何地方从Docker集线器中拉取映像,然后从下载的映像中运行容器。 确保您使用具有拉取映像特权的帐户登录Docker集线器。

Execute the following command to pull the image from the docker hub repository.

执行以下命令以从docker hub存储库中提取映像。

docker pull rangach99/my-own98:sqlcust

使用适当的数据库构建映像。 (Build images with databases in place.)

We can build custom SQL Server docker images that have your databases already in place so that when you run the container from the image it deploys SQL server along with your database that is required for your application. Let us see step by step to build a custom image that has the database called “MyProductDB”.

我们可以构建具有数据库的自定义SQL Server docker映像,以便从映像运行容器时,它会部署SQL Server以及应用程序所需的数据库。 让我们逐步了解如何构建一个具有名为“ MyProductDB”的数据库的自定义映像。

Pull the base image from the Microsoft repository and start the container.

从Microsoft存储库中提取基本映像,然后启动容器。

docker pull /mssql/server:-latest

Start the container from the image you downloaded.

从下载的映像启动容器。

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Password" -p 1433:1433 --name RBC-d /mssql/server:-latest

Check the name of the container by executing the below command.

通过执行以下命令来检查容器的名称。

docker ps -a

Now we need to copy the backup of the database to container and restore the backup. Execute the following command to copy the backup fileMyProductDB.bakfrom D drive of your system to the path in a container called “RBC”

现在,我们需要将数据库的备份复制到容器中并还原备份。 执行以下命令,将备份文件MyProductDB.bak从系统的D驱动器复制到名为“ RBC”的容器中的路径中

docker cp D:\MyProductDB.bak RBC:/var/opt/mssql/data/MyProductDB.bak

Now verify if the backup file is successfully copied to a specified path in the container. To run commands against the container, execute the following command.

现在,验证备份文件是否已成功复制到容器中的指定路径。 要对容器运行命令,请执行以下命令。

docker exec -it "RBC" "bash"

Please refer to the below image that shows that backup file in the container.

请参考下图,该图显示了容器中的备份文件。

Login to the SQL Server using SQL Server management studio and restore the backup.

使用SQL Server Management Studio登录到SQL Server并还原备份。

Exit the bash and stop the container.

退出bash并停止容器。

Now commit the container to build an image out of it.

现在提交容器以从中构建图像。

docker commit RBC

Now tag the image you created.

现在标记您创建的图像。

Now you can push this image to your docker hub repository and pull it the from docker hub in another system and create a container from the image. This container will have SQL Server with the database you restored.

现在,您可以将该映像推送到您的Docker Hub存储库中,并将其从另一个系统的Docker Hub中拉出,并从该映像创建一个容器。 该容器将具有SQL Server,其中包含您还原的数据库。

使用Docker文件创建自定义SQL Server Docker映像 (Creating custom SQL Server docker images using Docker file)

We can also create custom images using docker file. First, we should create a docker file and build an image from it.

我们还可以使用docker文件创建自定义图像。 首先,我们应该创建一个docker文件并从中构建映像。

As I am using windows for demo purpose, I created a folder called “dockerfiles” under “C:\Users\yourusername” and copied the backup file to the folder “dockerfiles”.

当我使用Windows进行演示时,我在“C:\ Users \ yourusername”下创建了一个名为“dockerfiles”的文件夹,并将备份文件复制到了“dockerfiles”文件夹。

Now open notepad from start and paste the following code and save it as “Dockerfile” without any extension under the folder “dockerfiles”.

现在,从头开始打开记事本,粘贴以下代码,并将其保存为“Dockerfile”,文件夹“dockerfiles”下没有任何扩展名。

FROM /mssql/server:-latestENV ACCEPT_EULA=YENV SA_PASSWORD=Test@12345ENV MSSQL_PID=DeveloperENV MSSQL_TCP_PORT=1433 WORKDIR /srcCOPY MyProductDB.bak /dbbackups/ RUN (/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" &&/opt/mssql-tools/bin/sqlcmd -S127.0.0.1 -Usa -PTest@12345-Q"RESTORE DATABASE MyProductDB FROM DISK='/dbbackups/MyProductDB.bak';"

Open the command prompt and navigate to the folder “dockerfiles”. execute the following command to build an image from the docker file.

打开命令提示符,然后导航到文件夹“dockerfiles”。 执行以下命令从docker文件构建映像。

docker build -t rangach99/my-own98:SQLImagefromDockerFile .

Once the build is successful without any errors or permission issues, it creates a custom SQL Server docker image with the repository name as “rangach99/my-own98” and the tag name as “SQLImagefromDockerFile”.

一旦构建成功且没有任何错误或权限问题,它将创建一个自定义SQL Server docker映像,其存储库名称为“ rangach99 / my-own98”,标记名称为“ SQLImagefromDockerFile”。

Execute the below command to check the images.

执行以下命令检查图像。

docker images

Now let us create a container from the image we built above using the docker file. Execute the following command to create and start a container from the above-created image.

现在,让我们根据上面使用docker文件构建的映像创建一个容器。 执行以下命令从上面创建的映像创建并启动容器。

docker run-p 1433:1433 --name SQLfromDcokerFile -d rangach99/my-own98:SQLImagefromDockerFile

Let us connect to the SQL Server using SQL Server management studio and see if we have the required database.

让我们使用SQL Server Management Studio连接到SQL Server,看看我们是否具有所需的数据库。

We can see the databaseMyProductDBis created in the SQL Server.

我们可以看到数据库MyProductDB是在SQL Server中创建的。

结论 (Conclusion)

In this article, we explored how to create custom SQL Server docker images using docker commit command and building images from the docker file. In case you have any questions, please feel free to ask in the comment section below.

在本文中,我们探讨了如何使用docker commit命令创建自定义SQL Server docker映像并从docker文件构建映像。 如果您有任何疑问,请随时在下面的评论部分中提问。

To continue your learning about Docker, please refer to the Docker articles on SQL Shack.

要继续学习Docker,请参阅SQL Shack上的Docker文章。

翻译自: /creating-your-own-sql-server-docker-image/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。