Skip to content

Instantly share code, notes, and snippets.

@sleemer
Last active June 6, 2017 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sleemer/412a1152d041478bd9561fb5e6cdc8be to your computer and use it in GitHub Desktop.
Save sleemer/412a1152d041478bd9561fb5e6cdc8be to your computer and use it in GitHub Desktop.
Investigation report of how to set up debugging of aspnetcore:2 containerised app in @VScode
FROM microsoft/aspnetcore-build:2
ENV NUGET_XMLDOC_MODE skip
WORKDIR /vsdbg
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
unzip \
&& rm -rf /var/lib/apt/lists/* \
&& curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /vsdbg
RUN mkdir /app
WORKDIR /app
COPY Banco.Orders.Api.csproj /app
RUN dotnet restore
COPY . /app
RUN dotnet publish -c Debug -o out
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000
ENTRYPOINT ["/bin/bash", "-c", "if [ -z \"$REMOTE_DEBUGGING\" ]; then dotnet out/Banco.Orders.Api.dll; else sleep infinity; fi"]
imageName="banco.orders.api"
projectName="banco"
serviceName="orders.api"
containerName="${projectName}.${serviceName}_1"
publicPort=5000
url="http://localhost:$publicPort"
runtimeID="debian.8-x64"
framework="netcoreapp2.0"
workdir="src/Banco.Orders.Api"
# Kills all running containers of an image and then removes them.
cleanAll () {
if [[ -z $ENVIRONMENT ]]; then
ENVIRONMENT="debug"
fi
dockerFileName="Dockerfile"
if [[ $ENVIRONMENT != "release" ]]; then
dockerFileName="Dockerfile.$ENVIRONMENT"
fi
echo "$workdir/$dockerFileName"
if [[ ! -f "$workdir/$dockerFileName" ]]; then
echo "$ENVIRONMENT is not a valid parameter. File '$dockerFileName' does not exist."
else
docker stop $containerName
docker rmi -f $imageName
# Remove any dangling images (from previous builds)
danglingImages=$(docker images -q --filter 'dangling=true')
if [[ ! -z $danglingImages ]]; then
docker rmi -f $danglingImages
fi
fi
}
# Builds the Docker image.
buildImage () {
if [[ -z $ENVIRONMENT ]]; then
ENVIRONMENT="debug"
fi
dockerFileName="Dockerfile"
if [[ $ENVIRONMENT != "release" ]]; then
dockerFileName="Dockerfile.$ENVIRONMENT"
fi
if [[ ! -f "$workdir/$dockerFileName" ]]; then
echo "$ENVIRONMENT is not a valid parameter. File '$dockerFileName' does not exist."
else
echo "Building the image $imageName ($ENVIRONMENT)."
docker build -t $imageName -f "$workdir/$dockerFileName" $workdir
fi
}
runContainer () {
echo "Running a new container $containerName"
if [[ -z $(docker images -q $imageName) ]]; then
echo "Couldn not find an image named $imageName"
else
containerId=$(docker ps -f "name=$containerName" -q -n=1)
if [[ ! -z $containerId ]]; then
docker stop $containerId
docker rm $containerId
fi
docker run -d -p $publicPort:5000 --name $containerName $imageName
fi
}
# Shows the usage for the script.
showUsage () {
echo "Usage: dockerTask.sh [COMMAND] (ENVIRONMENT)"
echo " Runs build or compose using specific environment (if not provided, debug environment is used)"
echo ""
echo "Commands:"
echo " build: Builds a Docker image ('$imageName')."
echo " compose: Runs docker-compose."
echo " clean: Removes the image '$imageName' and kills all containers based on that image."
echo " composeForDebug: Builds the image and runs docker-compose."
echo " startDebugging: Finds the running container and starts the debugger inside of it."
echo ""
echo "Environments:"
echo " debug: Uses debug environment."
echo " release: Uses release environment."
echo ""
echo "Example:"
echo " ./dockerTask.sh build debug"
echo ""
echo " This will:"
echo " Build a Docker image named $imageName using debug environment."
}
if [ $# -eq 0 ]; then
showUsage
else
case "$1" in
"buildForDebug")
ENVIRONMENT=$(echo $2 | tr "[:upper:]" "[:lower:]")
export REMOTE_DEBUGGING=1
buildImage
runContainer
;;
"clean")
ENVIRONMENT=$(echo $2 | tr "[:upper:]" "[:lower:]")
cleanAll
;;
*)
showUsage
;;
esac
fi

The goal of this investigation to find out the way to build aspnet core app inside Docker container and use VS Code to debug it remotely.

*** Useful links

https://github.com/OmniSharp/omnisharp-vscode/wiki https://github.com/OmniSharp/omnisharp-vscode/wiki/Attaching-to-remote-processes https://github.com/Microsoft/vscode-docker https://blogs.msdn.microsoft.com/stevelasker/2016/09/29/building-optimized-docker-images-with-asp-net-core/ microsoft/generator-docker#130

*** Docker commands to be able to debug

docker run -it --rm -v $(pwd):/app microsoft/aspnetcore-build:2 -it means keep the container running in interactive mode --rm means remove the container when complete -v $(pwd):/sln means volume mount the present working directory to a root folder in the Linux image named app

{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Docker Attach (web)",
"type": "coreclr",
"request": "attach",
"preLaunchTask": "dockerBuildForDebug",
"processName": "dotnet",
"sourceFileMap": {
"/app": "${workspaceRoot}/src/Banco.Orders.Api"
},
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}/swagger",
"osx": {
"command": "open"
}
},
"pipeTransport": {
"pipeProgram": "docker",
"pipeCwd": "${workspaceRoot}",
"pipeArgs": [
"exec", "-i", "banco.orders.api_1"
],
"quoteArgs": false,
"debuggerPath": "/vsdbg/vsdbg"
}
}
]
}
{
"version": "0.1.0",
"command": "/bin/bash",
"options": {
"cwd": "${workspaceRoot}"
},
"tasks": [
{
"taskName": "dockerBuildForDebug",
"suppressTaskName": true,
"args": [
"-c",
"./scripts/dockerTask.sh buildForDebug debug"
],
"isBuildCommand": false,
"showOutput": "always"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment