Docker SDK For Golang In 5 Sec – Part 1

Building Tools around Docker and ecosystem is one of trending thing in containerization world and another hand Golang is taking standard to develop Cloud-Native Application . and we know Docker is return in Golang so its great in-able for Go developer. lets see how to retrieve docker images and Docker Container ID not by Commands .but we see using Docker API With Golang .

if you don’t know basic of golang go visit and learn Golang : – https://gopherlabs.kubedaily.com/

Step 1. write Golang Simple App using Docker API

package main

import (

"context"

"fmt"


"github.com/docker/docker/api/types"

"github.com/docker/docker/client"

)

func main() {

cli, err := client.NewClientWithOpts(client.WithVersion("1.37"))

if err != nil {

panic(err)

}

containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})

if err != nil {

panic(err)

}

for _, container := range containers {

fmt.Printf("%s %s\n", container.ID[:10], container.Image)

}
}

How its work !

first thing first we need to download Docker API simple type command

go get github.com/docker/docker/client

It will download Docker API and ready to use it . what other library we need fmt and Context Library . fmt will give ability to printf and scanf and formatting text around feature. and Context will be used in this program as the top-level Context for incoming requests from docker client do retrieve the data .

"github.com/docker/docker/api/types"

"github.com/docker/docker/client"

import above docker api for communicate with docker daemon .

func main() {

cli, err := client.NewClientWithOpts(client.WithVersion("1.37"))

if err != nil {

panic(err)

}

define docker client api version using following command it will directly communicate with docker cli

cli, err := client.NewClientWithOpts(client.WithVersion("1.37"))

this Particular this if statement will give you if there some error validation

if err != nil {

panic(err)

}

create variable as containers it will talk with docker cli and containerlist will talk with your context library to communicate with incoming request via background function of context . type.ContainerlistOptions will talk with docker api type will retrive all docker images .

containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})

following for loop will retrieve all docker container from above function at iterate it . and we need only running docker image id and docker docker image name .

for _, container := range containers {

fmt.Printf("%s %s\n", container.ID[:10], container.Image)

}
}

Step 2 . How about containerizing above program so we need dockerfile .

FROM golang:latest
// we are using golang:lastest as docker base image to run this golang program //
RUN mkdir /app

ADD . /app/

WORKDIR /app
// after that create app directory using RUN mkdir /app and using ADD instruction add working directory to app folder inside a container now our application inside app directory //
RUN go get github.com/docker/docker/client
// we need docker api for that using RUN command as we are using golang as base image so we can directly download docker client using //
RUN go build -o main .
// go build command will build your app.go program

CMD ["/app/main"]

// CMD is command line instruction so whatever go binary we build it will take that binary and run for us .

Step 3: How its Work in real time

git clone https://github.com/sangam14/GopherLabs
// clone ditectory
cd GopherLabs/ 
~/GopherLabs $ cd DockerSDK-Go/ [node1] (local)  ~/GopherLabs/DockerSDK-Go $ cd simple-app/
// jump into working directory 
~/GopherLabs/DockerSDK-Go/simple-app $ docker build -t simple .
// build dockerfile 
~/GopherLabs/DockerSDK-Go/simple-app $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE simple latest ca3a98b3c7fb About a minute ago 1.05GB golang latest 5fbd6463d24b 26 hours ago 810MB
// verify docker images 
/GopherLabs/DockerSDK-Go/simple-app $ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -it simple:latest
// when we are this container it will communicate to docker client to docker daemon via REST API over docker socker and runs the container and prints container ID and container image 
ca3a98b3c7fb  simple

for learning more visit : https://gopherlabs.kubedaily.com/

You may also like...