Access host database from a docker container

Ryan Anderson :

If I have a mysql database running on some host machine, and that host is also running a docker container: How would I access the mysql database from within the docker container that is running on the host?.

For instance, is there a way to publish a hosts port to the container (the inverse of what docker run -p does)?

John Petrone :

There are several long standing discussions about how to do this in a consistent, well understood and portable way. No complete resolution but I'll link you to the discussions below.

In any event you many want to try using the --add-host option to docker run to add the ip address of the host into the container's /etc/host file. From there it's trivial to connect to the host on any required port:

Adding entries to a container hosts file

You can add other hosts into a container's /etc/hosts file by using one or more --add-host flags. This example adds a static address for a host named docker:

 $ docker run --add-host=docker:10.180.0.1 --rm -it debian
    $$ ping docker
    PING docker (10.180.0.1): 48 data bytes
    56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms
    56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms
    ^C--- docker ping statistics ---
    2 packets transmitted, 2 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms

Note: Sometimes you need to connect to the Docker host, which means getting the IP address of the host. You can use the following shell commands to simplify this process:

 $ alias hostip="ip route show 0.0.0.0/0 | grep -Eo 'via \S+' | awk '{ print $2 }'"
 $ docker run  --add-host=docker:$(hostip) --rm -it debian

Documentation:

https://docs.docker.com/engine/reference/commandline/run/

Discussions on accessing host from container:

https://github.com/docker/docker/issues/1143

https://github.com/docker/docker/issues/10023

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Docker replicate UID/GID in container from host

分類Dev

Copy folder with wildcard from docker container to host

分類Dev

How to mount volume from container to host in Docker?

分類Dev

Docker: Unable to connect to container from host

分類Dev

Angular and Docker: Unable to Access App from Host

分類Dev

Access Jetty server inside Docker from host

分類Dev

Access vagrant VMs from inside docker container

分類Dev

RUN Powershell Script in Docker Container From Host Powershell Script

分類Dev

Dockerfile: Copy directory from Windows host to docker container

分類Dev

Docker's container with --net=host is not reachable from the network

分類Dev

Copy file from bamboo user/host to docker container

分類Dev

docker for windows how to access docker daemon from container

分類Dev

difference between host and docker container

分類Dev

Get host IP in docker container

分類Dev

How to give non-root user in Docker container access to a volume mounted on the host

分類Dev

can my docker container app access the host's microphone and speaker? (Mac, Windows, Linux)

分類Dev

Generating a dump file from PostgreSQL database inside a Docker container

分類Dev

Issue Trying To Access Kubernetes API from Docker Container

分類Dev

Assign LAN IP address to Docker container different from host's IP address

分類Dev

Docker - Access nginx container using nginx server_name from another container

分類Dev

Restrict Internet Access - Docker Container

分類Dev

Restrict Internet Access - Docker Container

分類Dev

Access to docker container like an URL

分類Dev

Access to docker container like an URL

分類Dev

How to access service running docker with network_mode: "host" from external IP

分類Dev

How to delete postgres database within docker container

分類Dev

Can an ECS container have access to the docker socket?

分類Dev

Giving Docker access to db file outside container

分類Dev

How to access Docker container app on local?

Related 関連記事

  1. 1

    Docker replicate UID/GID in container from host

  2. 2

    Copy folder with wildcard from docker container to host

  3. 3

    How to mount volume from container to host in Docker?

  4. 4

    Docker: Unable to connect to container from host

  5. 5

    Angular and Docker: Unable to Access App from Host

  6. 6

    Access Jetty server inside Docker from host

  7. 7

    Access vagrant VMs from inside docker container

  8. 8

    RUN Powershell Script in Docker Container From Host Powershell Script

  9. 9

    Dockerfile: Copy directory from Windows host to docker container

  10. 10

    Docker's container with --net=host is not reachable from the network

  11. 11

    Copy file from bamboo user/host to docker container

  12. 12

    docker for windows how to access docker daemon from container

  13. 13

    difference between host and docker container

  14. 14

    Get host IP in docker container

  15. 15

    How to give non-root user in Docker container access to a volume mounted on the host

  16. 16

    can my docker container app access the host's microphone and speaker? (Mac, Windows, Linux)

  17. 17

    Generating a dump file from PostgreSQL database inside a Docker container

  18. 18

    Issue Trying To Access Kubernetes API from Docker Container

  19. 19

    Assign LAN IP address to Docker container different from host's IP address

  20. 20

    Docker - Access nginx container using nginx server_name from another container

  21. 21

    Restrict Internet Access - Docker Container

  22. 22

    Restrict Internet Access - Docker Container

  23. 23

    Access to docker container like an URL

  24. 24

    Access to docker container like an URL

  25. 25

    How to access service running docker with network_mode: "host" from external IP

  26. 26

    How to delete postgres database within docker container

  27. 27

    Can an ECS container have access to the docker socket?

  28. 28

    Giving Docker access to db file outside container

  29. 29

    How to access Docker container app on local?

ホットタグ

アーカイブ