~/docker/docker-container-connect-mysql-service-running-on-host.md

How to connect to a running mysql service on host from a docker container on same host

1 min readMarch 31, 2020by Gorav Singal#docker#mysql
// TL;DR

Use host.docker.internal (Mac/Windows) or --network=host (Linux) to reach host services from inside a Docker container. Localhost inside a container refers to the container itself.

How to connect to a running mysql service on host from a docker container on same host

Introduction

I have a host running mysql (not on a container). I have to run an nodejs app inside a container and access that mysql service here. Note: The host is not exposed on internet or local network.

When I’m inside that nodejs container, I can not use

  • localhost, as it would refer to the container’s IP, not the host
  • , I can not use the hostname, as the host is not exposed to the internet or local network

Running container in Host networking mode

Docker provides lot of networking models. One of them is to use Host networking.

docker run -it --net=host -d node

Now when inside the container, you try to use hostname as localhost, it will work like a charm.

Docker host networking mode

With this networking mode, the container shares the same networking space as of host. The container does not gets its own ip. Example, if you run apache container on port 8080 with this networking mode. That service is available on localhost:8080 on host, without need to expose or binding the port.

It is important to note that with this option, port binding options are ignored like -p, —publish. And, this mode works only on linux hosts. This do not work on docker for mac, windows.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to Copy Local Docker Image to Another Host Without Repository and Load

How to Copy Local Docker Image to Another Host Without Repository and Load

Introduction Consider a scenario where you are building a docker image on your…

MySql update query - Update column by string replacement in all records

MySql update query - Update column by string replacement in all records

Problem Statement In a mysql table, I wanted to replace the hostname of the…

Docker: unauthorized: incorrect username or password.

Docker: unauthorized: incorrect username or password.

While running docker commands with some images, I started getting error: The…

Docker image for Drupal 7, and Php extension MongoDB installed.

Docker image for Drupal 7, and Php extension MongoDB installed.

You have drupal 7 image from docker hub, and want to connect tomongo db via php…

Docker Push: How to push your docker image to your organization in hub.docker.com

Docker Push: How to push your docker image to your organization in hub.docker.com

Tag the image, by seeing its image id, from docker images command docker tag 04d…

How to connect Php docker container with Mongo DB docker container

How to connect Php docker container with Mongo DB docker container

Goto your command terminal. Type: This will expose port: 27017 by default. You…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…