How to access the local machine (host machine) in Docker

How to access the local machine (host machine) in Docker

Question

How to access the local database in Docker? Using 127.0.0.1 is definitely not an option, because this refers to the container itself in the Docker container. Therefore, it is necessary to solve the problem through other channels.

Solution

You can choose one of the following methods according to the type of operating system.

DockerFile:

RUN /sbin/ip route|awk '/default/ { print $3,"\tdockerhost" }' >> /etc/hosts

RunTime:

(may not use) docker run --add-host dockerhost:`/sbin/ip route|awk '/default/ { print $3}'` [my container]
(useful) docker run --add-host=dockerhost:`docker network inspect --format='{{range .IPAM.Config}}{{.Gateway}}{{end}}' bridge` [IMAGE]

Docker for Mac (17.12+):

docker.for.mac.host.internal
MONGO_SERVER=docker.for.mac.host.internal

# docker-compose.yml
version: '3'

services:
 API:
  build: ./api
  volumes:
   - ./api:/usr/src/app:ro
  ports:
   - "8000"
  environment:
   - MONGO_SERVER
  command: /usr/local/bin/gunicorn -c /usr/src/app/gunicorn_config.py -w 1 -b :8000 wsgi

Linux

Solution 1
/sbin/ip route|awk '/default/ { print $3 }'
docker run --add-host dockerhost:`/sbin/ip route|awk '/default/ { print $3}'` [my container]
# Solution 2
-e "DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')"

Principle

To understand the principle, you need to understand the model of computer networks and the model implemented by Docker. In fact, a virtual bridge docker0 is implemented inside Docker. The virtual address of the external host in the bridge, that is, docker.for.mac.host.internal, is needed to access the external host in the container. If you are interested, you can learn about Docker's network principles, computer network principles, and docker compose.

Reference

[1].(stackoverflow)insert-docker-parent-host-ip-into-containers-hosts-file

[2].(stackoverflow)how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed graphic tutorial on how to enable remote secure access with Docker
  • Solution to the problem of not being able to access the home page when adding a tomcat container to Docker
  • How to directly access the docker for windows container intranet through an independent IP
  • A troubleshooting experience of centos Docker bridge mode unable to access the host Redis service
  • Detailed explanation of using Docker to build externally accessible MySQL
  • Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP
  • How to use Docker container to access host network
  • Solution to the problem that docker nginx cannot be accessed after running
  • Method for accessing independent IP in Docker container
  • Three ways to communicate between Docker containers

<<:  Explain how to analyze SQL efficiency

>>:  Detailed explanation of the role of the new operator in Js

Recommend

A detailed introduction to Linux system configuration (service control)

Table of contents Preface 1. System Service Contr...

Don’t bother with JavaScript if you can do it with CSS

Preface Any application that can be written in Ja...

Introduction to useRef and useState in JavaScript

Table of contents 1. useState hook 2. useRef hook...

Summary of MySQL commonly used type conversion functions (recommended)

1. Concat function. Commonly used connection stri...

What hidden attributes in the form can be submitted with the form

The form elements with visibility=hidden and displ...

Vue Virtual DOM Quick Start

Table of contents Virtual DOM What is virtual dom...

Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

CI/CD Overview CI workflow design Git code versio...

Detailed explanation of HTML onfocus gain focus and onblur lose focus events

HTML onfocus Event Attributes Definition and Usag...

Vue implements horizontal scrolling of marquee style text

This article shares the specific code for Vue to ...