Maxime FRAPPAT

Hum …no thanks ! – Lordinaire

Tag: Docker

Work with Docker in Visual Studio 2017

Docker is a well-know tools in Microsoft ecosystem since few years. You can of course doing great things if you deal with Azure but it isn’t limited to “ops”. Developers can now easily play with Docker inside Visual Studio with some additional tools. Stuff like starting a container becomes so easy that everybody must use it! (I like Docker, a lot!)

Setup & Tools

First, you need to download and install Docker for Windows and select the .NET Core option in the installer of Visual Studio 2017.

Bonus: You can additionally download Kitematic (Visual Docker Container Management) if the CLI is not your friend.

Project sample

Let’s create a new ASP.NET Core 2.0 project of type WebAPI. The main benefit to target a .NET Core project is that it can run on Windows AND Linux platform.

Docker for Windows is able to run both of platforms type for a container but not a mixed use of it. So, you need that all your containers target the same platform, you can’t set a specific platform for a container. That’s the only limitation that I face off (and it can be a huge one…).

You can check the Enable Docker Support now or add it after when the solution will be created. If you doesn’t set the option, le solution explorer looks like that :

To activate the Docker support, right clic on the project and select Add > Enable Docker Support. Visual Studio will now automatically create a new project named docker-compose and a Dockerfile in your project. Well, thank you VS.

Dockerfile

FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

docker-compose.yml

version: '3'

services:
  webapplication1:
    image: webapplication1
  build:
    context: ./WebApplication1
    dockerfile: Dockerfile

Start the project

Before starting the project, we need to check if the shared drives settings are set properly because Docker required full access to drives where the source code.

Update 04/11/17 : This is only needed if you use Linux containers with Docker for Windows. You can avoid that by using the option Switching to Windows containers.

 

To run your project with Docker, you need to set the docker-compose project as startup project. Hit F5 and open the console window : you will see all the steps like downloading the image, creating the container, …

Bonus: You can add breakpoints like you always do and it will work!

Bonus 2: If you launch Kitematic you will see your container with all his settings

 

 

 

[Tips] Can’t share drives in Docker on Windows with Azure AD account

Few weeks ago, I tried to create a new ASP .NET Core Web Application with Visual Studio 2017 RC in order to use it with Docker.

The creation step worked fine, I can build the solution but if I launch it, it fails with that error :

ERROR: for webapplication3  Cannot create container for service webapplication3: C: drive is not shared. Please share it in Docker for Windows Settings

At first sigh, this is obvious that I need to share my C: drive to Docker. If I don’t do that, it can’t access to the project/container. So let’s go !

Here’s the few steps I made :

…but it seems that if you are logged with an Azure AD account, Docker failed to share drives. So, if you can’t wait for a fix, you can create a local account on your machine and repeat all the steps. That’s not an ideal solution but it works !

 

 

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: