Latest posts by Adam Bertram (see all)
- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
If you don't already have the Docker extension, it is available via the Extension marketplace. Be sure to download and install it first if you'd like to follow along with this article.
Once you've installed the extension, and Visual Studio Code is up and running, let's see what it can do by creating a Dockerfile we can use to build a new container image.
To do this, I'll create a new file in my workspace called Dockerfile.
Inside my Dockerfile, I'll hit Ctrl+Space to bring up a list of snippets available with the Docker extension.
You can see above that Visual Studio Code knows we're in a Dockerfile and immediately presents us with context-relevant snippet suggestions to use.
The first thing I need to do is specify the image I'm going to be building from. Here, I'll specify the latest Microsoft IIS image, which I have already downloaded.
Now let's copy some files to the new container image by using the COPY snippet. You can see as soon as I begin typing the COPY command, Visual Studio Code again presents me with context-relevant information about this command with the help of the Docker extension.
I'm going to specify the wwwroot folder currently in my workspace as the source.
COPY wwwroot dest
Once I've specified my COPY command, I'll then hit Tab to move to the next field in the snippet. Hitting Tab takes me directly to dest where I can then type in the destination where I'd like to store the container.
COPY wwwroot c:/inetpub/wwwroot
Finally, I'm going to expose port 80 and then save this Dockerfile again with convenient snippet help.
Now that I've finished building the Dockerfile, I can actually build my container image right from Visual Studio Code instead of using the command line. Simply right-click on the Dockerfile and then click on Build Image.
This will prompt us to tag the new image. You can assign a new tag here or use the default. I'll just use the default and hit Enter. Once I do this, you'll see the dockerbuild command running in the terminal below.
After doing this, I'm going to click on the Docker icon in the activity bar on the left-hand side, and we can see the new container image listed under Images. Let's go ahead and right-click that and run this container.
Now that it's running, I can right-click on it and attach a shell.
When I find the IP address of the container within the shell and put it in my web browser, you can see that the website is running on the new container.
Thanks to the Docker extension for Visual Studio Code, we were able to author, deploy, and run a new container easily in very little time!