In the initial rendition of the LearnWith book series, I gave instructions on how to create a database and populate it with SQL, but left no instructions or details on how to set up or install a DB Server from scratch. That was beyond the scope of the book.
About a year ago, I switched over from using a locally installed SQL Server to one powered by Docker. This would give my readers and myself a lot more flexibility, and one less thing to think about when reading my books to learn about Angular. In the process of creating the docker image for all to share, I took a ton of screenshots and a ton of notes but didn't have time to write it up until now. I'm using SQL Server as the basis however you should be able to morph these instructions for any type of Docker Image.
Here are my instructions for how to create and publish a Docker Image.
Create Your Docker Repo
First, you'll want to create an account and docker repository on the Docker Website. Go here and you should see something like this:For the purposes of this article, I am not going to connect it to a Github or Bitbucket account, because auto build is out of the current scope of this article.
Create the Docker Image
You'll need to install Docker on your dev machine. From there, you can create your first container:What does this command do? Let's dissect it:
- docker run: This command tells docker to start running something.
- e: The e argument is an environment variable, meaning you're passing some value into the Docker Image. In this case, I'm setting the default password and telling it to pre-accept the EULA. I'm also telling it which version of SQL Server to use--Express, the free version.
- p: The p argument is the port, it means the external port 1433, will redirect to the internal port 1433.
- d: This means the docker image runs in detached mode. It starts and returns you to your console, instead of showing you the console of the docker image.
- name: This argument is used to specify the name of our new docker image. I used learnwithdb, since I am creating a database to use with my learn with books.
- mcr.microsoft.com/mssql/server:2019-latest: The final piece of the command, isn't actually an argument; it is a reference to the docker image that we're making a copy of, in this case I'm copying the latest version of Microsoft's SQL Server 2019 docker image release.
You should see something like this:
No real response from the client, but once this was done, I was able to open up the server in SQL Server enterprise Manager, create the database, create the tables, populate it with some sample data. I'll refrain from going into details of this, since the focus on publishing a docker image.
Turn your Container into an Image
What we've created now is a container that is a copy of a default docker image provided by Microsoft. We need to turn that container into our own docker image. To do that we use the docker commit command:
You should see something like this:
I added a docker image ls at the end of the screenshot so we could see that the new image was indeed created. Grab the imageId and add a tag to it:
You should see something like this:
You may need to stop and delete the original running docker container before continuing. To stop it:
And then delete it:
When prepping for this, I neglected to grab screenshots of these two commands, sorry.
Start your new Docker Image
Now you can start up your new docker image:
This run command is a lot simpler than our previous one. Let's see what is here:
- docker run: This command tells docker to start running something.
- p: The p argument is the port, it means the external port 1433, will redirect to the internal port 1433.
- d: This means the docker image runs in detached mode. It starts and returns you to your console, instead of showing you the console of the docker image.
- name: This argument is used to specify the name of our new docker image. I used learnwithdb, since I am creating a database to use with my learn with books.
- learnwithdb: The final piece of the command, isn't actually an argument; it is a reference to the docker image that we're making a copy of, in this case I'm creating a container based off the image we just created, learnwithdb.
You should see something like this:
I added a docker ps command, to show me all the running containers, and we see our new one running. Grab the container ID and add a tag:
This tag will relate to how the image is found on the docker site, in this case it is found at https://hub.docker.com/repository/docker/learnwith/learn-with . I named it after my book series and then another one related to the DB.
It should look something like this:
I added an docker images at the end to listed all the images. You can see the original one we created, and the new tag.
Make the Image Available
Now, we can push image to repo with this command:
You'll see something like this:
Now, jump over to hub.docker.com to see your repos:
Now other people should be able to use or set up the image.
Final Test
I went ahead and used Docker Desktop to delete the images and containers I just created. Now try to run this:
You should see something like this:
Congratulations; you're done!
You can find my custom image at https://hub.docker.com/repository/docker/learnwith/learn-with.