Docker Networking

We use docker to stub out dependencies (things like API responses with wiremock) and quicky bring up standard infastructure like a SQL Database. This is great for local development as our local application can connect to the docker bridge network by using http://localhost:81 where 81 is the containers public port.

Stub out dependencies

The Problem

I ran into an intersting problem where I needed a containerized application (In this case K6 performance testing) to connect to both stubbed out dependencies and an application running at localhost:6000. This wouldnt be a problem in UAT / Production as everything would be either containerized or accessible to the container - this was purely a local development issue.

A quick hack was to add --network="host" and then specify as [IPADD]:6000 when spinning up the container, as the IP is not static this is not a good solution.

Docker network problem

The solution

The simplest way to allow communication for local development was to use the special DNS name host.docker.internal which resolves to the internal IP address used by the host.

Interestingly this worked both in the docker bridge network (so the K6 container could see other containers) and in the local pc’s network (so the container k6 container could see the local application on :6000)

Docker network solution

References