Image Hand Rail at The Ohio State University by Wendy Bayer
You can create, develop, and run a new C# (.NET) project just using
Docker without having to install dotnet
on your machine.
Not only does this make your projects portable, it also saves you time overall by not having to install (and manage multiple versions) of the underlying language, packages, framework, and dependencies on your own machine.
This time and hassle savings can really help when you just want to get a project up and started or when you want to try out or learn a new language or framework.
If you already have Docker installed on your machine, there's no need to install anything else to create, develop, and run your new C# (.NET) projects. Use the same "machine" and version to create your projects as you will to develop and run them.
The process is as simple as...
-
Start with (determine) the version that you want to use and find that base image (here you use the official Microsoft
dotnet
base images) -
Run the base image interactively with your local storage to create the new project
-
Add a multi-stage
Dockerfile
to your new project -
Build, develop, and run your project all in Docker
You will need both a dotnet SDK (Software Development Kit) base image and a runtime environment image for your new project. If you do not have a specific requirement for creating a new C# project using an older version of dotnet, it's always best to start with the latest proper release.
You can find the dotnet SDK and runtime images at the Microsoft Container Registry.
This example uses dotnet version 6.0, so...
- For the SDK base image:
mcr.microsoft.com/dotnet/sdk:6.0
- For the runtime base image:
mcr.microsoft.com/dotnet/runtime:6.0
You will first use the SDK image interactively as a bootstrap development environment to create your new dotnet project.
Then you will create a Dockerfile
for your new project
that uses the SDK image to build and develop your project
and the runtime image to run a deployable image.
Once you have your version and its base image, you can run that base
image (here the SDK which has the dotnet
command environment)
interactively to create your new project.
To actually create your new project on your machine, you will need to mount in to the running container your local filesystem where you want the new project created.
To create your new C# project in Docker...
-
Change to the directory where you want the project
-
Use the DOTNET SDK Version for the C# project you will be creating (e.g.
mcr.microsoft.com/dotnet/sdk:6.0
) -
Run the desired DOTNET SDK interactively (using
bash
command) and mount in your current directory (under/app
) where you want to create (dotnet new
) your new C# projectdocker run -it --rm -v $(pwd):/app -w /app mcr.microsoft.com/dotnet/sdk:6.0 bash
-
Once the container
bash
shell is running interactively, create the new C# project usingdotnet new
For example,
dotnet new console -n MyDockerProj.Con
-
Change directory to the newly created project directory
For example,
cd MyDockerProj.Con
-
Do a
dotnet restore
on the newly created projectdotnet restore
-
You've now created your new project and can exit the dotnet SDK container
exit
Use the same version of the Dotnet SDK and runtime as you used
to create the project in a
multi-stage Dockerfile
to create multiple and smaller more secure images.
Here you will use separate stages to be able to build a secure deployment (production) image and a development environment image that you can use to develop, build, and test your project using Docker.
This uses mcr.microsoft.com/dotnet/sdk:6.0
for the SDK
and mcr.microsoft.com/dotnet/runtime:6.0
for the runtime
deployment image.
Be sure to use your project
.dll
file name (this is using"MyDockerProj.Con.dll"
) or use your own entrypointCMD
.
Here is a sample C# project Dockerfile
that you can modify. This one
is for the new project you created above and contains a development
environment. It uses the SDK image to build the project (and for the
development environment) and the runtime image for the deployment image.
# --- Sample C# .NET App Dockerfile---
### Builder Image ###
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS sdk
# Add Any Builder Dependencies Here
# RUN apt-get update && apt-get --no-install-recommends -y install <package>
### Dev Environment ###
## To Build: docker build --no-cache --target devenv -t app-dev .
## To Run: docker run -it --rm -v $(pwd):/app app-dev
# ASSUMES Volume mounted source
# Runs as Root
FROM sdk AS devenv
# At least install vim (git is already present)
RUN apt-get update && apt-get --no-install-recommends -y install vim
# ASSUME project source is volume mounted into the container at path /app
WORKDIR /app
CMD bash
### Deployment ###
## To Build: docker build --no-cache -t app .
## To Run: docker run -it --rm app
## Builder Stage ##
FROM sdk AS builder
WORKDIR /app
# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out
## Deployment Image ##
# USE Runtime base image
FROM mcr.microsoft.com/dotnet/runtime:6.0
# Add a non-root user (deployer) to run the app
RUN adduser --disabled-password --gecos '' deployer
USER deployer
# Copy the source to /app
WORKDIR /app
COPY --from=builder --chown=deployer /app/out .
CMD ["dotnet", "MyDockerProj.Con.dll"]
For details and more information, see the Docker Documentation on Multi-Stage Builds
Use your added Dockerfile
to build and run the deployment image.
To build the deployment image (named app
), use the docker build
command
docker build --no-cache -t app .
To run the deployment image (named app
), use the docker run
command
docker run -it --rm app
Not only can you create a new dotnet project with just Docker, you can
also develop your new project using just Docker by volume mounting
in your local source code. You can use your favorite editor
or IDE on your local machine to edit your code if you like (or
this sample Dockerfile
installs vim
).
In the development environment you can edit, test, build, and
develop your project using the dotnet
command.
To build the devenv
image (named app-dev
), use the docker build
command
docker build --no-cache --target devenv -t app-dev .
To run the devenv
image (named app-dev
) interactively, use the
docker run
command with volume mounting of your source code for
the project
docker run -it --rm -v $(pwd):/app app-dev
Simply enter the exit
command to exit the development environment.
I learned the dotnet parts of this from the official Microsoft dotnet Tutorial: Containerize a .NET app and recommend that you try it yourself with the techniques in this post