Docker(Spotify)API - 无法连接到Docker

问题描述 投票:2回答:3

在我的Docker(Spring Boot)应用程序中,我想执行Docker命令。我使用docker-spotify-api(客户端)。

我得到不同的连接错误。我将应用程序作为docker-compose.yml的一部分启动。

这是我到目前为止在EC2 AWS VPS上尝试的内容:

docker = DefaultDockerClient.builder()
  .uri(URI.create("tcp://localhost:2376"))
  .build();
=> TCP protocol not supported. 

docker = DefaultDockerClient.builder()
  .uri(URI.create("tcp://localhost:2375"))
  .build();
=> TCP protocol not supported. 

docker = new DefaultDockerClient("unix:///var/run/docker.sock");
==> No such file

docker = DefaultDockerClient.builder()
          .uri("unix:///var/run/docker.sock")
          .build();
==> No such file

docker = DefaultDockerClient.builder()
            .uri(URI.create("http://localhost:2375")).build();
or
docker = DefaultDockerClient.builder()
            .uri(URI.create("http://localhost:2376")).build();
or 
docker = DefaultDockerClient.builder()
              .uri(URI.create("https://localhost:2376"))
              .build();
==> Connect to localhost:2376 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

我在EC2 VPC上的环境是什么:

$ ls -l /var/run
lrwxrwxrwx 1 root root 6 Nov 14 07:23 /var/run -> ../run

$ groups ec2-user                              
ec2-user : ec2-user adm wheel systemd-journal docker   

$ ls -l /run/docker.sock                       
srw-rw---- 1 root docker 0 Feb 14 17:16 /run/docker.sock

echo $DOCKER_HOST $DOCKER_CERT_PATH
(empty)
docker amazon-ec2 docker-api spotify-docker-client
3个回答
2
投票

这种情况类似于https://github.com/spotify/docker-client/issues/838#issuecomment-318261710

您在主机上使用docker-compose来启动您的应用程序;在容器内,Spring Boot应用程序使用docker-spotify-api。

你可以尝试在你的撰写文件中安装/var/run/docker.sock:/var/run/docker.sock。


2
投票

正如@ Benjah1所指出的那样,必须首先安装/var/run/docker.sock。

要在docker-compose / Docker Swarm环境中执行此操作,您可以执行以下操作:

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

此外,其他选项导致错误,因为Docker的默认设置是它不会打开到tcp / http连接。当然,你可以改变这种风险。


0
投票

什么是你的DOCKER_HOST和DOCKER_CERT_PATH env vars值。

请尝试以下内容,因为docker-client使用HTTP Remote API与本地Docker守护程序进行通信

    final DockerClient docker = DefaultDockerClient.builder()
      .uri(URI.create("https://localhost:2376"))
      .build();

还请验证docker.sock的权限是否对您的应用程序可见并检查您的docker服务运行的天气与否从上面截图您的docker.sock看起来是空的但如果服务正在运行它应该包含pid

© www.soinside.com 2019 - 2024. All rights reserved.