0

I want to write a script which going to use docker API. It will communicate with the docker engine which runs on the host machine. I don't wanna install the entire docker to my image just CLI and later use the host network to communicate with the docker engine.

My script under the hood gonna run commands like docker image inspect someiamge

112Legion
  • 1,129
  • 12
  • 25
  • There are [Docker SDKs](https://docs.docker.com/engine/api/sdk/) for many popular languages, and using one of these might be safer and easier than trying to script `docker` subprocesses. – David Maze Oct 18 '22 at 09:47
  • @DavidMaze the SDK client is not supported for PHP. Look like using SDK would add additional dependency which is poorly maintained. I don't think it would be easier. – 112Legion Oct 18 '22 at 10:22
  • @DavidMaze I think you should point to SDK as an answer to this question. one of the possible ones. – 112Legion Oct 18 '22 at 11:26

2 Answers2

1

You can use binary distribution of docker, which can be downloaded from here, you can get docker client binaries there.

Here is the documentation of installation of a docker from binaries. Just skip part of starting docker engine and make sure that your docker client points to a correct remote engine (mounting /var/run/docker.sock)

Monsieur Merso
  • 1,459
  • 1
  • 15
  • 18
0

I thinking about using docker image to extract the CLI client.

COPY --from=docker:latest /usr/local/bin/docker  /usr/local/bin/

Example of dockerfile

FROM php:8-cli-alpine

COPY --from=docker:latest /usr/local/bin/docker  /usr/local/bin/

And later you could use this image with following command

docker run -it --rm --entrypoint docker -v docker run -it --rm --entrypoint docker -v /var/run/docker.sock:/var/run/docker.sock yourimage:tag image ls

This has a significant downside. The docker image does not support linux/arm/v7 architecture.

 => ERROR [linux/arm/v7 internal] load metadata for docker.io/library/docker:latest                                                                                                                        4.5s
 => ERROR [linux/arm/v7 internal] load metadata for docker.io/library/composer:2  

Better to use SDK.

112Legion
  • 1,129
  • 12
  • 25
  • 1
    Copying binaries between images like this doesn't necessarily work reliably. You also need to know what shared-library dependencies and other files that binary needs, and for some basic things like the system C library there might be incompatibilities between the versions that binary needs and what's available in your destination image. – David Maze Oct 18 '22 at 09:46
  • @DavidMaze as far as I can remember Go consists of one binary file which does not really on any shared library. – 112Legion Oct 18 '22 at 10:19