How do I find the Docker REST API URL?
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)?
Solution 1:
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.
Solution 2:
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).
- 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
- 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
Save the modified file. Here I used port
1111
, but any free port can be used.Make sure the Docker service notices the modified configuration:
systemctl daemon-reload
- Restart the Docker service:
sudo service docker restart
- Test
curl http://localhost:1111/version
- 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.
Solution 3:
It depends on your host, but look for /etc/default/docker
or /var/lib/boot2docker/profile
(for Docker Machine hosts using a boot2docker VM).
You will see the port used by the docker daemon, for instance:
DOCKER_OPTS="-H unix:// -H tcp://0.0.0.0:2375"
^^^^^
Then get the IP address of the machine hosting your Docker daemon.
(With a Docker Machine created host, that would be: docker-machine ip <yourmachine>
.)
The URL to use is the combination of those the IP address and the port.