In continuation to my previous blog, I’m going to showcase how Docker images works hand to hand with containers.
As you saw my docker is able to sense the SQL Server 2017 image, let’s go ahead and create a container from it which is running instance of the image.
Setting Up a “Container” From the Image
Before that I would like to bring few things about Dockers and worth knowing those facts. Totally deepending how familiar is someone with Docker. You may or may not be aware that containers are not stateful. Once you delete a container, everything is deleted. Means you’ve lost all your data/work/etc. However, Docker has a feature called “Volumes”, another way to retain “state” between docker instances. So when one instance is shut down the “state” is stored in a Volume. When another container instance is up respective volume provides the container with “state” from the previous instance. This way possible to use containers for databases.
Create volume container
Command to create container as follows:
My container’s name is sqldata
docker create -v /var/opt/mssql --name sqldata microsoft/mssql-server-linux /bin/true
This volume container still uses the image as its base. But we won’t be running SQL Server from this instance.
Creating SQL Server container
As volume container is created next step it to create a container to run SQL Server. This container will use the data volume container for the persisted data.
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Passw0rd' -p 1433:1433 --volumes-from sqldata -d --name sql-server microsoft/mssql-server-linux
In aforesaid command there are two environment variables
Both are required.
- userid is ‘sa’.
- password requirements as Per SQL Server 2017 on Linux. (8 characters minimum)
Once aforesaid command is executed successfully let’s go ahead check containers. It only shows the regular containers.
The volume container is hidden so need to use –a switch.
docker image ps –a
I noticed that the container I will be using to run SQL Server is on a port 1433 but data volume container’s status is “Created”. It doesn’t have a port or not exposed on a port.
Test the SQL Server 2017 on Mac Docker with SQL Command Line
The command lines tools for Mac are sql-cli. You’ve to have a NPM utility to install sql-cli interface.
You can install that with:
npm install -g sql-cli
Start it up with the mssql command.
To minimum you need connect is to identify the server (which is at localhost by default, you don’t need to specify the port) and the password. It will presume sa for userid.
If it’s successful, you’ll get some information followed by a new prompt, mssql.
mssql -s localhost –u sa –p
Wonderful! Everything is working as expected to far. My docker on MacBook is able to run SQL Server 2017 on Linux, seamlessly.
In my next blog, I’ll talk about the Microsoft SQL Server extension in Visual Studio code, running on MacBook. That would be real fun playing nicely with whole bunch of technologies. You may go ahead and download Visual Studio for MacBook from here.
Regional Head – DataPlatformGeeks & DPS2017 Core Team Member
Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook