title: Creating a docker image with SQL Server (Linux) and a database of my own: database not found in container
body: <p>I'm starting with Docker and I need to create an image including SQL Server and my own database, to be later consumed in a multiple-containers stack.</p>
  
  <p>My host is a Windows 10 Pro workstation, but I'm going to use Linux containers for all these layers. So, first of all I'm going to create an image derived from <code>microsoft/mssql-server-linux:2017-latest</code>, with my own database in it.</p>
  
  <p>I'm following the steps listed at <a href="https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container</a>, i.e.:</p>
  
  <p>1.open an elevated Powershell prompt and execute:</p>
  
  <pre><code>docker pull microsoft/mssql-server-linux:2017-latest
  
  docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=mypwdhere" --name "sql1" -p 1401:1433 -v sql1data:/var/opt/mssql -d microsoft/mssql-server-linux:2017-latest
  </code></pre>
  
  <p>I can now ensure that the container is up with <code>docker ps -a</code>.</p>
  
  <p>2.copy a backup file into the container:</p>
  
  <pre><code>docker cp mydatabase.bak sql1:/home
  </code></pre>
  
  <p>3.I can use this command to get the logical names of the files to be restored:</p>
  
  <pre><code>docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "mypwdhere" -Q "RESTORE FILELISTONLY FROM DISK='/home/mydatabase.bak'"
  </code></pre>
  
  <p>4.once I get these names, I run the restore command:</p>
  
  <pre><code>docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "mypwdhere" -Q "RESTORE DATABASE [mydatabase] FROM DISK='/home/mydatabase.bak' WITH MOVE 'mydatabase' TO '/var/opt/mssql/data/mydatabase.mdf', MOVE 'mydatabase_log' TO '/var/opt/mssql/data/mydatabase.ldf'"
  </code></pre>
  
  <p>The restore command is successful.</p>
  
  <p>5.finally, I remove the bak file:</p>
  
  <pre><code>docker exec -it sql1 rm /home/mydatabase.bak
  </code></pre>
  
  <p>To check if all is OK, I can list the databases in the container with this command:</p>
  
  <pre><code>docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "mypwdhere" -Q "SELECT Name FROM sys.Databases"
  </code></pre>
  
  <p>When I execute this, I can see that my database is listed.</p>
  
  <p>6.I create an image from the container:</p>
  
  <pre><code>docker commit sql1 mydatabase-sql
  </code></pre>
  
  <p>I can run this image in a container like:</p>
  
  <pre><code>docker run --name mydb-sql -p 1401:1433 mydatabase-sql
  </code></pre>
  
  <p>Now the server listens at 127.0.0.1 (localhost) port 1401. I can successfully login with SSMS with: <code>localhost,1401</code>; user=<code>sa</code>; password=<code>mypwdhere</code>.</p>
  
  <p>The problem is that once connected I cannot see my database. I can only see the system databases like <code>master</code>, and nothing else. If I try to execute a query directly from the container, like this:</p>
  
  <pre><code>docker exec -it mydb-sql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "mypwdhere" -Q "USE [mydatabase]; SELECT COUNT(*) FROM Document"
  </code></pre>
  
  <p>I get an error telling me that this database does not exist. In fact, if I try listing the databases with this command:</p>
  
  <pre><code>docker exec -it mydb-sql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "mypwdhere" -Q "SELECT Name FROM sys.Databases"
  </code></pre>
  
  <p>I get only <code>master</code>, <code>tempdb</code>, <code>model</code>, <code>msdb</code>. What I'm missing here?</p>