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 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥100 网页游戏在本地页面修改游戏结果
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead