About this post
In this post, I’ll show you ways to manipulate docker operations directly from your Go codes as an alternative to using Docker cli and docker-compose script.
It is beyond the scope of this post to discuss in detail Docker’s architecture. Refer to the official Docker documentation for detail.
For the purpose of this post, Docker is a client-server application where the server is running as a daemon in your platform (Linux, macOS and Windows).
In essence, when you work with your Go code (i.e. the client) to interact with Docker you are basically interacting with Docker server via sockets.
There are two ways you can get your Go code to interact with Docker server. One approach is to use Docker’s Go SDK. The other approach is to use the RESTFul API. I’ll show you how it is done using the two approaches specifically when it comes to pulling Docker images from Docker registry.
Using Go SDK
The steps involve in using the SDK are:
-
STEP 1: Instantiate a Docker client;
-
STEP 2: Use the client to call the operations you are interested in.
Here is the example code to pull down an image:
Using Docker Engine API
In this approach, configure the Go Http client package to dial up docker socket. In the case of Linux and macOS, you can simply use unix socket as shown below:
For Windows programmers, please refer to the operating system’s equivalent.
Next, connect your client to the Docker API. The API to pull images is in the endpoint /images/create. Yes, pull and create images have the same endpoint. So it can be confusing.
Here is the example code demonstrating operations to call the API.
Last words
As you can see the steps involved in creating a Go base client using SDK and API is fairly straightforward.
However, you might be asking why not just execute shell commands via Docker’s cli with Docker Compose script. Of course, you could. What I have presented here are illustrations of alternative ways to manipulate docker from Go code. It is for you to find a use case appropriate for your project. For example, you might find it useful to create native apps manipulating a few Docker containers where you might not need platforms as complex as Kubernetes to manipulate containers.
In this post I have covered one operation, which is to pull an image. The steps involved in manipulating other operations is broadly the same except for details such as SDK arguments or API parameters settings. I’ll cover those and other Docker components in future posts.