How to connect to local MySQL server through Docker?

前端 未结 3 1985
时光说笑
时光说笑 2020-12-01 11:04

This is more a general question for how to connect to local services through Docker. There\'s a similar question in a Github issue here that doesn\'t seem to have any resolu

相关标签:
3条回答
  • 2020-12-01 11:33

    To help with several of the additional questions and the main post I would like to link to a repo I have been managing to manage my local development. I have stopped trying to run any service for my development directly on OS X and use Docker containers as they are the exact same running on production and my environments can be matched and streamlined.

    This repo consists of a web server, database server and a data container to load the MySQL databases.

    I have and will continue to support this repo and have recently upgraded the documentation to make it turn key for other developer.

    Docker Repo on GitHub

    0 讨论(0)
  • 2020-12-01 11:52

    The Docker CLI docs give this solution (which assumes you are running on a Linux host with ):

    Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the --add-host flag. To find the host’s address, use the ip addr show command.

    The flags you pass to ip addr show depend on whether you are using IPv4 or IPv6 networking in your containers. Use the following flags for IPv4 address retrieval for a network device named eth0:

    $ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`
    $ docker run  --add-host=docker:${HOSTIP} --rm -it debian
    

    Then the name docker inside the container will map to the host's IP address. For your case, you could use docker run --add-host=mysql_server:$(hostip) ...

    If using Boot2Docker, it sets up a mapping to the host at a predefined address, so on that platform the equivalent to the above is just the one command:

    $ docker run  --add-host=docker:192.168.59.3 --rm -it debian
    
    0 讨论(0)
  • 2020-12-01 11:52

    On a mac with boot2docker, you can use homebrew's default mysql/mariadb settings by adding the Mac OS Host.

    This worked for me (with, what I believe, are default settings).

    0 讨论(0)
提交回复
热议问题