'Deploy SQL Server with pre-installed DB using Docker
I have created a docker image of SQL Server that includes a restored backup of an empty DB using the following dockerfile:
FROM mcr.microsoft.com/mssql/server:2019-latest AS build
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=myP@ss0rd#
ENV MSSQL_PID=Express
# Have a stage specifically to populate the data directory
FROM build AS data
WORKDIR /tmp
COPY TiPS_v.1.3.4.0.bak .
COPY restore-backup.sql .
RUN /opt/mssql/bin/sqlservr --accept-eula & sleep 10 \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "myP@ss0rd#" -i /tmp/restore-backup.sql \
&& pkill sqlservr
# Final stage that actually will actually be run.
FROM build AS release
# Copy the prepopulated data tree, but not the backup file
COPY --from=data /var/opt/mssql/data /var/opt/mssql/data
Running a docker container off of that image works fine and I can access the restored DB. But when I try to run using docker-compose with following yml file:
version: '3'
services:
mssql:
image: tips-db:1.3.4.1
container_name: "mssql-tips"
user: root
restart: always
environment:
ACCEPT_EULA: Y
volumes:
- ./mssql/data/:/var/opt/mssql/data
- ./mssql/log/:/var/opt/mssql/log
- ./mssql/secrets/:/var/opt/mssql/secrets
ports:
- 11433:1433
the restored DB is not there anymore. Only system databases are present.
I have only basic understanding of docker. I really can't understand why the .mdf, .ldf files of my Db are not persisted when then container is run. I can only see the files from system databases inside my local mssql/data folder.
Is there something I could do so that my restored DB is persisted after the container is run through docker-compose?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
