Running .NET Applications that Communicate Over a Network Using Docker Compose
Download full source code.
In this post, I will show you how to run two .NET applications in Docker containers and have them communicate with each other. I will use Docker Compose to create the network, build the images, and run the containers.
The first application is a .NET Web API with an endpoint that returns a random number when called.
The second application is a .NET Worker Service that periodically calls the Web API endpoint.
Why do this? Well, it’s fun for one thing, and it’s a great way to learn about Docker Compose, its networking features, and build capabilities.
1. The Applications
There are two applications, both have a Dockerfile
and a .dockerignore
file.
One important thing to note is that the .dockerignore
file blocks copying the bin
and obj
directories if you have them. See this post for more details on that.
1.1 The .NET Web API Application
This is simple. I’m not going to show the source code here, it is all in the attached zip file.
Its /
endpoint returns a random number between 1 and 100.
1.2 The .NET Worker Service Application
Again, a simple Worker Service that calls the Web API endpoint every 2 seconds and prints the returned number.
2. The Docker Compose File
This is the fun part of the post.
The docker-compose.yml
will build the two images, create the network, and make sure that the Web API application is started before the worker service.
Here it is. There are inline comments to explain what is going on.
yml
files are very sensitive to indentation. If copy/pasting doesn’t work, use the file in the attached zip.
name: dotnet_app_to_app # this is the name of the docker-compose project, and how it will appear in Docker desktop
networks:
app_to_app_network: # this is how the network will be referenced in the services
name: app_to_app # this is the actual network name as it will appear in Docker
driver: bridge
services:
webapiindocker: # this is the network name of this container, and how it will be referenced in other containers
build: ./WebApiInDocker # path to the Dockerfile for this service
image: webapiindocker:1.0 # this is the image name and tag
container_name: webapiindocker # name of the container as it will appear in Docker
networks:
- app_to_app_network # network to use for this container
workertocalldockerapi:
build: ./WorkerToCallDockerApi
image: workertocalldockerapi:1.0
container_name: workertocalldockerapi
depends_on:
- webapiindocker # ensure this service starts after the webapiindocker service
command:
- "webapiindocker" # the name of the web api service passed as a command line argument
networks:
- app_to_app_network
3. Building and Running the Applications
To build and run the applications, open a terminal in the directory where the docker-compose.yml
file is and run the following command -
docker compose -f ./docker-compose.yml up
This will build the images, create the network, and start the containers.
After a few seconds, you should see output from the worker service showing the random numbers returned from the Web API.
If you want to force a rebuild of the images from the compose command, add the --build
flag -
docker compose -f ./docker-compose.yml up --build
Of course, you can add other services to the docker-compose.yml
file as needed. Docker Compose makes it easy to create applications that use other services like Kafka, Redis, CockroachDB, etc.
Download full source code.