title: How to wait for MSSQL in Docker Compose?
body: <p>I have a <em>service</em> (an ASP.NET Core Web application) that depends on MSSQL. The services are orchestrated using Docker compose, and I want docker compose to first start the database and wait for it to be <strong>ready</strong> before running my service. For that, I am defining the <code>docker-compose.yml</code> as:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>version: '3.7'
  
  services:
  
    sql.data:
      container_name: db_service
      image: microsoft/mssql-server-linux:2017-latest
      healthcheck:
        test: ["CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "http://localhost:1433", "-U", "sa", "-P", "Pass_word", "-Q", "SELECT 1", "||", "exit 1"]
  
    my_service:
      container_name: my_service_container
      image: ${DOCKER_REGISTRY-}my_service
      build:
        context: .
        dockerfile: MyService/Dockerfile
      depends_on:
        - sql.data
  </code></pre>
  
  <p>With this health-check, Docker compose does not wait for the database service to be ready, and starts <code>my_service</code> immediately after, and, as expected, <code>my_service</code> fails connecting to the database. Part of the log is: </p>
  
  <pre class="lang-bsh prettyprint-override"><code>Recreating db_service ... done
  Recreating my_service_container ... done
  Attaching to db_service, my_service_container 
  my_service_container | info: ...Context[0]
  my_service_container |       Migrating database associated with context Context
  my_service_container | info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
  my_service_container |       Entity Framework Core 3.1.1 initialized 'Context' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=MyService
  my_service_container | fail: Context[0]
  my_service_container |       An error occurred while migrating the database used on context Context
  my_service_container | Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
  ...
  exception details
  ...
  my_service_container | ClientConnectionId:00000000-0000-0000-0000-000000000000
  my_service_container exited with code 0
  db_service | 2020-03-05 05:45:51.82 Server      Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64)
          Nov 30 2018 12:57:58
          Copyright (C) 2017 Microsoft Corporation
          Developer Edition (64-bit) on Linux (Ubuntu 16.04.5 LTS)
  2020-03-05 05:45:51.82 Server      UTC adjustment: 0:00
  2020-03-05 05:45:51.82 Server      (c) Microsoft Corporation.
  2020-03-05 05:45:51.82 Server      All rights reserved.
  2020-03-05 05:45:51.82 Server      Server process ID is 4120.
  2020-03-05 05:45:51.82 Server      Logging SQL Server messages in file '/var/opt/mssql/log/errorlog'.
  2020-03-05 05:45:51.82 Server      Registry startup parameters:
           -d /var/opt/mssql/data/master.mdf
           -l /var/opt/mssql/data/mastlog.ldf
           -e /var/opt/mssql/log/errorlog
  </code></pre>
  
  <p>As shown in the logs, the docker compose first starts the DB, but does not wait for it become ready before running my service. </p>
  
  <p>I tried different syntax for the <code>healthcheck</code>, e.g.,: </p>
  
  <pre><code>test: /opt/mssql-tools/bin/sqlcmd -S http://localhost:1433 -U sa -P ${SA_PASSWORD} -Q "SELECT 1" || exit 1
  </code></pre>
  
  <p>But neither worked as expected. </p>
  
  <p>I have checked the following sources online, but using neither I was able to solve the problem: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/41854997/947889">stackoverflow answer</a></li>
  <li><a href="https://github.com/Microsoft/mssql-docker/issues/133#issuecomment-382635343" rel="noreferrer">github comment</a></li>
  <li><a href="https://github.com/microsoft/sql-server-samples/blob/1d93e1e66ffd633058fb6be9da68877baae7237d/samples/development-frameworks/SqlServerOnDocker/docker-compose.yml#L14" rel="noreferrer">github sample</a></li>
  </ul>
  
  <p>Is this functionality even supported in <code>version 3.7</code>? <a href="https://github.com/moby/moby/issues/30404#issuecomment-323212621" rel="noreferrer">because of this confusing comment</a></p>
  
  <hr>
  
  <h2>Question</h2>
  
  <p>Any thoughts on how best I can wait for MSSQL service to start?</p>