The Docker extension for Visual Studio Code by Microsoft has a lot of handy features when it comes to generating the files necessary for building and deploying Docker containers. By integrating directly with Visual Studio Code, the Docker extension lets you manage your images and containers without ever leaving your editor.

Adam Bertram

Adam Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

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.

Creating a Dockerfile

Creating a Dockerfile

Inside my Dockerfile, I'll hit Ctrl+Space to bring up a list of snippets available with the Docker extension.

Available snippets

Available snippets

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.

FROM microsoft/iis:latest

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.

Using the COPY snippet

Using the COPY snippet

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.

EXPOSE snippet help

EXPOSE snippet help

EXPOSE 80

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.

Building the image

Building the 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.

Image progress

Image progress

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.

Running the container

Running the container

Now that it's running, I can right-click on it and attach a shell.

Attaching a shell

Attaching 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.

Viewing the container web page

Viewing the container web page

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!

Win the monthly 4sysops member prize for IT pros

1+
Share
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account