Creating a custom SQL Server Docker Image on top of the Official Image

Creating a custom SQL Server Docker Image on top of the Official Image

ยท

3 min read

Hello people, so today let's discuss how we can create our own custom SQL Server image on top of the official image.

This can be really useful in multiple scenarios like when we have a new team member joining our team. Instead of giving them a fresh instance of SQL Server, we can have an image of the initial setup and they can just pull it and be ready to work on it

Pre-requisite

docker run -e "ACCEPT_EULA=Y" "SA_PASSWORD=MYPASSWORD123" -p 1433:1433 --name MyContainerName -d mcr.microsoft.com/mssql/server:2017-latest

Setting up the SQL Server ๐Ÿ”ฅ

  • Once we have the server up and running, log in to the SQL Server using SQL Server Management studio with the IP address of the host
  • Username would be SU and password is MYPASSWORD123 as used in the command above to run the container
  • Next we can set up our server like the database, tables manually or using any backup file, etc
  • Now we have our database in place and we want to create an image of this setup so as to when next time someone pulls the image they don't have to manually import the database anymore

Create Custom Docker Image

  • Firstly, stop the running container using the command
docker stop MyContainerName
  • Next we push our changes onto the container so that we can build an image of it
docker commit MyContainerName
  • Then copy the image of our specific container from the list using the command
Docker images
  • The newly created image does not have a repository and tag. Execute the following command to tag the image
docker tag <imageID> <docker-hub-username>/<docker-hub-repository-name>:<tag-name>

For example: docker tag a82e969d1395 rajatsrivas/ myownsql:sqlCustomImage

  • Now our image is built and we can create a container using the image
docker run -p 1433:1433 --name sqlCustomImage -d rajatsrivas/ myownsql
  • If you are logged into docker hub on local Docker Desktop this step will be skipped else login using the command prompt

docker login -username=rajatsrivas

  • Enter the password into the next line and finally push the image to the docker hub repository

docker push rajatsrivas/myownsql

  • Go to hub.docker.com
  • We should have the image which we pushed to the docker hub

image

Pull and Run our Custom Image ๐Ÿƒโ€โ™‚๏ธ

  • Pull the image onto any machine using the command

docker pull rajatsrivas/myownsql:latest

  • Run the container and access the server on the SSMS. The server should have the database which was imported and set up in the earlier steps
docker run -p 1433:1433 --name <container-name> -d rajatsrivas/myownsql:latest

Conclusion

So there it is we have implemented our custom image on top of an official docker image available.

This is quite a small step in onboarding but one can leverage similar implementations for setting up sandbox environments very quickly and efficiently.

Hope this was useful. Keep learning keep building

Did you find this article valuable?

Support StackUp by becoming a sponsor. Any amount is appreciated!

ย