Peter Kellner
Peter Kellner

Reputation: 15508

Problem Optimizing Docker Container Start With SqlServer on MacBook OSX

I'm a Docker newbie and have managed to create simple steps to create, start and load an image running sqlserver with a database backup. For me, it's three steps now.

docker pull mcr.microsoft.com/mssql/server:2019-latest
docker run --name SQL19c -p 1433:1433 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=xxxx" -v /Users/useraccount/sql:/sql -d mcr.microsoft.com/mssql/server:2019-latest
docker exec -it SQL19c /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'xxxx' -Q 'RESTORE DATABASE svcodecamp FROM DISK = "/sql/sv-small-2019.bak" WITH MOVE "361684_codecamp08_dat" TO "/var/opt/mssql/ata/codecamp08_dat.mdf", MOVE "361684_codecamp08_log" TO "/var/opt/mssql/data/codecamp08_log.mdf"'

This is on my macbook running OSX which I reboot frequently so I need to do this everytime I need to use SqlServer.

Questions with this:

1) Each time I do this, I have to increment the SQL19c to Sql19d (or next letter of alphabet) because I get error saying name in use. How to re-use same name?

2) If I rm the container, it needs to repull the full image (1gig). I need to just start it and reload the data, not pull the full image

3) Is there a more optimum way to start SqlServer and load the data without using too much of my battery every time I reboot my computer or restart docker?

(notice my backup file is on a docker share so I don't need to recopy that in)

Upvotes: 1

Views: 79

Answers (1)

Max
Max

Reputation: 7100

  1. This is because you already have a container with this name. Try to execute the command:
docker container list -a
  1. If you repull the image I think you remove the image NOT the container for remove the container you must run
docker container rm SQL19x 
  1. The way is to restart the container, drop database and then restore the database

Run ONCE: This create a SQL19x container

docker pull mcr.microsoft.com/mssql/server:2019-latest
docker run --name SQL19x -p 1433:1433 -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=xxxx" -v /Users/useraccount/sql:/sql -d mcr.microsoft.com/mssql/server:2019-latest

Now each time you restart the machine you must run the command below to start the container and restart the database.

docker container start SQL19x
docker exec -it SQL19x /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'xxxx' -Q 'DROP DATABASE svcodecamp'
docker exec -it SQL19x /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'xxxx' -Q 'RESTORE DATABASE svcodecamp FROM DISK = "/sql/sv-small-2019.bak" WITH MOVE "361684_codecamp08_dat" TO "/var/opt/mssql/ata/codecamp08_dat.mdf", MOVE "361684_codecamp08_log" TO "/var/opt/mssql/data/codecamp08_log.mdf"'

If you want to have a clean shutdown before to poweroff your machine execute

docker container stop SQL19x

Upvotes: 1

Related Questions