Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run SQL script after start of SQL Server on docker

I have a Dockerfile with below code

FROM microsoft/mssql-server-windows-express
COPY ./create-db.sql .
ENV ACCEPT_EULA=Y
ENV sa_password=##$wo0RD!
CMD sqlcmd -i create-db.sql

and I can create image but when I run container with the image I don't see created database on the SQL Server because the script is executed before SQL Server was started.

Can I do that the script will be execute after start the service with SQL Server?

like image 618
Art Base Avatar asked Jun 26 '18 20:06

Art Base


People also ask

How do I run SQL Server in Docker container?

Here are the steps you can follow to set up and deploy a SQL Server Docker Container seamlessly: SQL Server Docker Setup: Install Docker on your System. SQL Server Docker Setup: Execute and Run Docker. SQL Server Docker Setup: Pull & Run the Docker Image for SQL Server.


1 Answers

You can follow this link https://github.com/microsoft/mssql-docker/issues/11. Credits to Robin Moffatt. Change your docker-compose.yml file to contain the following

mssql:
image: microsoft/mssql-server-windows-express
environment: 
  - SA_PASSWORD=##$wo0RD!
  - ACCEPT_EULA=Y
volumes:
 # directory with sql script on pc to /scripts/
 # - ./data/mssql:/scripts/
  - ./create-db.sql:/scripts/
command:
  - /bin/bash
  - -c 
  - |
    # Launch MSSQL and send to background
    /opt/mssql/bin/sqlservr &
    # Wait 30 seconds for it to be available
    # (lame, I know, but there's no nc available to start prodding network ports)
    sleep 30
    # Run every script in /scripts
    # TODO set a flag so that this is only done once on creation, 
    #      and not every time the container runs
    for foo in /scripts/*.sql
      do /opt/mssql-tools/bin/sqlcmd -U sa -P $$SA_PASSWORD -l 30 -e -i $$foo
    done
    # So that the container doesn't shut down, sleep this thread
    sleep infinity
like image 181
Santander Avatar answered Sep 21 '22 07:09

Santander