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:
|
<<: Explain how to analyze SQL efficiency
>>: Detailed explanation of the role of the new operator in Js
Table of contents Preface 1. System Service Contr...
Table of contents 1. Handwritten instanceof 2. Im...
Preface Any application that can be written in Ja...
Table of contents 1. useState hook 2. useRef hook...
1. Concat function. Commonly used connection stri...
The form elements with visibility=hidden and displ...
Call How to call Amap API? The official open docu...
Table of contents Early creation method Factory P...
In Linux C/C++, thread-level operations are usual...
Table of contents Virtual DOM What is virtual dom...
CI/CD Overview CI workflow design Git code versio...
HTML onfocus Event Attributes Definition and Usag...
This article shares the specific code for Vue to ...
Request logic Front-end --> Request nginx via ...
This article shares the installation tutorial of ...