Run command after docker container is started

2019-03-01 17:51发布

问题:

I've prepared a docker-compose file to deploy container with database:

services:
  tmp-db:
    image: microsoft/mssql-server-linux:latest
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: yourStrong(!)Password
    ports:
    - 1433:1433

It is okay. But now I need to create a database and build its structure. I need to execute some sql commands. To check if i am able to do this I added command to the service:

services:
  tmp-db:
    image: microsoft/mssql-server-linux:latest
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: yourStrong(!)Password
    command: /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"
    ports:
    - 1433:1433

However I got the following errors:

tmp-db_1  | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
tmp-db_1  | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
tmp-db_1  | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

I feel the command is executed before Sql Server instance is started. How can I fix it? How can I execute some sql after Sql Server is started?

回答1:

The issue is that only one command is executed in a container. When you specify the command in docker-compose.yml it overrides the default command, which was supposed to start the container. So you have two options

Run the command manually

services:
  tmp-db:
    image: microsoft/mssql-server-linux:latest
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: yourStrong(!)Password
    ports:
    - 1433:1433

Then you can execute

docker-compose exec tmp-db /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"

Have two services - one for server & one for data loading

services:
  load-db:
    image: microsoft/mssql-server-linux:latest
    command: sh -c 'sleep 10 && /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"'
    network_mode: service:tmp-db
  tmp-db:
    image: microsoft/mssql-server-linux:latest
    environment:
      ACCEPT_EULA: Y
      SA_PASSWORD: yourStrong(!)Password
    ports:
    - 1433:1433

In this approach we launch another container with the command to load our data, we run it on the network of our DB server container. This is done just to avoid the host name of the DB, if you prefer you can pass the host name also as tmp-db and remove the network_mode: service:tmp-db.