AppDeveloper
AppDeveloper

Reputation: 2210

docker container dies after bash command and sql execution

At first docker container boots up fine but soon after the delaye and execution of the schema.sql file, it disconnects and dies.

Maybe there's something wrong with entry-point.sh or following command line.

command: bash -c "/opt/mssql/bin/sqlservr & chmod +x entry-point.sh && ./entry-point.sh"

docker-compose.sql

database:
  image: microsoft/mssql-server-linux:2017-latest
  container_name: database
  ports:
    - 1433:1433
  volumes:
    - /var/opt/mssql
    - ./assets:/assets
  working_dir: /assets
  command: bash -c "/opt/mssql/bin/sqlservr & chmod +x entry-point.sh && ./entry-point.sh"
  environment:
    SA_PASSWORD: "Password"
    ACCEPT_EULA: "Y"

entry-point.sh

#!/usr/bin/env bash

#wait for the SQL Server to come up
sleep 10s

#run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Password -d master -i schema.sql

schema.sql

CREATE DATABASE DataDemo;
GO

USE DataDemo;
GO

Edit tried different approaches but unable to set up database in docker with initialization script.

Edit2

following answer, Loader gave an error

enter image description here

Upvotes: 1

Views: 737

Answers (1)

David Maze
David Maze

Reputation: 159697

As soon as that command: finishes, the container exits. In your case you're starting a background process and then running two commands. Once those two commands complete the container is done; it doesn't matter that the background process is the thing you actually wanted the container to do.

Most of the standard database images provide some means to pre-populate application data; if I've found the right Dockerfile, Microsoft's SQL server images don't have this capability. (The PostgreSQL entrypoint script to do this is rather involved and I probably wouldn't try to replicate it on my own.)

One straightforward path to do this is to do the initialization in another container.

version: '3'
services:
  database:
    image: microsoft/mssql-server-linux:2017-latest
    ports:
      - 1433:1433
    volumes:
      - /var/opt/mssql
    environment:
      SA_PASSWORD: "Password"
      ACCEPT_EULA: "Y"
  loader:
    image: microsoft/mssql-tools:2017-latest
    volumes:
      - ./assets:/assets
    command: /assets/entry-point.sh
    restart: no
    environment:
      SA_HOST: database
      SA_PASSWORD: Password

(You could build a custom image that included the loader script instead of mounting it into the container, and use something more robust like the wait-for-it script to check that the database is running.)

Upvotes: 1

Related Questions