douguxun6866 2015-12-09 10:34
浏览 1383
已采纳

获取Docker链接容器localhost指向主机localhost

So on my host environment I have this tomcat service running on port 10000 so I can access the office internal services.

I have a windows hosts entry:

localhost developer.mycompany.com

So I can access the endpoint developer.mycompany.com:10000/some/url and if successful return a json response.

I also have a Docker compose file that has spun up nginx and php-fpm containers and linked them to run a linux based PHP development environment.

What I am trying to achieve is to make the docker container(s) aware of the developer.mycomoany.com host entry. So when my PHP code on my linke containers sends a POST request to http://developer.mycompany.com:10000/some/urlit knows about the host entry and is able to hit that end point.

I have tried the config net=host but that doesn't work with linked containers.

PHP app error message:

{"result":{"success":false,"message":"Error creating resource: [message] fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known [file] /srv/http/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php [line] 282 [message] fopen(http://developer.mycompany.com:10000/app/register): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known [file] /srv/http/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php [line] 282"}}

How do I enable my PHP app on my linked containers to talk to the host developer.mycompany.com (localhost) entry?

Here is my docker-compose:

app:
  image: linxlad/docker-php-fpm
  tty: true
  ports:
   - "9000:9000"
  volumes:
   - ./logs/php-fpm:/var/log/php-fpm
   - ~/Development/Apps/php-hello-world:/srv/http
web:
  image: linxlad/docker-nginx
  ports:
   - "8080:80"
  volumes:
   - ./conf/nginx:/etc/nginx/conf.d
   - ./logs/nginx:/var/log/nginx
   - ~/Development/Apps/php-hello-world:/srv/http
  links:
   - app

Edits:

Her is my ifconfig output from docker machine.

enter image description here

Thanks

  • 写回答

1条回答 默认 最新

  • douhuo3696 2015-12-14 10:41
    关注

    I found the IP of my docker machine and used that in my hosts nginx proxy config like such which then redirected it to the nginx location config in my docker container.

    Host nginx:

    location /apps/myapp/appversion {
        proxy_pass   http://192.168.99.100:8080;
    } 
    

    Docker Nginx container config:

    location /apps/myapp/appversion {
        try_files $uri $uri/ /index.php?args;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php last;
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!