Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the Docker REST API URL?

Tags:

docker

jenkins

I have installed the Docker build step plugin for Jenkins.

The documentation is telling me:

Name      : Choose a name for this Docker cloud provider Docker URL: The URL to use to access your Docker server API (e.g: http://172.16.42.43:4243) 

How can I find my URL to the REST API (I have Docker installed on my host)?

like image 273
DenCowboy Avatar asked May 12 '16 06:05

DenCowboy


People also ask

Does docker have a REST API?

The Docker Engine API is a RESTful API accessed by an HTTP client such as wget or curl , or the HTTP library which is part of most modern programming languages.

What is the URL of my docker container?

docker ps command shows the running container which displays the port your application is running. On browser type http://localhost:54529/your_application_page. if you are running application locally, then this is the port which needs to be changed in browser to access container application.

What port does docker API use?

It is conventional to use port 2375 for un-encrypted, and port 2376 for encrypted communication with the daemon.


2 Answers

If you are on Linux and need to connect to Docker API on the local machine, its URL is probably unix:///var/run/docker.sock, like it is mentioned in documentation: Develop with Docker Engine SDKs and API

By default the Docker daemon listens on unix:///var/run/docker.sock and the client must have root access to interact with the daemon. If a group named docker exists on your system, docker applies ownership of the socket to the group.

This might be helpful if you are connecting to Docker from a JetBrains IDE.

like image 111
Anton Belonovich Avatar answered Sep 27 '22 23:09

Anton Belonovich


Here are two approaches.

How do I access the Docker REST API remotely?

Warning: After this setup your Docker REST API port (in this case 1111) is exposed to remote access.

Here is how I enabled it on Ubuntu 16.04 (Xenial Xerus).

  1. Edit the docker service file (it is better to avoid directly editing /lib/systemd/system/docker.service as it will be replaced on upgrades)
sudo systemctl edit docker.service 
  1. Add the following content
[Service] ExecStart= ExecStart=/usr/bin/docker daemon -H fd:// -H tcp://0.0.0.0:1111 

For docker 18+, the content is a bit different:

[Service] ExecStart= ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:1111 
  1. Save the modified file. Here I used port 1111, but any free port can be used.

  2. Make sure the Docker service notices the modified configuration:

systemctl daemon-reload 
  1. Restart the Docker service:
sudo service docker restart 
  1. Test
curl http://localhost:1111/version 
  1. See the result
{"Version":"17.05.0-ce","ApiVersion":"1.29","MinAPIVersion":"1.12","GitCommit":"89658be","GoVersion":"go1.7.5","Os":"linux","Arch":"amd64","KernelVersion":"4.15.0-20-generic","BuildTime":"2017-05-04T22:10:54.638119411+00:00"} 

Now you can use the REST API.

How do I access the Docker REST API through a socket (from localhost)?

Connect the internal Unix socket somewhat like this,

Using curl

curl --unix-socket /var/run/docker.sock http:/localhost/version 

And here is how to do it using PHP

$fs = fsockopen('/var/run/docker.sock');  fwrite($fs, "GET / HTTP/1.1\r\nHOST: http:/images/json\r\n\r\n");  while (!feof($fs)) {     print fread($fs,256); } 

In PHP 7 you can use curl_setopt with the CURLOPT_UNIX_SOCKET_PATH option.

like image 29
aimme Avatar answered Sep 28 '22 00:09

aimme