Page 19 - MSDN Magazine, July 2017
P. 19

Here’s the file listing for SqlCmdScript.sql, the same commands I used earlier when I was working directly from the command line:
create database juliedb;
GO
use juliedb;
create table people (PersonId int Primary Key, Name nvarchar(50)); insert into people values (1,'julie');
insert into people values (2,'giantpuppy'); select * from people
Next is the SqlCmdStartup.sh. Again, this is where I start up the sqlcmd utility and tell it to run the script I just listed:
#wait for the SQL Server to come up sleep 20s
#run the setup script to create the DB and the schema in the DB /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Passw0rd -d master -i SqlCmdScript.sql
I’m using just a simple set of parameters, although there’s a lot that you can control when building images.
The next file is entrypoint.sh, where I tell Docker what external processes to run. This is where I trigger the SqlCmdStartup bash script that runs sqlcmd, as well as the bash script inside the base image that starts up the SQL Server database. I combine the two commands using the & character:
#start the script to create the DB and data then start the sqlserver ./SqlCmdStartup.sh & /opt/mssql/bin/sqlservr
Notice that I’m running sqlcmd first. This is important and I strug- gled with this for a long time because of a misunderstanding. When Docker encounters a command that then completes, it stops the con- tainer. Originally, I ran the server startup first, then the sqlcmd. But
when Docker finished the sqlcmd, it decided it had finished its job and shut down. In contrast, when I run the server startup second, the server is just a long-running process, therefore, Docker keeps the container running until something else tells it to stop.
If, like me, you’re curious what’s in the sqlservr.sh script, take a look at bit.ly/2qJ9mGe, where I blogged about the file listing.
Finally, here’s the Dockerfile, which does a number of things:
FROM microsoft/mssql-server-linux
ENV SA_PASSWORD=Passw0rd ENV ACCEPT_EULA=Y
COPY entrypoint.sh entrypoint.sh
COPY SqlCmdStartup.sh SqlCmdStartup.sh COPY SqlCmdScript.sql SqlCmdScript.sql
RUN chmod +x ./SqlCmdStartup.sh
CMD /bin/bash ./entrypoint.sh
It first identifies the base image (mssql-server-linux), which, if not found on your machine, will be automatically pulled from Docker Hub. Then, it sets the environment variables so I don’t have to do that in the docker run command. Dockerfile then copies my two bash files and the SQL script file into the image. Next, it runs the chmod command, which permits my bash file to run inside the container. Finally, I instruct Docker what to do at the time that a new container is created from my image by specifying the CMD command to call the entrypoint bash script, which will in turn run sqlcmd with my TSQL and then start up SQL Server.
Build the New Image
With all of this in place, it’s time to build my new image with the docker build command:
docker build -t julielinuximage .
I’m using just a simple set of parameters, although there’s a lot that you can control when building images. I’m using only the -t param- eter here, which will force a default tag on the image, though you can also specify tags for versioning and other purposes. Then I specify the
Figure 2 The Final Lines of the Log Captured in Kitematic After Instantiating the Container with the Docker Run Command
msdnmagazine.com
name of the image and, finally, with the period at the end, I let Docker know that the Dockerfile I want to build from is in the current folder.
At this point, I have the image that I can publish somewhere to share with my team. Doing so requires a registry, but you don’t have to store your images on the Docker Hub. You can create a reg- istry on your own network, use a public or private registry on Azure, or choose one of the other myriad options for hosting Docker images. However, for this article, I’ll con- tinue to just work locally.
My next step is to run a con- tainer from my new image. Remember that because I put my environment variables in the Dockerfile, I don’t need to include
July 2017 15


































































   17   18   19   20   21