Creating Docker container (with out complete linux distribution and without docker image)ie From scratch only with simple go program

sonu kushwaha
3 min readOct 18, 2021

ALERT!!! go through this article only when you are well familiar with the container (docker/podman). other wise you will be confused with the container concepts.

As, we know container are just the isolated and restricted process(in terms of linux) and here in this blog we will be seeing container process that will be without linux distribution into it .

Bundle is the only thing , that runc(actual container runtime) requires , bundle is nothing but the collection of regular fileSystem directory with at least one executable file init, and a config.json file and that's it required for running the container not even the container images from docker hub .

now , I am going to create an directory(tj) inside which the complete bundle will reside , then i have created the config.json file with the help of “runc spec” with its content in the following image

[sonu@localhost ~]# mkdir tj
[sonu@localhost ~]# cd tj
[sonu@localhost tj]# ls
[sonu@localhost tj]# runc spec
[sonu@localhost tj]# ls
config.json

now , we need to have the root_file_system for our container to get the executable file form it ,execute.and then i have added the simple go code to execute the name of the host.

[sonu@localhost ~]# mkdir rootfs/
[sonu@localhost ~]# cd rootfs
[sonu@localhost rootfs]# vi main.go
[sonu@localhost rootfs]# cat main.go
package main
import "os"
import "fmt"
func main() {
fmt.Println(os.Hostname())
}

after , this we will be building this go code to an executable file of linux, here i have named the executable file as sonu , along with its description as shown below

[sonu@localhost rootfs]# GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o sonu
[sonu@localhost rootfs]# ls
main.go sonu
[root@015cf2df0834 rootfs]# file sonu
sonu: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

now we need to modify the config.json file ie with process.args[0] to /sonu,disable terminal and change the hostname as per choice, you can refer following two images for changes i have made…

so , we are almost done with creating container without complete linux, and container without container images,

now what , I am going to do is create the container , and start with the help of container runtime ie runc here rather than container manger(docker/podman)

the output showing below is the hostname i have set, and it running good.

[sonu@localhost rootfs]# sudo runc create sonu1
[sonu@localhost rootfs]# sudo runc start sonu1
this is hostname changed by sonu <nil>

--

--