Use Case
I want to run uTorrent (a popular torrent client) on my machine in an isolated way. This use case is a perfect fit for Docker, and there's already an exiting image on Docker Hub 🎉, so it seems like a no-brainer.
Also, to manage that uTorrent container, I want to create a simple .NET Core app with which I'll build any feature I want, like automatic queueing.
Of course, it never works the first time
Everything was working fine while developing locally on my machine until I decided to run my application inside a container instead of a process running on my computer.
Boom! The network connection between my two containers doesn't work. It turns out that 127.0.0.1
doesn't mean the same thing when the DownloadService
runs on the host machine or inside a Docker container. So how can I find the right IP address to point to then?
By default, Docker will provision a virtual network called
bridge
and join any containers to it.
Docker network inspect
Let's diagnose what's going on with the bridge
network using the following command-line:
docker network inspect bridge
With that command line, we can find the IP address of the uTorrent container and call it from our DownloadService container. Everyone is happy, and we can move on... or can we 🤔?
Docker-compose and networking
The previous solution works fine, but it's highly unreliable as Docker will assign IPs dynamically, and we have absolutely no warranties that it will always be the same.
Fortunately, there are a few different ways we can fix this issue. We can assign a fixed IP with some parameters on our docker run
command. However, the new recommended way for local deployments that involve more than one container is to use Docker-Compose. Docker-Compose is simply another YAML file that defines a configuration for a group of containers. By default, docker-compose will join the defined containers in the same network AND associate them with a routable DNS record—something the default bridge
network doesn't do.
version: '3.4'
services:
downloadservice:
image: ${DOCKER_REGISTRY-}downloadservice
build:
context: .
dockerfile: DownloadService/Dockerfile
uTorrent:
image: ekho/utorrent:latest
ports:
- "8080:8080"
- "6881:6881"
- "6881:6881/udp"
With that docker-compose configuration, we can call the uTorrent
directly by its name instead of its IP address. Voilà!
var client = new UTorrentClient("uTorrent", 8080, "admin", "");