无法从容器内访问Docker API [英] Docker API not accessible from within a container

查看:98
本文介绍了无法从容器内访问Docker API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以对Docker API进行cUrl调用,但是如果我从容器(docker-compose)运行相同的脚本,则无法访问任何API端点.

I can make cUrl calls to Docker API but if I run the same script from a container (docker-compose), I can't reach any API endpoint.

我的请求如下:

$ curl --unix-socket /var/run/docker.sock -X POST http:/localhost/containers/json

原因可能是使用本地主机,但目前找不到解决方案.有什么建议吗?

The reason could be the use of localhost but I can't find the solution for now. Any suggestions?

这是我正在使用的堆栈的简要摘要(Api平台 https://api-platform.com/).每个容器都附加到一个"api_default"容器上.桥接网络:

EDIT : Here's a brief summary of the stack i'm using (Api-Platform https://api-platform.com/). Every container is attached to an "api_default" bridge network :

  • php =基于Symfony的应用程序
  • nginx = NGINX Web服务器
  • psql =托管主数据库
  • mssql =用于同步数据
  • ...

我的"php"中确实有一个shell脚本.旨在到达Docker API的容器.总而言之,我要求在"mssql"中输入"mssql".容器与外部服务同步,然后我请求"psql"容器以复制这些数据(通过共享卷对csv文件进行导出/导入).

I do have a shell script in my "php" container which is meant to reach the Docker API. To summarize, I request the "mssql" container to sync with an external service and then I ask to "psql" container to copy those datas (export/import done with csv files through shared volumes).

问题是:

  • 如果我执行 docker-compose exec php bin/console app:sync (运行脚本的Symfony命令),则一切正常:容器通过docker.sock与Docker API进行通讯,而我可以同步我的数据库.
  • 但是,如果我使用api端点(localhost:8080/api/sync)或通过VueJS应用(Axios)运行相同的脚本,则Docker API永远不会返回任何响应.
  • If I do docker-compose exec php bin/console app:sync (Symfony command which runs the script), everything works fine : the containers communicate through docker.sock with the Docker API and I can sync my databases.
  • But if I run the same script with an api endpoint (localhost:8080/api/sync) or via a VueJS app (Axios), the Docker API never return any response.

下面是我的脚本的一部分:

Below is a part of my script :

curl -XPOST -H 'Content-Type: application/json' \
      --unix-socket /var/run/docker.sock http:/localhost/containers/mssql/exec \
      -d '{
        "AttachStdout":true,
        "Tty":true,
        "Cmd":["/opt/mssql-tools/bin/sqlcmd", ...]
      }'

这是我在api端点控制器中调用Symfony命令的方式:

And here is the way I call my Symfony command in the api endpoint Controller :

 /**
  * @Route("/api/sync", name="sync", methods={"GET"})
  */
  public function sync(KernelInterface $kernel)
  {
    $application = new Application($kernel);
    $application->setAutoExit(false);

    $input = new ArrayInput(array(
        'command' => 'app:sync'
    ));

    $output = new BufferedOutput();
    $application->run($input, $output);
    $content = $output->fetch();

    return new Response($content);
  }

也许我应该替换"localhost";与"api_default"一起使用;到达良好的网络,但这是行不通的……?

Maybe should I replace "localhost" with "api_default" to reach the good network, but that doesn't work ... ?

推荐答案

是的.如果要在一个Docker容器和另一个Docker容器之间进行通信,至少需要使用服务名称而不是 localhost ,因为正如@BMW所说的那样, localhost 相对于您所在的计算机,从容器的角度来看,localhost是容器本身.

Yep. If you want to communicate between one docker container and another docker container, at the very least, you need to use the service name instead of localhost because, as @BMW said, localhost is relative to the machine you are on, and from the container's point of view, localhost is the container itself.

最近我也遇到类似的情况,仅使用服务名称是不够的.我还必须创建一个网络并将每个服务添加到该网络.像这样:

I had a similar situation recently where it wasn't enough to use the service name. I also had to create a network and add each service to that network. Like this:

my_service:
    ...
    ...
    ...
    networks:
      - backend

networks:
  backend:
    driver: bridge

这篇关于无法从容器内访问Docker API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆