Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #35: Connect to a Database Running on Your Docker Host

docker-tips-and-tricks.jpg

Once in a while you may want to connect a container to a database or service running on your Docker host. Here's how to do it.

Quick Jump:

A lot has chanced with Docker since this post was created, Check out this post for an updated solution.

I get it, you might not trust Docker well enough to run your database in a container (with a Docker volume to deal with persistence).

Fortunately you can easily have a container connect to any service that’s installed on your Docker host. This means you could install your database / service directly on your Docker host and then connect to it from a running Docker container.

You can simply connect to your local network IP address.

You can figure out your local network IP address by looking for the IP address that belongs to the same subnet as your router (assuming you’re using one). It’s very likely going to be a 192.x.x.x or 10.x.x.x address.

Find Your Local Network IP Address on MacOS / Linux:

$ ifconfig
eth2      Link encap:Ethernet  HWaddr d0:50:99:4a:e8:68
          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::c542:1540:8737:c9cd/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

if ifconfig is unavailable you can also use ip a.

Find Your Local Network IP Address on Windows:

$ ipconfig
Ethernet adapter vEthernet (External Virtual Network):

   Connection-specific DNS Suffix  . : home
   Link-local IPv6 Address . . . . . : fe80::c542:1540:8737:c9cd%11
   IPv4 Address. . . . . . . . . . . : 192.168.1.3
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

In my case, my local network IP address is 192.168.1.3.

Testing It Out, Can the Container Reach the Docker Host?

We can test this out without needing to run a database or any service. We’ll just run an Alpine image, drop into a shell, install the ping utility and ping the Docker host.

# Start the Alpine container and drop into a Shell prompt.
docker container run --rm -it alpine sh

# Install the ping utility.
apk update && apk add iputils

# Ping your local network IP address (replace my IP address with yours).
ping 192.168.1.3

# You should see this output (hit CTRL+C to stop it):
PING 192.168.1.3 (192.168.1.3) 56(84) bytes of data.
64 bytes from 192.168.1.3: icmp_seq=1 ttl=37 time=0.539 ms
64 bytes from 192.168.1.3: icmp_seq=2 ttl=37 time=0.417 ms
64 bytes from 192.168.1.3: icmp_seq=3 ttl=37 time=0.661 ms

There you have it, you’ve successfully connected to your Docker host over your local network.

If you were running a database, you would use 192.168.1.3 (in my case) as part of your connection string, and then configure your database to bind on 0.0.0.0.

Keep in mind, if you do this, you may want to restrict the outside world from connecting to it because 0.0.0.0 will allow connections from anywhere. Details on how to do this will depend on what database / service you’re using.

But Nick, What If My Local IP Address Changes?

In development this could happen a lot, but in production not so much. In either case the above way will get the job done but there is a cleaner way to do it.

I think it was necessary to show the previous way of doing it because it demonstrates exactly how it works. Now, let’s use a custom Docker network instead.

Create a custom bridge Docker network:
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 mynet

Feel free to change around the IP addresses and mynet name if you want. In the end, after running this command you’ll be able to access your Docker host by the IP address of 192.168.0.1 regardless of what your real local IP address is.

Confirm it works again, but we’ll use the new network:
# Start the Alpine container, but this time we'll use our custom network.
docker container run --rm -it --net=mynet alpine sh

# Install the ping utility.
apk update && apk add iputils

# Ping the custom IP address we set up.
ping 192.168.0.1

# You should see this output (hit CTRL+C to stop it):
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.119 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=0.062 ms

Ok, so now we have a legit implementation of connecting to your Docker host over a custom network with a static IP address. Nice!

Plot twist: If you decide to run your own database, I would personally use Docker!

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments