How to Use the Docker Extension for Visual Studio Code to Build a Dockerfile

51 VIEWS

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. In this post, we’ll create a Dockerfile that we can use to build a new container image.

First, I’ll create a new file in my workspace called Dockerfile.

Inside my Dockerfile, I’ll hit Control-Space to bring up a list of snippets that are available with the Docker extension.

First I need to specify the image that I’m going to be building from. Below, 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. I’m going to specify the wwwroot folder that is currently in my workspace as the source. Then I’ll hit Tab to move to the next field in the snippet, where I’ll specify my wwwroot folder on the container as the destination.


COPY wwwroot c:/inetpub/wwwroot

Finally, I’m going to expose port 80 and then save this Dockerfile.

Now that I’m finished, I can actually build my container image right from VS Code instead of using the command line. Simply right-click on the Dockerfile, then click on Build Image.

You’ll be prompted to tag the new image. I’m going to use the default tag and hit Enter.


You’ll see the Docker build command running in the terminal below.


Once that’s done, 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 want to attach a shell so that I can get this container’s IP address.



When I take the IP address and put it in my web browser, it’s possible to see the website is running on the new container.

Thanks to the Docker extension for VS Code, we were able to easily author, deploy, and run a new container in very little time!

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Matt McElreath is a Windows Server administrator concentrating on automation, PowerShell, Desired State Configuration (DSC), Octopus Deploy, and anything else thrown his way. You can follow Matt on Twitter at @mmcelreath.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

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

Menu