官术网_书友最值得收藏!

Running databases in containers

Now I have an image that can work for new deployments and upgrades. The image can be used by developers who might run it without mounting a volume while they're working on a feature, so they can start with a fresh database every time they run a container. And the same image can be used in environments where the existing database needs to be preserved by running the container with a volume that contains the database files.

This is how you run the NerdDinner database in Docker, using the default administrator password, using a host directory for the database files, and naming the container so I can access it from other containers:

mkdir -p C:\databases\nd

docker container run -d -p 1433:1433 ` --name nerd-dinner-db ` -v C:\databases\nd:C:\data ` dockeronwindows/ch03-nerd-dinner-db

The first time you run that container, the Dacpac will run to create the database, saving the data and log files in the mounted directory on the host. You can check whether the files exist on your host with ls, and the output from docker container logs shows the generated SQL script running and creating resources:

> docker container logs nerd-dinner-db
VERBOSE: Starting SQL Server
VERBOSE: Changing SA login credentials
VERBOSE: No data files - will create new database
Generating publish script for database 'NerdDinner' on server '.\SQLEXPRESS'.
Successfully generated script to file C:\init\deploy.sql.
VERBOSE: Changed database context to 'master'.
VERBOSE: Creating NerdDinner...
VERBOSE: Changed database context to 'NerdDinner'.
VERBOSE: Creating [dbo].[Dinners]... ...

The run command also publishes the standard SQL Server port 1433, so you can connect to the database running inside the container remotely through a .NET connection or with SQL Server Management Studio (SSMS). If you already have a SQL Server instance running on your host, you can map the container's port 1433 to a different port on the host.

To connect to the SQL Server instance running in the container with SSMS, Visual Studio, or Visual Studio Code, just use the container's IP address, select SQL Server Authentication, and use the sa credentials:

Then, you can work with the dockerized database just like any other SQL Server database, querying tables and inserting data. From the Docker host machine, you use the container's IP address as the database server name, but by publishing the port, you can access the containerized database outside of the host, using the host machine name as the server name. Docker will route any traffic on port 1433 into SQL Server running on the container.

主站蜘蛛池模板: 杭锦旗| 巫溪县| 廉江市| 西昌市| 石阡县| 栖霞市| 格尔木市| 广安市| 客服| 吉木萨尔县| 广德县| 镇原县| 漳州市| 普安县| 喀喇沁旗| 平泉县| 托克托县| 泸定县| 陇西县| 浦县| 叶城县| 余江县| 德阳市| 邯郸县| 延庆县| 宣化县| 睢宁县| 保亭| 张掖市| 富源县| 治县。| 望奎县| 绥中县| 南岸区| 万安县| 中超| 额敏县| 丘北县| 从化市| 和林格尔县| 怀柔区|