Tooling to help debug your Sitecore Docker Containers

Tooling to help debug your Sitecore Docker Containers

The world of containers seems to be on a very fast paced development cycle at the moment from everyone - Docker, Microsoft and the Sitecore community at large.

If you have created your first Docker image and started your first Sitecore container, then no doubt you have read through the Sitecore Docker for Dummies posts by our good friend Mark Cassidy. For those of you jumping into the world of Sitecore Docker, we thought it might be helpful to share some recent updates to help you in the debugging world.

Visual Studio 2019

First off, upgrade to the latest version of Visual Studio 2019. Make sure you are on version 16.5+. I cannot stress this enough, the tooling is a really nice integration.

Then form the menu select View > Other Windows > Containers. 🎉

The Containers window will then show you a list of all the containers on your machine. You can select each indivisual container and the tab through the Environment Variables, mapped Ports, the Logs for the container and there is also a Files tab.

Visual Studio Containers - Variables

Visual Studio Containers - Ports

Visual Studio Containers - Logs

You can already browse files inside a container via PowerShell (docker exec -it CONTAINERID powershell), but for those of us that like a visual GUI this is great to take a look around the file system and even to open the files and see the contents.

Visual Studio Containers - Files

Visual Studio Containers - Open Files

You can also see a view the local images on your machine, grouped by registry.

Visual Studio Containers - Images

You can read more about these changes here: https://docs.microsoft.com/en-us/visualstudio/containers/view-and-diagnose-containers?view=vs-2019

I would also suggest taking a look at the PowerShell Remoting Explorer by Micheal West. It’s a great utility to have on your belt.

There are also some nifty Docker tools available in VS Code via the extension. Definitely worth checking these out as well.

Docker Dashboard

While you’re upgrading your software, make sure you have also upgraded to the latest version of Docker Desktop for Windows. As of writing, it is [2.2.0.5]. The latest minor update adds the Docker Dashboard feature. Again, this is information that is visible through from the command line (docker ps -a) but for those of us that are visual, this again adds a nice interface. The dashboard is somewhat similar to the Visual Studio integration, but it is useful to know for those team members who may not be using Visual Studio.

You can view all running containers, view the logs, inspect the environment variables/ports/mounts and also start/stop/restart/delete the application. There is also a link to open the application in the browser.

Docker Dashboard

Docker Dashboard - Container View

Debugging

At some point you will probably also want to debug your application. In the 3rd part of his series, Mark Cassidy walks us through the necessary steps. Fortunately, things have become a lot simpler due to the latest updates to Visual Studio and changes in the docker-images github repo.

First off, make sure you have installed the Visual Studio Remote Debugging Tools on your local machine. You can download them from here: https://visualstudio.microsoft.com/downloads/#remote-tools-for-visual-studio-2019

Next, the community Sitecore Docker Images repo has introduced a change in the entry point script which checks for the below volume mount, and if that exsists then it starts the remote debugger.

You no longer need to make any changes to the entrypoint parameter in your compose file, just the presence of this volume mount directory within the container is sufficient. Update your docker compose file to add the below mount:

cm:
  image: ${REGISTRY}sitecore-xp-spe-standalone:${SITECORE_VERSION}-windowsservercore-${WINDOWSSERVERCORE_VERSION}
  entrypoint: powershell.exe -Command "& C:\\tools\\entrypoints\\iis\\Development.ps1"
  volumes:
    - .\data\cm\wwwroot:C:\src
    - .\data\cm\logs:C:\inetpub\wwwroot\App_Data\logs\
    - C:\Program Files\Microsoft Visual Studio 16.0\Common7\IDE\Remote Debugger:C:\remote_debugger

That’s it, the remote debugger is now configured to run. Easy!

Note: Adjust the local Remote Debugger location according to your machine. If you enabled the tools as part of your Visual Studio install then it may be under a different folder, however the above should be correct if you installed the Remote Tools listed earlier.

If you are running the latest Visual Studio 2019, then you no longer need to add this volume mount. When you attach the debugger, the remote debugger tools are automatically downloaded and installed inside the container. Thank you Nick Wesselman for the tip

Thanks also to the added Docker support in the latest Visual Studio 2019, attaching to the container process is also much more simple. You no longer need to go through the process of figuring out the IP address and ports. Instead, just open up the Attach to Process window as normal (Ctrl + Alt + P or Debug > Attach to Process).

In the dialog, change the Connection Type to Docker (Windows Container). You can now hit Find in the Connection target option and you should be presented a list of all running containers. Select the container in which you wish to attach the debugger.

Be sure to select the Managed code option - breakpoints do not seem to be hit if you select Automatic - and also to check the Show processes from all users option in order to list the IIS Worker Process.

Visual Studio Docker integration - select container type

Visual Studio Docker integration - select docker container

Visual Studio Docker integration - attach to process

As you can see, debugging your Docker applications from Visual Studio 2019 is now a much simpler process. What are you waiting for, go and upgrade now!

Please get in touch if you have any questions.