Docker (Spotify) API - cannot connect to Docker

845 Views Asked by At

In my Docker (Spring Boot) application I would like to execute Docker commands. I use the docker-spotify-api (client).

I get different connection errors. I start the application as part of a docker-compose.yml.

This is what I tried so far on an 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)

Wthat is my environment on EC2 VPS:

$ 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)
4

There are 4 best solutions below

4
On BEST ANSWER

This situation is similar to https://github.com/spotify/docker-client/issues/838#issuecomment-318261710.

You use docker-compose on the host to start up your application; Within the container, the Spring Boot application is using docker-spotify-api.

What you can try is to mount /var/run/docker.sock:/var/run/docker.sock in you compose file.

0
On

It took me some time to figure out, but I was running https://hub.docker.com/r/alpine/socat/ locally, and also wanted to connect to my Docker daemon and couldn't (same errors). Then it struck me: the solution on that webpage uses 127.0.0.1 as the ip address to bind to. Instead, start that container with 0.0.0.0, and then inside your container, you can do this: DockerClient dockerClient = new DefaultDockerClient("http://192.168.1.215:2376"); (use your own ip-address of course).

This worked for me.

0
On

As @Benjah1 indicated, /var/run/docker.sock had to be mounted first.

To do so in a docker-compose / Docker Swarm environment you can do:

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

Furthermore, the other options resulted in errors because the default setting of Docker is that it won't open up to tcp/http connections. You can change this, of course, taking a small risk.

2
On

What is your DOCKER_HOST and DOCKER_CERT_PATH env vars value.

Try below as docker-client communicates with your local Docker daemon using the HTTP Remote API

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

please also verify the privileges of docker.sock is it visible to your app and check weather your docker service is running or not as from above screenshot your docker.sock looks empty but if service is running it should contain pid in it