connect to docker daemon from inside docker container

后端 未结 1 985
故里飘歌
故里飘歌 2021-01-12 04:18

Im trying configure the docker daemon so i can connect to it from inside the docker containers i start..

So i changed /etc/docker/daemon.json to

{
           


        
1条回答
  •  一生所求
    2021-01-12 04:52

    You can start your containers mounting the host docker socket into your containers.

    docker run -v /var/run/docker.sock:/var/run/docker.sock ...
    

    With this setup, Docker clients inside the containers will be using the Docker daemon from the host. Your containers will be able to build, run, push etc. using daemon running in host. Please note that with these setup everything is happening on the host, so if you start new containers they will be “sibling” containers.

    EDIT

    If you are using the bridge network, you can connect to any service running on host machine using host IP address.

    For example, I have mysqld running on my host with IP 10.0.0.1 and from a container I can do

    mysql -u user -p -h 10.0.0.1
    

    The trick is to find out the host IP address from containers.

    In Docker for Mac (I am running version 17.07.0) is as simple as connecting to the special host "docker.for.mac.localhost"

    Another option is to add an alias IP to your loopback interface

    sudo ifconfig lo0 alias 192.168.1.1
    

    And then when running containers add a host for this alias IP

    docker run --rm -ti --add-host host-machine:192.168.1.1 mysql:5.7 bash
    

    With this setup, inside container you should be able to do

    mysql -u user -p -h host-machine
    

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