Runar Ovesen Hjerpbakk

Programmer. Software Architect. Technical Manager.

DNS worked using Docker run but not in Docker Compose

I created an API internal to our organization today. The API needed to access an SQL Server database and worked perfectly running on my local machine on the corporate network.

Then the time came to run it in a Docker container. And it failed. The API could not connect to the SQL database.

The API connects to the SQL Server using a regular connection string:

Data Source=DB;Initial Catalog=Catalog;Persist Security Info=True;User ID=ID;Password=Password

But I didn’t understand how it couldn’t work from within the container. Luckily, my co-worker had a suggestion, try telling Docker the IP of our internal DNS server.

Using docker run, you can specify a DNS server using the --dns option:

docker run --dns 10.0.103.78 -p 1337:5000 hjerpbakk/api

The API worked perfectly.

Then I needed to get Docker Compose to work. Compose also has the dns option, so naturally, I tried this first:

version: '3'

services:
  dsak-api:
    image:  hjerpbakk/api${tag}
    restart: always
    ports:
      - 1337:5000
    dns: 10.0.103.78

But it turns out using bridged network mode was both necessary and sufficient:

version: '3'

services:
  dsak-api:
    image:  hjerpbakk/api${tag}
    restart: always
    ports:
      - 1337:5000
    network_mode: "bridge"

Every time I containerize an application I learn something new.