This example assumes that you are using a Windows computer and want to run a Docker Container for use with .Net Core Development.
- Install Docker Desktop: https://www.docker.com/products/docker-desktop
- Set Docker Desktop to use Linux Containers
- Open Docker Desktop Settings and share the drive where you store your code
- Download and install from https://gitforwindows.org/
- Run Git Bash from the Window Start button
Bash is a Linux shell that will allow you to interact with Docker. If you try to send commands to a Linux container from a Windows command prompt or powershell, a carriage return (\r) character will be appended to each command and Linux won't know what to do with it.
- From Git Bash: navigate to this project on you hard drive, for example if it is stored in
D:\Projects\HelloWebApiCoreDocker
then enter:cd /d/projects/hellowebapicoredocker
- List all of the files in this directory:
ls -g
The Dockerfile contains the commands to build the Docker Image.
- To build the image with the name "myapp":
docker build -t myapp .
Note that the space and dot:.
at the end of the command is required. It means that the Dockerfile to be used is in the current directory which is signified by.
in Linux.
My screenshot shows Docker using cached data as I have run this before. The first time you run this it will download the necessary files from the internet and may take a while.
- Once the build is complete, then we can create a container instance (or multiple instances) of the Docker Image (like instantiating an object from a class in C#) using:
docker run -i -p 7000:5000 -v //d//Projects//HelloWebApiCoreDocker//webapi:/app/webapi --name test myapp
Each instance needs to map ports from the host machine ie your Windows PC to ports inside the Docker Container for that ASP.Net Core application to uses. The application uses port 5000 which is mapped to port 7000 on your host machine using:-p 7000:5000
. We also want to give the container access to our source files so that when we update them, it can re-build and deploy them to the Kestrel server for us. This command creates a volume for us:
-v //d//Projects//HelloWebApiCoreDocker//webapi:/app/webapi
The double forward slashes are required to escape the forward slash for the directory path on a Windows host machine. If we were running this on a Linux host machine we would not need this. After the :
is the path inside the Docker Container to map the files to. Then we give this Docker Container instance the name of "test": --name test
. Finally we want to use the Docker Image that we built before, that we tagged as myapp
Because we used the -i
(interactive) option in the command, it will output the ASP.Net Core's output to Git Bash.
- To exit press
Ctrl+C
This won't stop the ASP.Net Core application or the Docker Container, it will only exit from the interactive session. - To see the Docker Containers currently on our system:
docker ps -a
This shows us that the Docker Container named "test" that we created from image: "myapp" has a status of being "Up" (running) for 6 minutes and has port 7000 being forwarded to port 5000.
- The ASP.Net Core application has an endpoint at: http://localhost:7000/api/hello, when you click on this link you should see a response in the browser.
- If you shutdown the Docker Container using:
docker kill test
, then refresh the browser, then the endpoint is no longer available. - To start the Docker Container again:
docker start test
- If you want to destroy the Docker Container, kill it and then prune:
docker kill test
thendocker container prune
- Open VS Code, select Open Folder and choose the root directory of this repository on your hard drive eg D:\Projects\HelloWebApiCoreDocker
- Select Debug -> Open Configurations
This will open the launch.json file. Notice the top configuration: ".NET Core Docker Attach" which has been added for a Docker Container with the name "test". If your container's name is different to this, then update this line "pipeArgs": ["exec", "-i", "test"], // NOTE: the 3rd argument is the container name
- From the dropdown at the top left of the window, change the selected debugger profile to ".NET Core Docker Attach"
- Now press F5 to start the debugger
- You will prompted for the process that you want to attach to, choose the one for "HelloWebApiCoreDocker.dll"
- Open the HelloController.cs file within: webapi/Controllers and place a breakpoint on the Get() method's return by pressing F9
- To hit this end point click on this link: http://localhost:7000/api/hello This will open a browser but it won't get a response as VSCode has hit the break point
- Press F5 in VSCode to return the response to the browser
- Exit Debugging by pressing Shift+F5
- Change the text returned by the HelloController Get() method and save the file.
- Refresh the browser window and after the ASP.Net Core application has restarted, then the new text will appear. This is because the Docker Container is running the ASP.Net Core application with the watch command:
dotnet run watch
, which will listen for changes in the source code, build and redeploy when necessary.