0

I run SQL Server in container

docker run --network=bridge --name sql29 -h sql29 -it --rm -v h:/sql219data:/var/opt/mssql -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=sQL_19[pwd]" -p 12433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

as described here: https://learn.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-ver15

I see the active docker

docker ps

But if I try to create the new folder :

docker exec -it sql29 mkdir /var/opt/mssql/bkp22

then the docker disappeared.

docker ps
.........

How to understand: why the docker disappeared? Maybe the volume was mapped incorrectly?

Thom A
  • 88,727
  • 11
  • 45
  • 75
ZedZip
  • 5,794
  • 15
  • 66
  • 119
  • 1
    Output of `docker logs sql29` ? – AymDev Jan 04 '22 at 15:48
  • I'm not familiar with docker so I could be *far* off base here, but I assume that `-v h:/sql219data:/var/opt/mssql` mounts the hosts path `h:/sql219data` to the container's `/var/opt/mssql`. Running that while the SQL Server service is running would likely be a bad idea, as that's where it store is database, log, configuration and "secrets". Did the host have copies of all the files in the directory `/var/opt/mssql` before you overwrote it? – Thom A Jan 04 '22 at 15:54
  • 1
    Your `exec` command most probably crashed the main process in the container (I have no idea why though). Since you ran the container with `--rm`, the container will be removed as soon as it stops (for whatever reason) leaving you with no logs or anything else. You can either watch the logs in a separate terminal while you issue the exec command to see what is happening (`docker logs -f sql29`) or eventually drop the `--rm` option to be able to see the logs after the crash (`docker logs sql29`). – Zeitounator Jan 04 '22 at 16:02
  • @Larnu the host directories take over the container ones with bind mounts. The host directory won't get emptied on container startup. The tutorial OP linked shows the usage of named volume, I guess that the `h:/sql219data` directory is empty when starting. – AymDev Jan 04 '22 at 16:03
  • 1
    Please see the following note in the tutorial you have linked: `Host volume mapping for Docker on Windows does not currently support mapping the complete /var/opt/mssql directory. However, you can map a subdirectory, such as /var/opt/mssql/data to your host machine.` – Zeitounator Jan 04 '22 at 16:06
  • 1
    In other words, you **need** to use a named volume and **not** a bind mount because docker will copy the actual content of `/var/opt/mssql` inside the image to a freshly created volume. This will not happen with a bind mount. – Zeitounator Jan 04 '22 at 16:11
  • @AymDev : I have tried this >docker logs sql29 , but no any log. It seems Zeitounator is right. The docker removed. I will try to do not use --rm. – ZedZip Jan 05 '22 at 06:41
  • May be I do something wrong: my goal was to keep created/restored databases between docker runs. Is it possible? – ZedZip Jan 05 '22 at 06:43

1 Answers1

1

As @Zeitounator commented, the tutorial you linked has a note saying that bind mounts don't work on Windows with the /var/opt/mssql directory:

Host volume mapping for Docker on Windows does not currently support mapping the complete /var/opt/mssql directory. However, you can map a subdirectory, such as /var/opt/mssql/data to your host machine.

You commented that your goal was to keep or restore databases between docker runs. You don't need a bind mount to do that, this is the primary purpose of a volume. You can create a volume using docker volume create:

docker volume create sql219data

Then run your container using this volume:

docker run -v sql219data:/var/opt/mssql # ...

For debugging purposes you can remove the --rm option from your docker run command so the container won't be removed when stopped. You will then be able to read the logs of the container (even if it stopped):

docker logs sql29
# Then remove it to run the same `docker run` command again
docker rm sql29
docker run # ...
AymDev
  • 6,626
  • 4
  • 29
  • 52
  • Thank you. One question : if I create volume as you described and then use it >docker run -v sql219data:/var/opt/mssql where this volume is in my host? – ZedZip Jan 05 '22 at 11:09
  • @ZedZip that's a good question. Docker will store it in its internal directories (location depends on the OS/setup but check [this question](https://stackoverflow.com/questions/43181654/locating-data-volumes-in-docker-desktop-windows)). It means that you *can* find it but you shouldn't touch it to avoid breaking things. Generally you should use **volumes** to persist things you don't change by yourself (often database storage folders) and use **bind mounts** otherwise (e.g. your source code). – AymDev Jan 05 '22 at 11:57