Академический Документы
Профессиональный Документы
Культура Документы
This article describes how to containerize an ASP.NET WebApi solution with Visual Studio, how to pass data via environment
variables and also how to run the images in docker without Visual Studio.
Introduction
The tricky part when running a web solution with a web API in docker containers is to map the URLs and ports so that the code
running inside the docker container can be accessed from outside. This is a question of docker configuration and minor code
changes.
Background
This article is a contribution to the Docker Contest described in this article.
Prerequisites
Visual Studio 2017, community version. Latest release.
You have installed “Docker For Windows” on your computer: https://download.docker.com/win/stable/Docker for Windows
Installer.exe.
You have an existing solution with a web API and a web “model-view-controller“ project and the MVC project is able to
communicate with the web API via a REST http interface. If not, you may use the CarApi and CarClient projects (see
below) to implement your own solution.
The code belonging to this article is the containerized versions of CarClient and CarApi from this article.
In this article, docker support has been added and the docker configuration files have been updated to make it possible to access
the API from CarClient, both frontend and backend.
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 1/7
16/03/2019 Running ASP.NET Web API Solution in Docker - CodeProject
Do this for both projects in your existing solution, i.e., for the web API and the web MVC project. When this is done, you need to
add a docker-compose project to your solution.
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 2/7
16/03/2019 Running ASP.NET Web API Solution in Docker - CodeProject
The added project is of type “.dcproj” and the following files are created:
The next step is to right click the other project and in the same way, select Add -> Container Orchestrator Support ->Docker
Compose -> Target OS:Linux.
Suppose that your two projects are called “CarClient” and “CarApi”, then the resulting docker-compose.yml looks like this:
version: '3.4'
services:
web:
image: ${DOCKER_REGISTRY}carclient
build:
context: .
dockerfile: CarClient/Dockerfile
api:
image: ${DOCKER_REGISTRY}carapi
build:
context: .
dockerfile: CarApi/Dockerfile
Backend
In the original CarClient project, the web API was reached via the following URL:
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 3/7
16/03/2019 Running ASP.NET Web API Solution in Docker - CodeProject
Here, the URL is hard coded but we could also define an environment variable in launchSettings.json:
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"CarApiUrl": "http//localhost:54411/"
}
For the containerized solution, we use “dns discovery”. Docker networking, as well as kubernetes handles all this magic. Instead of
localhost, the name of the service, as defined in the docker-compose, is used. To call the CarApi, use http://carapi. You don’t
need to set the port number as the port number is an external attribute.
We will use the environment variable called CarApiUrl. This variable is defined in the docker-compose.yml file like this:
version: '3.4'
services:
...
carclient:
image: ${DOCKER_REGISTRY}carclient
environment:
- CarApiUrl=http://carapi/
build:
context: .
dockerfile: CarClient/Dockerfile
By using an environment variable, we don't need to change the code, only the configuration, when containerizing the solution.
Frontend
The JavaScript running in the browser uses port 54411. We must expose port 54411 by changing the docker configuration file for
CarApi like this:
In the web API Dockerfile, write EXPOSE 54411:
version: '3.4'
services:
carapi:
image: ${DOCKER_REGISTRY}carapi
ports:
- 54411:80
...
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 4/7
16/03/2019 Running ASP.NET Web API Solution in Docker - CodeProject
That’s all that's needed. You can now run your containerized solution in Visual Studio.
Outside of Visual Studio, you’ll need to use the docker-compose command rather than docker run. In PowerShell, cd to the
solution folder where docker-compose.yml is located. Then run the docker-compose command like this:
…> docker ps
Carclient runs on port 32781. The app will then become accessible at http://localhost:32781.
Explanation
If the Debug configuration is set, then empty non-workable images are created by Visual Studio. It manually maps the empty
container to the filesystem to make possible debugging, "Edit and Continue" features and so on. Therefore, dev image is useless
without Visual Studio. Build the image in the Release configuration to make it usable.
The full publishing process is described in the documentation: Visual Studio Tools for Docker.
cd to carapi
Inspect the docker bridge network to find the IP address used by carapi;
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 5/7
16/03/2019 Running ASP.NET Web API Solution in Docker - CodeProject
You see that carapi is using IP: 172.17.0.3. Use this ip number for the environment variable CarApiUrl in the docker run
command below:
cd to carclient
Now the containers communicate without YAML using the docker bridge network.
Points of Interest
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 6/7
16/03/2019 Running ASP.NET Web API Solution in Docker - CodeProject
In this article, we've discussed how to containerize an ASP.NET WebApi solution with Visual Studio, how to pass data via
environment variables and also how to run the images in docker without Visual Studio. We also see how to use docker networking
as an alternative to docker-compose and YAML files.
History
August 24, 2018 - The first revision of this article was published
September 1, 2018 - Environment variable was introduced
September 6, 2018 - Run the app without Visual Studio
March 6, 2019 - Run app with docker networking without docker-compose
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
I work as Senior Developer mainly in Microsoft environment and my strenghts are SQL, C# and ReactJS.
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2018 by Gunnar S
Web04 | 2.8.190306.1 | Last Updated 7 Mar 2019 Everything else Copyright © CodeProject, 1999-2019
https://www.codeproject.com/Articles/1257705/Running-ASP-NET-Web-API-Solution-in-Docker?display=Print 7/7