tony qi
tony qi

Reputation: 105

How can I use REST API to interact with the Docker engine?

We can use the command docker images to list the Docker images we have on local host.

Now I want to get the same information from a remote server by sending an HTTP GET request in Firefox or Chrome. Does Docker provide some REST API to do this?

I did a lot of search. For example: Examples using the Docker Engine SDKs and Docker API

It provides a way something like this:

curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json

I know a little about Unix sockets, and I don't think this is what I want. The URL (http:/v1.24/containers/json) is so weird and don't even have a server name in it. I don't think it can work on a remote server. (It does work on a local server.)

Is there any official documentation that Docker provides on this topic?

Upvotes: 4

Views: 2421

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

You need to expose the Docker daemon on a port.

You can configure the Docker daemon to listen to multiple sockets at the same time using multiple -H options:

listen using the default Unix socket, and on two specific IP addresses on this host.

$ sudo dockerd -H unix:///var/run/docker.sock -H tcp://192.168.59.106 -H tcp://10.10.10.2

The Docker client will honor the DOCKER_HOST environment variable to set the -H flag for the client. Use one of the following commands:

https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-socket-option

You need to do this by creating a systemd dropin:

mkdir -p /etc/systemd/system/docker.service.d/
cat > /etc/systemd/system/docker.service.d/10_docker.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// -H tcp://0.0.0.0:2376
EOF

Then reload and restart Docker:

systemctl daemon-reload
systemctl restart docker

Note: this way you would be exposing your host and you shouldn't do it this way in production. Please read more about this on the link I shared earlier.

Upvotes: 3

Related Questions