1

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)
tm1701
  • 7,307
  • 17
  • 79
  • 168

4 Answers4

1

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.

benjah1
  • 1,440
  • 1
  • 13
  • 17
  • Trivial: of course I have Docker installed. How could I run docker-compose without docker installed on the VPS? – tm1701 May 01 '19 at 18:19
  • 1
    https://github.com/spotify/docker-client/issues/838#issuecomment-318261710 could this be the case? Is it the case that you use docker-compose on the host to start up your stack; Within the container, the Spring Boot application is using docker-spotify-api? – benjah1 May 01 '19 at 20:30
  • Not a stack, just docker-compose. Can i put a stack on 1 ec2 aws? I guess hoor suggestion on mounting docker.sock will solve the problem! Please pour as an answer. – tm1701 May 02 '19 at 06:41
  • Updated my answer. I was referred to solution stack (https://en.wikipedia.org/wiki/Solution_stack). I assumed there is multiple components within the docker-compose file. Like ELK stack, LAMP stack, you can put the stack on 1 aws ec2. I didn't mean the 'docker stack'. – benjah1 May 02 '19 at 16:15
1

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.

tm1701
  • 7,307
  • 17
  • 79
  • 168
0

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

  • Tx, but didnot help. The DOCKER_HOST and DOCKER_CERT_PATH env vars are both empty. – tm1701 May 01 '19 at 18:26
  • Your docker server is running ? Please check as Docker service should be running on your system before you use docker client to connect – rohitkumar May 02 '19 at 03:32
0

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.