How to automate creating and running a "clean" SQL Server database starting from a backup using Docker and Docker Compose?

millimoose

I'm onboarding in a company that handles bootstrapping the database using a small SQL Server backup file. I prefer to avoid having to pollute my main Windows installation with various middleware, so I'd like to dockerize as much of this as possible.

That said, I'm not very familiar with SQL Server administration, so I'm somewhat at a loss as to how to accomplish the details, and if my thinking on this is at all correct.

I'm considering two basic approaches to this:

  • Make initializing the database (i.e. restoring the backup) part of the build for the database image. That is, I'd add a Dockerfile with FROM microsoft/mssql-server-windows-express to the project, restore the backup file, end up with a container image with the database ready as the end result.
    • The upside here is that it kind of makes sense for this to be part of the image build - if the initial backup file is updated, I only need to use docker-compose up --build to get a correct state.
    • The drawback is the data files should probably be in a Docker volume, and those don't really exist at container build-time. Having to remember to clear the volume before image rebuild to actually recreate a schema seems like it would kind of obviate the desired advantage.
  • Make a one-off tool to restore the database into a MDF+LDF stored in a Docker volume, then detach them from the server. Then use the attach_dbs environment variable to attach them in the SQL Server service that'll be running long-term.
    • This approach makes it obvious that the lifetime of the database files is independent from the lifetime of any given SQL Server instance.

My questions then are:

  1. Which of those approaches is a better idea, if they're even both at all workable?
  2. Is there a better approach to accomplish going from .bak -> working database in container?
  3. How do I restore, using the command-line, a SQL Server database backup to a specific path - i.e. "C:\Data" within the container. (That will be mapped to a host directory using a volume.)
Peter Wishart

Its not clear exactly when you need the state of the container database to be reset, both your options sound like they'd work.

In the event that changes to the backup require the database to be rebuilt, this can be done quite efficiently in a two stage windows container:

from microsoft/mssql-server-windows-developer as db_restore
copy db.bak \.

run Invoke-Sqlcmd -Query \"restore database [temp] from disk = 'c:\\db.bak' \
  with move 'Db_Data' to 'c:\\db.mdf', \
  move 'Db_Log' to 'c:\\db.ldf'\"

run Invoke-Sqlcmd -Query \"shutdown with nowait\"

from microsoft/mssql-server-windows-developer
workdir \data
copy --from=db_restore \db.mdf .
copy --from=db_restore \db.ldf .

run Invoke-Sqlcmd -Query \"create database [Db] \
  on primary ( name = N'Db_Data', filename = N'c:\\data\\db.mdf') \
  log on (name = N'Db_Log', filename = N'c:\\data\\db.ldf') for attach\"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to automate multi server deployment using docker

From Dev

Error creating database while running container with docker compose

From Dev

Prevent Docker Compose from creating separate network

From Java

How do I seed a mongo database using docker-compose?

From Dev

how do i backup a database in docker

From Dev

How to debug a Glassfish application running with Docker Compose

From Dev

Backup SQL Server database using WITH FORMAT

From Dev

Development workflow for server and client using Docker Compose?

From Dev

Docker Compose - How to store database data?

From Dev

how to backup of specific tables from database sql server and save .bak file in computer using vb.net code

From Dev

How to start a mysql docker container with a dump.sql file in a single command without using docker-compose

From Java

Docker Compose keep container running

From Dev

Docker-compose running container

From Dev

Problems with running docker-compose

From Dev

How to automate a docker run from a private Dockerhub repo?

From Dev

Automate Database backup in PostgreSQL

From Dev

Prevent public access to MySQL port running using Docker-compose

From Dev

Error when running node container using docker compose

From Dev

How to setup hostnames using docker-compose?

From Java

How to keep Docker container running after starting services?

From Dev

How to copy a file to a Docker container before starting/running it?

From Dev

Can't connect to MongoDB running from docker-compose

From Dev

Name an image built from running a docker-compose file

From Dev

My docker-compose is running, but I cannot reach from host

From Dev

Can't connect to MongoDB running from docker-compose

From Dev

Name an image built from running a docker-compose file

From Dev

How to run Redis on Docker using docker-compose.yml?

From Java

How do you perform Django database migrations when using Docker-Compose?

From Dev

Building and uploading images to Docker Hub, how to from Docker Compose?

Related Related

  1. 1

    How to automate multi server deployment using docker

  2. 2

    Error creating database while running container with docker compose

  3. 3

    Prevent Docker Compose from creating separate network

  4. 4

    How do I seed a mongo database using docker-compose?

  5. 5

    how do i backup a database in docker

  6. 6

    How to debug a Glassfish application running with Docker Compose

  7. 7

    Backup SQL Server database using WITH FORMAT

  8. 8

    Development workflow for server and client using Docker Compose?

  9. 9

    Docker Compose - How to store database data?

  10. 10

    how to backup of specific tables from database sql server and save .bak file in computer using vb.net code

  11. 11

    How to start a mysql docker container with a dump.sql file in a single command without using docker-compose

  12. 12

    Docker Compose keep container running

  13. 13

    Docker-compose running container

  14. 14

    Problems with running docker-compose

  15. 15

    How to automate a docker run from a private Dockerhub repo?

  16. 16

    Automate Database backup in PostgreSQL

  17. 17

    Prevent public access to MySQL port running using Docker-compose

  18. 18

    Error when running node container using docker compose

  19. 19

    How to setup hostnames using docker-compose?

  20. 20

    How to keep Docker container running after starting services?

  21. 21

    How to copy a file to a Docker container before starting/running it?

  22. 22

    Can't connect to MongoDB running from docker-compose

  23. 23

    Name an image built from running a docker-compose file

  24. 24

    My docker-compose is running, but I cannot reach from host

  25. 25

    Can't connect to MongoDB running from docker-compose

  26. 26

    Name an image built from running a docker-compose file

  27. 27

    How to run Redis on Docker using docker-compose.yml?

  28. 28

    How do you perform Django database migrations when using Docker-Compose?

  29. 29

    Building and uploading images to Docker Hub, how to from Docker Compose?

HotTag

Archive