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

MySQL slow_log table cannot be modified to innodb engine detailed explanation

background Getting the slow query log from mysql....

mysql server is running with the --skip-grant-tables option

The MySQL server is running with the --skip-grant...

jQuery implements navigation bar effect with expansion animation

I designed and customized a navigation bar with a...

Docker - Summary of 3 ways to modify container mount directories

Method 1: Modify the configuration file (need to ...

Will css loading cause blocking?

Maybe everyone knows that js execution will block...

Implementation of mysql split function separated by commas

1: Define a stored procedure to separate strings ...

Steps to change mysql character set to UTF8 under Linux system

Table of contents 1. Check the MySQL status in th...

MySQL Database Iron Laws (Summary)

Good database specifications help reduce the comp...

Linux file and user management practice

1. Display the files or directories in the /etc d...

Basic usage knowledge points of mini programs (very comprehensive, recommended!)

Table of contents What to do when registering an ...

MySQL learning database backup detailed explanation

Table of contents 1.DB,DBMS,SQL 2. Characteristic...

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

Detailed explanation of Angular structural directive modules and styles

Table of contents 1. Structural instructions Modu...

Example of how to implement keepalived+nginx high availability

1. Introduction to keepalived Keepalived was orig...