title: Restore a SQL Server DB.bak in a Dockerfile
body: <p>I am running a .NET Razor application, an instance of gitea, and a SQL Server database each in separate containers that communicate with one another. I would like to start my database image with a database schema and data (by restoring a <code>.bak</code> file).</p>
  <p>I can do this with my current Dockerfile, if once it is up and running, I run these additional commands:</p>
  <ol>
  <li><p><code>docker exec -it myContainer /opt/mssql-tools/bin/sqlsmd -S localhost -U sa -P myPassword</code></p>
  </li>
  <li><p><code>/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -Q &quot;RESTORE DATABASE MY_DB_NAME FROM DISK='/var/opt/mssql/backup/MY_DB_NAME.bak' WITH MOVE 'MY_DB_NAME_TEST' TO '/var/opt/mssql/data/MY_DB_NAME_TEST.mdf', MOVE 'MY_DB_NAME_TEST_log' TO '/var/opt/mssql/data/MY_DB_NAME_TEST_log.ldf'&quot;</code></p>
  </li>
  </ol>
  <p>This gets the job done, but I want to fully automate the process so that this is configured 100% by my docker-compose.yml and Dockerfile so I need only type: <code>docker-compose up -d</code>.</p>
  <p>I don't think the content of my docker-compose.yml file is relevant, but here is my Dockerfile (where I am trying to run that script that I currently need to run after <code>docker-compose up</code>):</p>
  <pre><code>FROM microsoft/mssql-server-linux
  
  ENV SA_PASSWORD=myPassword
  ENV ACCEPT_EULA=Y
  
  COPY ./ACES_DB.bak /var/opt/mssql/backup/MY_DB_NAME.bak
  RUN docker exec -it myContainer bin/sh /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -Q &quot;RESTORE DATABASE MY_DB_NAME FROM DISK='/var/opt/mssql/backup/MY_DB_NAME.bak' WITH MOVE 'MY_DB_NAME_TEST' TO '/var/opt/mssql/data/MY_DB_NAME_TEST.mdf', MOVE 'MY_DB_NAME_TEST_log' TO '/var/opt/mssql/data/MY_DB_NAME_TEST_log.ldf'&quot;  
  </code></pre>
  <p>Any help would be much appreciated.</p>